19 #ifndef ASYLO_PLATFORM_PRIMITIVES_EXTENT_H_ 20 #define ASYLO_PLATFORM_PRIMITIVES_EXTENT_H_ 26 #include <type_traits> 43 constexpr Extent(
void *data, size_t size)
53 : data_(raw_pointer(data)),
size_(
sizeof(
T)) {}
63 constexpr Extent(T *data, size_t count)
64 : data_(raw_pointer(data)),
size_(
count *
sizeof(
T)) {}
70 void *
data() {
return data_; }
73 const void *
data()
const {
return data_; }
78 bool empty()
const {
return data_ ==
nullptr || size_ == 0; }
85 memcpy(out, data_, size_);
94 return size_ >=
sizeof(T) ?
reinterpret_cast<T *>(data_) :
nullptr;
101 template <
typename T>
102 const T *
As()
const {
103 return size_ >=
sizeof(T) ?
reinterpret_cast<
const T *>(data_) :
nullptr;
107 template <
typename T>
108 static constexpr void *raw_pointer(
const T *ptr) {
109 return reinterpret_cast<
void *>(
const_cast<T *>(ptr));
112 template <
typename T>
113 static constexpr void *raw_pointer(T *ptr) {
114 return reinterpret_cast<
void *>(ptr);
120 static void CheckLayout() {
121 static_assert(std::is_trivially_copy_assignable<Extent>::value,
122 "Extent must satisfy std::is_trivially_copy_assignable");
123 static_assert(std::is_standard_layout<Extent>::value,
124 "Extent must satisfy std::is_standard_layout");
125 static_assert(
sizeof(size_t) == 8,
"Unexpected size for type size_t");
126 static_assert(offsetof(Extent, data_) == 0x0,
127 "Unexpected layout for field Extent::data_");
128 static_assert(offsetof(Extent, size_) ==
sizeof(uint64_t),
129 "Unexpected layout for field Extent::size_");
size_t size() const
Definition: extent.h:67
constexpr Extent(T *data)
Initializes an extent with a pointer to a value.
Definition: extent.h:52
T * As()
A size-aware reinterpret_cast for a mutable pointer.
Definition: extent.h:93
void CopyTo(char *out) const
Copies the contents of the extent to out.
Definition: extent.h:84
ABSL_CONST_INIT const char kStatusMoveAssignmentMsg[]
const T * As() const
A size-aware reinterpret_cast for a constant pointer.
Definition: extent.h:102
constexpr Extent()
Initializes an empty extent.
Definition: extent.h:37
constexpr Extent(T *data, size_t count)
Initializes an extent with a pointer to an array of count objects of type T.
Definition: extent.h:63
const void * data() const
Definition: extent.h:73
constexpr Extent(void *data, size_t size)
Initializes an extent with a void pointer.
Definition: extent.h:43
void * data()
Definition: extent.h:70
A extent object suitable for sharing address ranges between trusted and untrusted code...
Definition: extent.h:34
bool empty() const
A predicate for whether the extent is empty.
Definition: extent.h:78