JASSv2
Public Member Functions | Static Public Member Functions | Protected Attributes | Static Protected Attributes | List of all members
JASS::allocator Class Referenceabstract

Virtual base class for C style allocators. More...

#include <allocator.h>

Inheritance diagram for JASS::allocator:
JASS::allocator_memory JASS::allocator_pool

Public Member Functions

 allocator ()
 Constructor.
 
virtual ~allocator ()
 Destructor.
 
virtual bool operator== (const allocator &with)=0
 Compare for equality two objects of this class type. More...
 
virtual bool operator!= (const allocator &with)=0
 Compare for inequlity two objects of this class type. More...
 
virtual void * malloc (size_t bytes, size_t alignment=alignment_boundary)=0
 Allocate a small chunk of memory from the internal pool and return a pointer to the caller. More...
 
size_t capacity (void) const
 Return the amount of memory that this object has allocated to it. More...
 
size_t size (void) const
 Return the number of bytes of memory this object has handed back to callers. More...
 
virtual void rewind (void)=0
 Throw away (without calling delete) all objects allocated in the memory space of this allocator Delete is not called for any objects allocated in this space, the memory is simply re-claimed.
 

Static Public Member Functions

static size_t realign (const void *address, size_t boundary)
 Compute the number of extra bytes of memory necessary for an allocation to start on an aligned boundary. More...
 

Protected Attributes

std::atomic< size_t > used
 The number of bytes this object has passed back to the caller.
 
std::atomic< size_t > allocated
 The number of bytes this object has allocated.
 

Static Protected Attributes

static constexpr size_t alignment_boundary = 1
 Elsewhere don't bother with alignment (align on byte boundaries)
 

Detailed Description

Virtual base class for C style allocators.

All allocators are thread safe. A single allocator can be called from multiple threads concurrently and they will each return a valid pointer to a piece of memory that is not overlapping with any pointer returned from any other call and is of the requested size.

Member Function Documentation

§ capacity()

size_t JASS::allocator::capacity ( void  ) const
inline

Return the amount of memory that this object has allocated to it.

Returns
The number of bytes of memory allocated to this object.

§ malloc()

virtual void* JASS::allocator::malloc ( size_t  bytes,
size_t  alignment = alignment_boundary 
)
pure virtual

Allocate a small chunk of memory from the internal pool and return a pointer to the caller.

Parameters
bytes[in] The size of the chunk of memory.
alignment[in] If a word-aligned piece of memory is needed then this is the word-size (e.g. sizeof(void*))
Returns
A pointer to a block of memory of size bytes, or NULL on failure.

Implemented in JASS::allocator_pool, and JASS::allocator_memory.

§ operator!=()

virtual bool JASS::allocator::operator!= ( const allocator with)
pure virtual

Compare for inequlity two objects of this class type.

Parameters
with[in] The object to compare to.
Returns
True if this != that, else false.

Implemented in JASS::allocator_pool, and JASS::allocator_memory.

§ operator==()

virtual bool JASS::allocator::operator== ( const allocator with)
pure virtual

Compare for equality two objects of this class type.

Parameters
with[in] The object to compare to.
Returns
True if this == that, else false.

Implemented in JASS::allocator_pool, and JASS::allocator_memory.

§ realign()

static size_t JASS::allocator::realign ( const void *  address,
size_t  boundary 
)
inlinestatic

Compute the number of extra bytes of memory necessary for an allocation to start on an aligned boundary.

Aligning all allocations on a machine-word boundary is a space / space trade off. Allocating a string of single bytes one after the other and word-aligned would result in a machine word being used per byte. To avoid this wastage this class, by default, does not word-align any allocations. However, it is sometimes necessary to word-align because some assembly instructions require word-alignment. This method return the number of bytes of padding necessary to make an address word-aligned.

Parameters
address[in] Compute the number of wasted bytes from this address to the next bounday
boundary[in] The byte-boundary to which this address should be alligned (e.g. 4 will ensure the least significant 2 bits are alwasys 00)
Returns
The number of bytes to add to address to make it aligned

§ size()

size_t JASS::allocator::size ( void  ) const
inline

Return the number of bytes of memory this object has handed back to callers.

Returns
Bytes used from the capacity()

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