Quick Links:

bal | bbl | bdl | bsl

Public Types | Public Member Functions | Static Public Member Functions

bslma::BufferAllocator Class Reference

#include <bslma_bufferallocator.h>

Inheritance diagram for bslma::BufferAllocator:
bslma::Allocator

List of all members.

Public Types

enum  AlignmentStrategy { MAXIMUM_ALIGNMENT, NATURAL_ALIGNMENT }
 

Types of alignment strategy to allocate memory.

More...
typedef void *(* AllocCallback )(int)
typedef bsls::Types::size_type size_type

Public Member Functions

 BufferAllocator (char *buffer, size_type bufSize, AllocCallback allocCallback=0)
 BufferAllocator (char *buffer, size_type bufSize, AlignmentStrategy strategy, AllocCallback allocCallback=0)
 ~BufferAllocator ()
void * allocate (size_type size)
void deallocate (void *address)
void print () const
template<class TYPE >
void deleteObject (const TYPE *object)
void deleteObject (bsl::nullptr_t)
template<class TYPE >
void deleteObjectRaw (const TYPE *object)
void deleteObjectRaw (bsl::nullptr_t)

Static Public Member Functions

static void * allocateFromBuffer (int *cursor, char *buffer, size_type bufSize, size_type size, AlignmentStrategy strategy)
static void * allocateFromBuffer (int *cursor, char *buffer, size_type bufSize, size_type size, int alignment)
static void throwBadAlloc ()

Detailed Description

This class provides a concrete buffer allocator that implements the Allocator interface, and allocates memory blocks from a fixed-size buffer that is supplied by the user at construction. The allocator can supply memory that can be maximally (default) or naturally aligned.

See Component bslma_bufferallocator


Member Typedef Documentation

AllocCallback is an alias for (the address of) a function that takes an integer argument and returns void *.


Member Enumeration Documentation

Enumerator:
MAXIMUM_ALIGNMENT 

Default. Return memory on maximally-aligned boundary bsls::AlignmentUtil::BSLS_MAX_ALIGNMENT (as required for ANSI standard allocators).

NATURAL_ALIGNMENT 

Return memory aligned on a boundary that is the largest power of two dividing the requested size (up to bsls::AlignmentUtil::BSLS_MAX_ALIGNMENT).


Constructor & Destructor Documentation

bslma::BufferAllocator::BufferAllocator ( char *  buffer,
size_type  bufSize,
AllocCallback  allocCallback = 0 
)

Create a buffer allocator for allocating memory blocks from the specified buffer of the specified bufSize. Optionally specify a callback function allocCallback that is invoked when a call to allocate cannot be fulfilled from buffer. Note that maximum alignment is used when allocating memory.

bslma::BufferAllocator::BufferAllocator ( char *  buffer,
size_type  bufSize,
AlignmentStrategy  strategy,
AllocCallback  allocCallback = 0 
)

Create a buffer allocator for allocating memory blocks from the specified buffer of the specified bufSize. Use the specified alignment strategy to allocate memory. Optionally specify a callback function allocCallback that is invoked when a call to allocate cannot be fulfilled from buffer.

bslma::BufferAllocator::~BufferAllocator (  ) 

Destroy this buffer allocator.


Member Function Documentation

static void* bslma::BufferAllocator::allocateFromBuffer ( int *  cursor,
char *  buffer,
size_type  bufSize,
size_type  size,
AlignmentStrategy  strategy 
) [static]
static void* bslma::BufferAllocator::allocateFromBuffer ( int *  cursor,
char *  buffer,
size_type  bufSize,
size_type  size,
int  alignment 
) [static]

Allocate a memory block of the specified size from the specified buffer at the specified cursor position. Return the address of the allocated memory block if buffer contains enough available memory, and null otherwise. The cursor is set to the index of the next available byte in buffer after the allocation. Use the specified alignment strategy (or alignment value) to allocate memory. If size is 0, do not allocate memory and return zero. The behavior is undefined unless 0 < = bufSize, 0 <= size, 0 < alignment <= bsls::AlignmentUtil::BSLS_MAX_ALIGNMENT, and alignment is an integral power of 2.

void* bslma::BufferAllocator::allocate ( size_type  size  )  [virtual]

Return a newly allocated block of memory of (at least) the specified positive size (in bytes). If size is 0, a null pointer is returned with no other effect. If this allocator cannot return the requested number of bytes, then it will return the result of invoking an optional callback function (supplied at construction); if no callback function was specified, throw a std::bad_alloc exception in an exception-enabled build, or else abort the program in a non-exception build. The behavior is undefined unless 0 <= size. Note that the alignment of the address returned depends on the alignment strategy (MAXIMUM_ALIGNMENT or NATURAL_ALIGNMENT) specified at construction of this buffer allocator. Also note that it is up to the callback function whether to return zero or throw an exception if this buffer allocator is unable to satisfy the request.

Implements bslma::Allocator.

void bslma::BufferAllocator::deallocate ( void *  address  )  [virtual]

This function has no effect for this buffer allocator.

Implements bslma::Allocator.

void bslma::BufferAllocator::print (  )  const

Write formatted debugging information to stdout describing the internal state of this buffer allocator.

static void bslma::Allocator::throwBadAlloc (  )  [static, inherited]

DEPRECATED: Use bsls::BslExceptionUtil::throwBadAlloc instead.

Throw std::bad_alloc if exceptions are enabled or abort the program otherwise. Derived classes and helper functions will typically call this function when they are unable to satisfy an allocation request. This function never returns.

template<class TYPE >
void bslma::Allocator::deleteObject ( const TYPE *  object  )  [inherited]

Destroy the specified object based on its dynamic type and then use this allocator to deallocate its memory footprint. Do nothing if object is a null pointer. The behavior is undefined unless object, when cast appropriately to void *, was allocated using this allocator and has not already been deallocated. Note that dynamic_cast<void *>(object) is applied if TYPE is polymorphic, and static_cast<void *>(object) is applied otherwise.

void bslma::Allocator::deleteObject ( bsl::nullptr_t   )  [inherited]

This function has no effect. Note that it exists to support calling deleteObject will a null pointer literal, that would otherwise not deduce to a pointer type for the method above. As calls to deleteObject with (typed) null pointer values have well-defined behavior, it should also support calls with a null pointer literal.

template<class TYPE >
void bslma::Allocator::deleteObjectRaw ( const TYPE *  object  )  [inherited]

Destroy the specified object and then use this allocator to deallocate its memory footprint. Do nothing if object is a null pointer. The behavior is undefined unless object was allocated using this allocator, is not a secondary base class pointer -- i.e., the address is (numerically) the same as when it was originally dispensed by this allocator, and has not already been deallocated.

void bslma::Allocator::deleteObjectRaw ( bsl::nullptr_t   )  [inherited]

This function has no effect. Note that it exists to support calling deleteObjectRaw will a null pointer literal, that would otherwise not deduce to a pointer type for the method above. As calls to deleteObjectRaw with (typed) null pointer values have well-defined behavior, it should also support calls with a null pointer literal.


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