BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdlma::BufferManager Class Reference

#include <bdlma_buffermanager.h>

Public Member Functions

 BufferManager (bsls::Alignment::Strategy strategy=bsls::Alignment::BSLS_NATURAL)
 
 BufferManager (char *buffer, bsls::Types::size_type bufferSize, bsls::Alignment::Strategy strategy=bsls::Alignment::BSLS_NATURAL)
 
 ~BufferManager ()
 Destroy this buffer manager.
 
void * allocate (bsls::Types::size_type size)
 
void * allocateRaw (bsls::Types::size_type size)
 
template<class TYPE >
void deleteObjectRaw (const TYPE *object)
 
template<class TYPE >
void deleteObject (const TYPE *object)
 
bsls::Types::size_type expand (void *address, bsls::Types::size_type size)
 
char * replaceBuffer (char *newBuffer, bsls::Types::size_type newBufferSize)
 
void release ()
 
void reset ()
 
bsls::Types::size_type truncate (void *address, bsls::Types::size_type originalSize, bsls::Types::size_type newSize)
 
bsls::Alignment::Strategy alignmentStrategy () const
 
char * buffer () const
 
bsls::Types::size_type bufferSize () const
 
int calculateAlignmentOffsetFromSize (const void *address, bsls::Types::size_type size) const
 
bool hasSufficientCapacity (bsls::Types::size_type size) const
 

Detailed Description

This class implements a buffer manager that dispenses heterogeneous blocks of memory (of varying, user-specified sizes) from an external buffer whose address and size are optionally supplied at construction. If an allocation request exceeds the remaining free memory space in the external buffer, the allocation request returns 0 if allocate is used, or results in undefined behavior if allocateRaw is used. Note that in no event will the buffer manager attempt to deallocate the external buffer.

See bdlma_buffermanager

Constructor & Destructor Documentation

◆ BufferManager() [1/2]

bdlma::BufferManager::BufferManager ( bsls::Alignment::Strategy  strategy = bsls::Alignment::BSLS_NATURAL)
inlineexplicit

Create a buffer manager for allocating memory blocks. Optionally specify an alignment strategy used to align allocated memory blocks. If strategy is not specified, natural alignment is used. A default constructed buffer manager is unable to allocate any memory until an external buffer is provided by calling the replaceBuffer method.

◆ BufferManager() [2/2]

bdlma::BufferManager::BufferManager ( char *  buffer,
bsls::Types::size_type  bufferSize,
bsls::Alignment::Strategy  strategy = bsls::Alignment::BSLS_NATURAL 
)
inline

Create a buffer manager for allocating memory blocks from the specified external buffer having the specified bufferSize (in bytes). Optionally specify an alignment strategy used to align allocated memory blocks. If strategy is not specified, natural alignment is used. The behavior is undefined unless 0 < bufferSize and buffer has at least bufferSize bytes.

◆ ~BufferManager()

bdlma::BufferManager::~BufferManager ( )
inline

Member Function Documentation

◆ alignmentStrategy()

bsls::Alignment::Strategy bdlma::BufferManager::alignmentStrategy ( ) const
inline

Return the alignment strategy passed to this object at construction.

◆ allocate()

void * bdlma::BufferManager::allocate ( bsls::Types::size_type  size)
inline

Return the address of a contiguous block of memory of the specified size (in bytes) on success, according to the alignment strategy specified at construction. If size is 0 or the allocation request exceeds the remaining free memory space in the external buffer, no memory is allocated and 0 is returned.

◆ allocateRaw()

void * bdlma::BufferManager::allocateRaw ( bsls::Types::size_type  size)
inline

Return the address of a contiguous block of memory of the specified size (in bytes) according to the alignment strategy specified at construction. The behavior is undefined unless the allocation request does not exceed the remaining free memory space in the external buffer, 0 < size, and this object is currently managing a buffer.

◆ buffer()

char * bdlma::BufferManager::buffer ( ) const
inline

Return an address providing modifiable access to the buffer currently managed by this object, or 0 if this object currently manages no buffer.

