Cbeam
Loading...
Searching...
No Matches
cbeam::container::stable_reference_buffer Class Reference

Manages memory buffers with stable reference counting, optimized for shared library contexts. More...

#include <stable_reference_buffer.hpp>

Inheritance diagram for cbeam::container::stable_reference_buffer:
Collaboration diagram for cbeam::container::stable_reference_buffer:

Classes

class  delay_deallocation
 Manages delayed deallocation of memory blocks in shared library contexts. More...
 

Public Member Functions

 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_bufferoperator= (const stable_reference_buffer &other)
 copy assignment means that the copied instance increases the reference count of the managed memory address
 
stable_reference_bufferoperator= (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.
 
- Public Member Functions inherited from cbeam::container::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
 

Static Public Member Functions

static bool is_known (const void *address)
 return if the given address is one of the managed addresses
 

Additional Inherited Members

- Protected Attributes inherited from cbeam::container::buffer
std::size_t _size {0}
 
uint8_t * _buffer {nullptr}
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ stable_reference_buffer() [1/5]

cbeam::container::stable_reference_buffer::stable_reference_buffer ( )
inline

◆ stable_reference_buffer() [2/5]

cbeam::container::stable_reference_buffer::stable_reference_buffer ( const std::size_t size,
const size_t size_of_type = 1 )
inline

Create a managed memory block with optional element size.

Parameters
sizeNumber of elements to allocate. Defaults to allocating single bytes if size_of_type is not specified.
size_of_type(Optional) Byte size of each element to allocate. Defaults to 1 if not specified, allowing for byte-sized handling similar to std::memset.

◆ stable_reference_buffer() [3/5]

cbeam::container::stable_reference_buffer::stable_reference_buffer ( const buffer & base)
inline

◆ ~stable_reference_buffer()

cbeam::container::stable_reference_buffer::~stable_reference_buffer ( )
inlineoverridenoexcept

deallocate the managed memory block, in case this instance olds the last reference to it. Otherwise, do nothing.

◆ stable_reference_buffer() [4/5]

cbeam::container::stable_reference_buffer::stable_reference_buffer ( const void * address)
inlineexplicit

create a managed memory block from address. If this address is a known address, it only increases the reference count

Different from std::shared_ptr<T>(T*), because it checks if pointer is managed by another instance. Note that size() will be zero for instances the are constructed in this way. Appending to such an instance will throw.

◆ stable_reference_buffer() [5/5]

cbeam::container::stable_reference_buffer::stable_reference_buffer ( const stable_reference_buffer & other)
inline

copy construction means that the copied instance increases the reference count of the managed memory address

Member Function Documentation

◆ append()

void cbeam::container::stable_reference_buffer::append ( const void * bufferToAppend,
const size_t lengthOfBuffer )
inlineoverride

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.

◆ is_known()

static bool cbeam::container::stable_reference_buffer::is_known ( const void * address)
inlinestatic

return if the given address is one of the managed addresses

◆ operator=() [1/2]

stable_reference_buffer & cbeam::container::stable_reference_buffer::operator= ( const buffer & other)
inlineoverridevirtual

make a deep copy of the other buffer, overwriting the content of this buffer

Reimplemented from cbeam::container::buffer.

◆ operator=() [2/2]

stable_reference_buffer & cbeam::container::stable_reference_buffer::operator= ( const stable_reference_buffer & other)
inline

copy assignment means that the copied instance increases the reference count of the managed memory address

◆ reset()

void cbeam::container::stable_reference_buffer::reset ( )
inlineoverridevirtualnoexcept

Resets the shared_buffer instance, potentially deallocating the managed memory block.

Reimplemented from cbeam::container::buffer.

◆ safe_get()

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.

◆ swap() [1/2]

virtual void cbeam::container::buffer::swap ( buffer & other)
inlinevirtualnoexcept

Swaps the contents of this shared_buffer with another shared_buffer.

Parameters
otherAnother shared_buffer to swap with

Reimplemented from cbeam::container::buffer.

◆ swap() [2/2]

void cbeam::container::stable_reference_buffer::swap ( stable_reference_buffer & other)
inline

Swaps the contents of this shared_buffer with another shared_buffer.

Parameters
otherAnother shared_buffer to swap with

◆ use_count()

size_t cbeam::container::stable_reference_buffer::use_count ( ) const
inline

Get the use count of the managed memory block.

Returns
Number of shared_buffer instances managing this block

The documentation for this class was generated from the following file: