8 #ifndef META_OCEAN_BASE_MEMORY_H
9 #define META_OCEAN_BASE_MEMORY_H
42 explicit inline Memory(
const size_t size,
const size_t alignment =
size_t(1));
50 inline Memory(
void* useData,
const size_t size);
58 inline Memory(
const void* useData,
const size_t size);
69 [[nodiscard]]
inline const void*
constdata()
const;
75 [[nodiscard]]
inline void*
data();
84 [[nodiscard]]
inline const T*
constdata(
const bool checkAlignment =
true)
const;
93 [[nodiscard]]
inline T*
data(
const bool checkAlignment =
true);
101 [[nodiscard]]
inline bool isInside(
const void*
const start,
const size_t size)
const;
109 [[nodiscard]]
inline bool isInside(
const void*
const start,
const void*
const end)
const;
120 [[nodiscard]]
inline size_t size()
const;
126 [[nodiscard]]
inline bool isOwner()
const;
138 [[nodiscard]]
inline bool isNull()
const;
144 [[nodiscard]]
explicit inline operator bool()
const;
158 template <
typename T>
159 [[nodiscard]]
static inline Memory create(
const size_t elements);
168 static inline void memcpy(
void* target,
const void* source,
const unsigned int size,
Worker* worker =
nullptr);
177 static inline void memset(
void*
data,
const int value,
const unsigned int size,
Worker* worker =
nullptr);
200 static inline void memcpySubset(uint8_t* target,
const uint8_t* source,
const unsigned int firstByte,
const unsigned int numberBytes);
209 static inline void memsetSubset(uint8_t*
data,
const int value,
const unsigned int firstByte,
const unsigned int numberBytes);
229 *
this = std::move(memory);
235 ocean_assert(alignment >=
size_t(1));
237 if (
size !=
size_t(0))
239 static_assert(
sizeof(uint8_t) == 1,
"Invalid data type!");
246 const size_t alignmentOffset = (alignment - (size_t(
allocatedData_) % alignment)) % alignment;
248 ocean_assert(alignmentOffset < alignment);
249 ocean_assert((
size_t(
allocatedData_) + alignmentOffset) % alignment ==
size_t(0));
265 alignedData_(useData),
268 ocean_assert(useData !=
nullptr);
269 ocean_assert(
size > 0);
280 constAlignedData_(useData),
283 ocean_assert(useData !=
nullptr);
284 ocean_assert(
size > 0);
332 ocean_assert(start !=
nullptr);
344 const uint8_t*
const start_u8 = (
const uint8_t*)(start);
347 return start_u8 >= constAlignedData_u8 && start_u8 +
size <= constAlignedData_u8 +
size_;
352 ocean_assert(start !=
nullptr && end !=
nullptr);
353 ocean_assert(start <= end);
408 inline Memory::operator bool()
const
410 ocean_assert((constAlignedData_ ==
nullptr && size_ ==
size_t(0)) || (constAlignedData_ !=
nullptr && size_ !=
size_t(0)));
412 return constAlignedData_ !=
nullptr;
421 allocatedData_ = memory.allocatedData_;
422 constAlignedData_ = memory.constAlignedData_;
423 alignedData_ = memory.alignedData_;
424 size_ = memory.size_;
426 memory.allocatedData_ =
nullptr;
427 memory.constAlignedData_ =
nullptr;
428 memory.alignedData_ =
nullptr;
429 memory.size_ = size_t(0);
435 template <
typename T>
438 return Memory(
sizeof(T) * elements,
sizeof(T) );
465 inline void Memory::memcpySubset(uint8_t* target,
const uint8_t* source,
const unsigned int firstByte,
const unsigned int numberBytes)
467 ocean_assert(target !=
nullptr);
468 ocean_assert(source !=
nullptr);
470 ::memcpy(target + firstByte, source + firstByte, numberBytes);
473 inline void Memory::memsetSubset(uint8_t* data,
const int value,
const unsigned int firstByte,
const unsigned int numberBytes)
475 ocean_assert(
data !=
nullptr);
static Caller< void > createStatic(typename StaticFunctionPointerMaker< void, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass, NullClass >::Type function)
Creates a new caller container for a static function with no function parameter.
Definition: Caller.h:2876
This class implements an object able to allocate memory.
Definition: base/Memory.h:22
bool isReadOnly() const
Returns whether this object provides read-only memory only.
Definition: base/Memory.h:396
static Memory create(const size_t elements)
Creates a new object and allocates enough memory necessary for 'elements' of type T.
Definition: base/Memory.h:436
bool isOwner() const
Returns whether this object owns the memory.
Definition: base/Memory.h:391
Memory(const Memory &memory)=delete
The disabled copy constructor.
bool isInside(const void *const start, const size_t size) const
Returns whether a specified memory range is entirely enclosed inside the memory managed by this objec...
Definition: base/Memory.h:330
size_t size() const
Returns the size of the memory in bytes.
Definition: base/Memory.h:386
static void memcpySubset(uint8_t *target, const uint8_t *source, const unsigned int firstByte, const unsigned int numberBytes)
Copies a subset of the a memory block.
Definition: base/Memory.h:465
void * allocatedData_
The pointer to the memory which is allocated and owned by this object, this pointer is pointing to th...
Definition: base/Memory.h:214
bool isNull() const
Returns whether this object holds any memory.
Definition: base/Memory.h:401
static void memset(void *data, const int value, const unsigned int size, Worker *worker=nullptr)
Sets the value of a given memory block using a worker object to speed up the process.
Definition: base/Memory.h:453
size_t size_
The size of the actual usable memory in bytes, with range [0, infinity)
Definition: base/Memory.h:223
const void * constAlignedData_
The pointer to the read-only aligned memory which is reported to be the actual memory pointer,...
Definition: base/Memory.h:217
Memory & operator=(Memory &&memory) noexcept
Move operator.
Definition: base/Memory.h:415
Memory()=default
Creates a new object without any allocated memory.
void * data()
Returns the pointer to the writable memory which is allocated by this object.
Definition: base/Memory.h:303
static void memcpy(void *target, const void *source, const unsigned int size, Worker *worker=nullptr)
Copies a block of memory using a worker object to speed up the process.
Definition: base/Memory.h:441
static void memsetSubset(uint8_t *data, const int value, const unsigned int firstByte, const unsigned int numberBytes)
Sets a subset of a given memory block.
Definition: base/Memory.h:473
const void * constdata() const
Returns the pointer to the read-only memory which is allocated by this object.
Definition: base/Memory.h:298
~Memory()
Releases the object and frees the memory if it holds any memory.
Definition: base/Memory.h:293
void * alignedData_
The pointer to the writable aligned memory which is reported to be the actual memory pointer,...
Definition: base/Memory.h:220
void free()
Explicitly frees (releases) the memory before this object is released.
Definition: base/Memory.h:370
Memory & operator=(const Memory &memory)=delete
The disabled assign operator.
This class implements a worker able to distribute function calls over different threads.
Definition: Worker.h:33
bool executeFunction(const Function &function, const unsigned int first, const unsigned int size, const unsigned int firstIndex=(unsigned int)(-1), const unsigned int sizeIndex=(unsigned int)(-1), const unsigned int minimalIterations=1u, const unsigned int threadIndex=(unsigned int)(-1))
Executes a callback function separable by two function parameters.
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15