◆ bufferSize()

bsls::Types::size_type bdlma::BufferManager::bufferSize ( ) const
inline

Return the size (in bytes) of the buffer currently managed by this object, or 0 if this object currently manages no buffer.

◆ calculateAlignmentOffsetFromSize()

int bdlma::BufferManager::calculateAlignmentOffsetFromSize ( const void *  address,
bsls::Types::size_type  size 
) const
inline

Return the minimum non-negative integer that, when added to the numerical value of the specified address, yields the alignment as per the alignmentStrategy provided at construction for an allocation of the specified size. Note that if 0 == size and natural alignment was provided at construction, the result of this method is identical to the result for 0 == size and maximal alignment.

◆ deleteObject()

template<class TYPE >
void bdlma::BufferManager::deleteObject ( const TYPE *  object)
inline

Destroy the specified object. Note that this method has the same effect as the deleteObjectRaw method (since no deallocation is involved), and exists for consistency with a pool interface.

◆ deleteObjectRaw()

template<class TYPE >
void bdlma::BufferManager::deleteObjectRaw ( const TYPE *  object)
inline

Destroy the specified object. Note that memory associated with object is not deallocated because there is no deallocate method in BufferManager.

◆ expand()

bsls::Types::size_type bdlma::BufferManager::expand ( void *  address,
bsls::Types::size_type  size 
)

Increase the amount of memory allocated at the specified address from the original size (in bytes) to also include the maximum amount remaining in the buffer. Return the amount of memory available at address after expanding, or size if the memory at address cannot be expanded. This method can only expand the memory block returned by the most recent allocate or allocateRaw request from this buffer manager, and otherwise has no effect. The behavior is undefined unless the memory at address was originally allocated by this buffer manager, the size of the memory at address is size, and release was not called after allocating the memory at address.

◆ hasSufficientCapacity()

bool bdlma::BufferManager::hasSufficientCapacity ( bsls::Types::size_type  size) const
inline

Return true if there is sufficient memory space in the buffer to allocate a contiguous memory block of the specified size (in bytes) after taking the alignment strategy into consideration, and false otherwise. The behavior is undefined unless 0 < size, and this object is currently managing a buffer.

◆ release()

void bdlma::BufferManager::release ( )
inline

Release all memory currently allocated through this buffer manager. After this call, the external buffer managed by this object is retained. Subsequent allocations will allocate memory from the beginning of the external buffer (if any).

◆ replaceBuffer()

char * bdlma::BufferManager::replaceBuffer ( char *  newBuffer,
bsls::Types::size_type  newBufferSize 
)
inline

Replace the buffer currently managed by this object with the specified newBuffer of the specified newBufferSize (in bytes); return the address of the previously held buffer, or 0 if this object currently manages no buffer. The replaced buffer (if any) is removed from the management of this object with no effect on the outstanding allocated memory blocks. Subsequent allocations will allocate memory from the beginning of the new external buffer. The behavior is undefined unless 0 < newBufferSize and newBuffer has at least newBufferSize bytes.

◆ reset()

void bdlma::BufferManager::reset ( )
inline

Reset this buffer manager to its default constructed state, except retain the alignment strategy in effect at the time of construction. The currently managed buffer (if any) is removed from the management of this object with no effect on the outstanding allocated memory blocks.

◆ truncate()

bsls::Types::size_type bdlma::BufferManager::truncate ( void *  address,
bsls::Types::size_type  originalSize,
bsls::Types::size_type  newSize 
)

Reduce the amount of memory allocated at the specified address of the specified originalSize (in bytes) to the specified newSize (in bytes). Return newSize after truncating, or originalSize if the memory at address cannot be truncated. This method can only truncate the memory block returned by the most recent allocate or allocateRaw request from this object, and otherwise has no effect. The behavior is undefined unless the memory at address was originally allocated by this buffer manager, the size of the memory at address is originalSize, newSize <= originalSize, 0 <= newSize, and release was not called after allocating the memory at address.


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