|
| MemoryPool (const std::string &) noexcept |
| Constructor. More...
|
|
| ~MemoryPool () noexcept |
| Destructor. More...
|
|
uint8_t * | addressOf (off_t) const noexcept |
| Convert an offset into a pointer. More...
|
|
off_t | alloc (const size_t) noexcept |
| Allocate memory from the pool. More...
|
|
off_t | alloc (const size_t, std::error_code &) noexcept |
| Allocate memory from the pool. More...
|
|
off_t | alloc (size_t, uint32_t) noexcept |
| Allocate memory from the pool. More...
|
|
off_t | alloc (size_t, uint32_t, std::error_code &) noexcept |
| Allocate memory from the pool. More...
|
|
off_t | alloc (size_t, uint8_t) noexcept |
| Allocate memory from the pool. More...
|
|
off_t | alloc (size_t, uint8_t, std::error_code &) noexcept |
| Allocate memory from the pool. More...
|
|
std::string | dump (size_t, size_t) const noexcept |
| Output the internal state. More...
|
|
int | fd () const noexcept |
| The backing file descriptor. More...
|
|
void | free (off_t &) noexcept |
| Free allocated memory. More...
|
|
std::error_code | init (const size_t, const bool=false, const MemoryPool::Alignment=MemoryPool::Alignment::Bits_64) noexcept |
| Initialize the MemoryPool. More...
|
|
void | onRemap (MemoryPool::LambdaAddressMap) noexcept |
| Set the Remap Event callback. More...
|
|
off_t | realloc (off_t, size_t) noexcept |
| Change the size of allocated memory. More...
|
|
off_t | realloc (off_t, size_t, std::error_code &) noexcept |
| Change the size of allocated memory. More...
|
|
VectorSegment | segmentList () const noexcept |
| Information about the current internal state. More...
|
|
size_t | size () const noexcept |
| The size of the memory pool. More...
|
|
void | sizeOnChange (MemoryPool::LambdaSize) noexcept |
| Set the Size Event callback. More...
|
|
Refer to Zakero_MemoryPool.h to learn how to include this library.
This object will create a region of memory and provide an interface to allocate from that memory.
std::error_code zakero::MemoryPool::init |
( |
const size_t |
size, |
|
|
const bool |
expandable = false , |
|
|
const MemoryPool::Alignment |
alignment = MemoryPool::Alignment::Bits_64 |
|
) |
| |
|
noexcept |
The MemoryPool must be initialized before it can be used. At a minimum, the size of the MemoryPool must be specified in bytes and the size must be greater than 0.
The maximum allowable size is MemoryPool::Size_Max which represents the largest offset value supported by the MemoryPool. Your hardware configuration and/or operating system may lower this limit.
Setting the expandable
flag to true
will allow the MemoryPool to grow to a larger size. If an allocation request is made that is larger that the largest available contiguous space available, then the Memory Pool will expand just enough to accommodate the request.
The Byte Boundary of all the allocations for this MemoryPool object is specified by the alignment
.
- Note
- The size of the MemoryPool will never shrink.
- Example
#define KILOBYETS(size_) ((size_) * 1024)
#define MEGABYTES(size_) (KILOBYTES(size_) * 1024)
rgba_textures.init(MEGABYTES(32)
, false
, zakero::MemoryPool::Alignment::Bits_32
);
- Returns
- An error condition. If there was no error, then the value of the error condition will be
0
.
- Parameters
-
size | The initial size in bytes |
expandable | Allow the MemoryPool to expand |
alignment | The Byte Alignment |
There are times when the MemoryPool will move allocated data. When this happens, the provided lambda
will be called so that the caller will have an opportunity to update their pointers.
The lambda will receive a map of addresses where the key is the old address and the value is the new address.
If the MemoryPool was configured to be not expandable (see init()), then the MemoryPool will never have a need to move it's region of memory. Therefore, the lambda
will never be called and the pointers will never become invalid (unless the memory is freed).
- Example
uint8_t* secret = nullptr;
{
});
off_t secret_offset = memory_pool.alloc(512);
secret = memory_pool.addressOf(secret_offset);
std::map< uint8_t *, uint8_t * > AddressMap
A mapping of old addresses to new addresses.
Definition: Zakero_MemoryPool.h:299
static uint8_t * remap(const MemoryPool::AddressMap &, uint8_t *) noexcept
Get the new memory address.
Definition: Zakero_MemoryPool.h:1371
- Note
- The MemoryPool will be in a "locked state" so any call from the lambda to a non-const MemoryPool method will block indefinitely.
- See also
- remap()
- Parameters
-
off_t zakero::MemoryPool::realloc |
( |
off_t |
offset, |
|
|
size_t |
size |
|
) |
| |
|
noexcept |
This method is similar to std::realloc(), in that it will resize the allocated memory at the given offset
. If the resize was successful, the new offset will be returned.
The contents of the memory will be preserved.
The return value will be -1
if the allocated memory could not be resized.
- Example
memory_pool.init(128);
off_t offset = memory_pool.alloc(64);
offset = memory_pool.resize(offset, 96);
- Returns
- The offset of the resized memory location.
- Parameters
-
offset | The memory to resize |
size | The size in bytes |
off_t zakero::MemoryPool::realloc |
( |
off_t |
offset, |
|
|
size_t |
size, |
|
|
std::error_code & |
error |
|
) |
| |
|
noexcept |
This method is similar to std::realloc(), in that it will resize the allocated memory at the given offset
. If the resize was successful, the new offset will be returned.
The contents of the memory will be preserved.
The return value will be -1
if the allocated memory could not be resized and the reason will be stored in error
.
- Example
memory_pool.init(128);
off_t offset = memory_pool.alloc(64);
std::error_code error;
auto new_offset = memory_pool.resize(offset, 96, error);
if(error(bool) == true)
{
std::cerr << "Error: " << error.message() << "\n";
}
else
{
offset = new_offset;
}
- Returns
- The offset of the resized memory location.
- Parameters
-
offset | The memory to resize |
size | The size in bytes |
error | The error |