libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
buffer.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <cassert>
12#include <promeki/core/list.h>
15#include <promeki/core/error.h>
16
18
85class Buffer {
87 public:
90
95 static size_t getPageSize();
96
98 static const size_t DefaultAlign;
99
101 using List = promeki::List<Buffer>;
102
105
107 Buffer() = default;
108
115 Buffer(size_t sz, size_t an = DefaultAlign, const MemSpace &ms = MemSpace::Default) :
116 _alloc(ms.alloc(sz, an))
117 {
118 _data = _alloc.ptr;
119 }
120
129 Buffer(void *p, size_t sz, size_t an = 0, bool own = false, const MemSpace &ms = MemSpace::Default) :
130 _data(p), _owned(own)
131 {
132 _alloc.ptr = p;
133 _alloc.size = sz;
134 _alloc.align = an;
135 _alloc.ms = ms;
136 }
137
144 Buffer(const Buffer &o) : _owned(true), _size(o._size) {
145 if(o._data != nullptr) {
146 _alloc = o._alloc.ms.alloc(o._alloc.size, o._alloc.align);
147 if(_alloc.ptr == nullptr) { _size = 0; return; }
148 o._alloc.ms.copy(o._alloc, _alloc, _alloc.size);
149 size_t shift = static_cast<uint8_t *>(o._data) - static_cast<uint8_t *>(o._alloc.ptr);
150 _data = static_cast<uint8_t *>(_alloc.ptr) + shift;
151 }
152 }
153
156 if(this == &o) return *this;
157 if(_owned) _alloc.ms.release(_alloc);
158 _data = nullptr;
159 _owned = true;
160 _size = 0;
161 _alloc = {};
162 if(o._data != nullptr) {
163 _alloc = o._alloc.ms.alloc(o._alloc.size, o._alloc.align);
164 if(_alloc.ptr != nullptr) {
165 o._alloc.ms.copy(o._alloc, _alloc, _alloc.size);
166 size_t shift = static_cast<uint8_t *>(o._data) - static_cast<uint8_t *>(o._alloc.ptr);
167 _data = static_cast<uint8_t *>(_alloc.ptr) + shift;
168 _size = o._size;
169 }
170 }
171 return *this;
172 }
173
176 _alloc(o._alloc), _data(o._data), _owned(o._owned), _size(o._size)
177 {
178 o._alloc = {};
179 o._data = nullptr;
180 o._owned = false;
181 o._size = 0;
182 }
183
185 Buffer &operator=(Buffer &&o) noexcept {
186 if(this == &o) return *this;
187 if(_owned) _alloc.ms.release(_alloc);
188 _alloc = o._alloc;
189 _data = o._data;
190 _owned = o._owned;
191 _size = o._size;
192 o._alloc = {};
193 o._data = nullptr;
194 o._owned = false;
195 o._size = 0;
196 return *this;
197 }
198
201 if(_owned) _alloc.ms.release(_alloc);
202 }
203
208 bool isValid() const { return _data != nullptr; }
209
214 void *data() const { return _data; }
215
225 void *odata() const { return _alloc.ptr; }
226
237 size_t size() const { return _size; }
238
247 void setSize(size_t s) const {
248 assert(s <= availSize());
249 _size = s;
250 return;
251 }
252
261 size_t availSize() const {
262 if(_data == nullptr) return 0;
263 return _alloc.size - static_cast<size_t>(
264 static_cast<uint8_t *>(_data) -
265 static_cast<uint8_t *>(_alloc.ptr)
266 );
267 }
268
276 size_t allocSize() const { return _alloc.size; }
277
282 size_t align() const { return _alloc.align; }
283
288 const MemSpace &memSpace() const { return _alloc.ms; }
289
294 const MemAllocation &allocation() const { return _alloc; }
295
304 void shiftData(size_t bytes) {
305 assert(bytes <= _alloc.size);
306 _data = static_cast<uint8_t *>(_alloc.ptr) + bytes;
307 _size = 0;
308 return;
309 }
310
315 bool isHostAccessible() const { return _alloc.ms.isHostAccessible(_alloc); }
316
322 _owned = val;
323 return;
324 }
325
331 Error fill(char value) const {
332 return _alloc.ms.fill(_data, availSize(), value);
333 }
334
348 Error copyFrom(const void *src, size_t bytes, size_t offset = 0) const;
349
350 private:
351 MemAllocation _alloc;
352 void *_data = nullptr;
353 bool _owned = true;
354 mutable size_t _size = 0;
355};
356
Generic memory buffer descriptor with alignment and memory space support.
Definition buffer.h:85
Error fill(char value) const
Fills the entire buffer with the given byte value.
Definition buffer.h:331
Buffer(void *p, size_t sz, size_t an=0, bool own=false, const MemSpace &ms=MemSpace::Default)
Wraps an existing memory pointer as a buffer.
Definition buffer.h:129
size_t size() const
Returns the logical content size in bytes.
Definition buffer.h:237
Buffer(Buffer &&o) noexcept
Move constructor. Transfers ownership from the source buffer.
Definition buffer.h:175
void shiftData(size_t bytes)
Shifts the data pointer forward from the allocation base.
Definition buffer.h:304
void setOwnershipEnabled(bool val)
Enables or disables ownership of the underlying memory.
Definition buffer.h:321
static size_t getPageSize()
Returns the system memory page size in bytes.
Buffer & operator=(Buffer &&o) noexcept
Move assignment operator. Transfers ownership from the source buffer.
Definition buffer.h:185
bool isValid() const
Returns true if the buffer has been allocated or assigned memory.
Definition buffer.h:208
void * odata() const
Returns the original allocation pointer.
Definition buffer.h:225
Buffer(const Buffer &o)
Copy constructor.
Definition buffer.h:144
~Buffer()
Destructor. Releases owned memory.
Definition buffer.h:200
void * data() const
Returns a pointer to the buffer data.
Definition buffer.h:214
size_t align() const
Returns the alignment of the buffer in bytes.
Definition buffer.h:282
static const size_t DefaultAlign
Default memory alignment in bytes.
Definition buffer.h:98
size_t availSize() const
Returns the available space from data() to the end of the allocation.
Definition buffer.h:261
void setSize(size_t s) const
Sets the logical content size.
Definition buffer.h:247
Buffer(size_t sz, size_t an=DefaultAlign, const MemSpace &ms=MemSpace::Default)
Allocates a buffer of the given size.
Definition buffer.h:115
Buffer()=default
Constructs an invalid (empty) buffer.
promeki::List< Ptr > PtrList
List of shared Buffer pointers.
Definition buffer.h:104
bool isHostAccessible() const
Returns true if the buffer memory is directly accessible from the host CPU.
Definition buffer.h:315
size_t allocSize() const
Returns the total allocation size in bytes.
Definition buffer.h:276
const MemAllocation & allocation() const
Returns the underlying allocation descriptor.
Definition buffer.h:294
Error copyFrom(const void *src, size_t bytes, size_t offset=0) const
Copies data from an external source into the buffer.
promeki::List< Buffer > List
List of Buffer values.
Definition buffer.h:101
const MemSpace & memSpace() const
Returns the memory space this buffer was allocated from.
Definition buffer.h:288
Buffer & operator=(const Buffer &o)
Copy assignment operator. Performs a deep copy.
Definition buffer.h:155
Lightweight error code wrapper for the promeki library.
Definition error.h:39
Dynamic array container wrapping std::vector.
Definition list.h:40
List()=default
Default constructor. Creates an empty list.
Abstraction for memory allocation in different address spaces.
Definition memspace.h:30
@ Default
Alias for System memory.
Definition memspace.h:36
bool copy(const MemAllocation &src, const MemAllocation &dst, size_t bytes) const
Copies bytes from a source allocation to a destination allocation.
Definition memspace.h:138
MemAllocation alloc(size_t bytes, size_t align) const
Allocates memory in this memory space.
Definition memspace.h:143
Error fill(void *ptr, size_t bytes, char value) const
Fills memory with a byte value.
Definition memspace.h:110
bool isHostAccessible(const MemAllocation &alloc) const
Returns true if the given allocation is directly accessible from the host CPU.
Definition memspace.h:73
void release(MemAllocation &alloc) const
Releases a previously allocated memory region.
Definition memspace.h:152
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19
const T & value(const Result< T > &r)
Returns the value from a Result.
Definition result.h:56
#define PROMEKI_SHARED_FINAL(TYPE)
Macro for non-polymorphic native shared objects.
Definition sharedptr.h:88
Describes a memory allocation from a MemSpace.
Definition memspace.h:127
size_t size
Size of the allocation in bytes.
Definition memspace.h:129
size_t align
Alignment of the allocation in bytes.
Definition memspace.h:130
MemSpace ms
The memory space this was allocated from.
Definition memspace.h:131
void * ptr
Pointer to the allocated memory.
Definition memspace.h:128