BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bslma::BufferAllocator Class Reference

#include <bslma_bufferallocator.h>

Inheritance diagram for bslma::BufferAllocator:
bslma::Allocator bsl::memory_resource

Public Types

enum  AlignmentStrategy { MAXIMUM_ALIGNMENT , NATURAL_ALIGNMENT }
 Types of alignment strategy to allocate memory. More...
 
typedef void *(* AllocCallback) (int)
 
- Public Types inherited from bslma::Allocator
typedef std::size_t 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 () BSLS_KEYWORD_OVERRIDE
 Destroy this buffer allocator.
 
void * allocate (size_type size) BSLS_KEYWORD_OVERRIDE
 
void deallocate (void *address) BSLS_KEYWORD_OVERRIDE
 This function has no effect for this buffer allocator.
 
void print () const
 
- Public Member Functions inherited from bslma::Allocator
 ~Allocator () BSLS_KEYWORD_OVERRIDE
 
template<class TYPE >
void deleteObject (const TYPE *object)
 
template<class TYPE >
void deleteObjectRaw (const TYPE *object)
 
void deleteObject (bsl::nullptr_t)
 
void deleteObjectRaw (bsl::nullptr_t)
 
- Public Member Functions inherited from bsl::memory_resource
 memory_resource () BSLS_KEYWORD_DEFAULT
 Create this object. Has no effect other than to begin its lifetime.
 
 memory_resource (const memory_resource &) BSLS_KEYWORD_DEFAULT
 
virtual ~memory_resource ()
 Destroy this object. Has no effect other than to end its lifetime.
 
memory_resourceoperator= (const memory_resource &) BSLS_KEYWORD_DEFAULT
 Return a modifiable reference to this object.
 
BSLS_ANNOTATION_NODISCARD void * allocate (size_t bytes, size_t alignment=k_MAX_ALIGN)
 
void deallocate (void *p, size_t bytes, size_t alignment=k_MAX_ALIGN)
 
bool is_equal (const memory_resource &other) const BSLS_KEYWORD_NOEXCEPT
 

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 Public Member Functions inherited from bslma::Allocator
static void throwBadAlloc ()
 

Additional Inherited Members

- Protected Member Functions inherited from bslma::Allocator
void * do_allocate (std::size_t bytes, std::size_t alignment) BSLS_KEYWORD_OVERRIDE
 
void do_deallocate (void *p, std::size_t bytes, std::size_t alignment) BSLS_KEYWORD_OVERRIDE
 
bool do_is_equal (const memory_resource &other) const BSLS_KEYWORD_NOEXCEPT BSLS_KEYWORD_OVERRIDE
 

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 bslma_bufferallocator

Member Typedef Documentation

◆ AllocCallback

typedef void *(* bslma::BufferAllocator::AllocCallback) (int)

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

Member Enumeration Documentation

◆ AlignmentStrategy

Enumerator
MAXIMUM_ALIGNMENT 
NATURAL_ALIGNMENT 

Constructor & Destructor Documentation

◆ BufferAllocator() [1/2]

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

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.

◆ BufferAllocator() [2/2]

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

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.

◆ ~BufferAllocator()

bslma::BufferAllocator::~BufferAllocator ( )

Member Function Documentation

◆ allocate()

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.

◆ allocateFromBuffer() [1/2]

static void * bslma::BufferAllocator::allocateFromBuffer ( int *  cursor,
char *  buffer,
size_type  bufSize,
size_type  size,
AlignmentStrategy  strategy 
)
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.

◆ allocateFromBuffer() [2/2]

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

◆ deallocate()

void bslma::BufferAllocator::deallocate ( void *  address)
inlinevirtual

Implements bslma::Allocator.

◆ print()

void bslma::BufferAllocator::print ( ) const

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


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