Manages memory buffers with stable reference counting, optimized for shared library contexts.
More...
|
| stable_reference_buffer () |
|
| stable_reference_buffer (const std::size_t size, const size_t size_of_type=1) |
| Create a managed memory block with optional element size.
|
|
| stable_reference_buffer (const buffer &base) |
|
| ~stable_reference_buffer () noexcept override |
| deallocate the managed memory block, in case this instance olds the last reference to it. Otherwise, do nothing.
|
|
| stable_reference_buffer (const void *address) |
| create a managed memory block from address. If this address is a known address, it only increases the reference count
|
|
| stable_reference_buffer (const stable_reference_buffer &other) |
| copy construction means that the copied instance increases the reference count of the managed memory address
|
|
void | append (const void *bufferToAppend, const size_t lengthOfBuffer) override |
| append the given buffer to the end of the current buffer. If there is no current buffer yet, it will be allocated. Memory will be copied if required, otherwise expanded.
|
|
void * | safe_get () const noexcept |
| Provides safe access to the raw pointer of the managed memory buffer.
|
|
stable_reference_buffer & | operator= (const stable_reference_buffer &other) |
| copy assignment means that the copied instance increases the reference count of the managed memory address
|
|
stable_reference_buffer & | operator= (const buffer &other) override |
| make a deep copy of the other buffer, overwriting the content of this buffer
|
|
size_t | use_count () const |
| Get the use count of the managed memory block.
|
|
void | reset () noexcept override |
| Resets the shared_buffer instance, potentially deallocating the managed memory block.
|
|
void | swap (stable_reference_buffer &other) |
| Swaps the contents of this shared_buffer with another shared_buffer.
|
|
virtual void | swap (buffer &other) noexcept |
| Swaps the contents of this shared_buffer with another shared_buffer.
|
|
| buffer ()=default |
| Will not create any memory block. Use append to create one or append bytes to an existing one.
|
|
| buffer (const std::size_t size, const std::size_t size_of_type=1) |
| Create a managed memory block with optional element size.
|
|
virtual | ~buffer () noexcept |
| Deallocate the memory block.
|
|
| buffer (const void *address, const std::size_t length_of_buffer) |
| Create an instance from a given memory block from address.
|
|
| buffer (const buffer &other) |
| Copy construction means that the other buffer is deep copied to construct this instance.
|
|
virtual void | append (const void *buffer_to_append, const std::size_t length_of_buffer) |
| append the given buffer to the end of the current buffer. If there is no current buffer yet, it will be allocated. Memory will be copied if required, otherwise expanded.
|
|
virtual std::size_t | size () const noexcept |
| returns the size of the buffer in bytes
|
|
virtual void * | get () const noexcept |
| return a pointer to the managed memory block
|
|
Manages memory buffers with stable reference counting, optimized for shared library contexts.
Extends dynamic memory buffer functionality with a robust reference counting mechanism, essential in multi-library environments. The cbeam::stable_interprocess_map
ensures a consistent and unique count for each buffer instance, even across different shared libraries. This approach solves the common problem of duplicated reference counts and ensures memory buffer management remains reliable and predictable in varied compiler environments. The interprocess memory is exclusively for reference counting, while the buffer itself uses standard memory.
- Reference Counting in Shared Libraries: Utilizes interprocess memory for a consistent reference count, separate from the buffer's standard memory.
- Memory Management: Employs efficient memory resizing techniques using malloc and free, with uint8_t as the internal data type.
Usage
Suitable for complex memory management scenarios involving multiple shared libraries within a single process.
void * cbeam::container::stable_reference_buffer::safe_get |
( |
| ) |
const |
|
inlinenoexcept |
Provides safe access to the raw pointer of the managed memory buffer.
This method is a convenience function designed for typical use cases where the safety of accessing the raw pointer depends on the lifecycle management of the stable_reference_buffer
instance. It checks the reference count of the buffer and returns a raw pointer only if the buffer is not the sole reference. This ensures safety in contexts where instances of stable_reference_buffer::delay_deallocation
are used to manage memory lifecycles and avoid premature deallocation.
The method is safe under the condition that the raw pointer is not used outside the scope of the delay_deallocation
instance. The direct use of get()
can be safe as well, provided the caller guarantees that the stable_reference_buffer
instance is not destructed while the raw pointer is in use. Conversely, safe_get()
can be unsafe if the raw pointer is passed beyond the scope of delay_deallocation
.
Best Practices
- Returns
- void* A raw pointer to the memory buffer if safe, otherwise
nullptr
.
- Note
- The safety of using the returned raw pointer depends on the scope of
delay_deallocation
.