Nugget
Public Member Functions | List of all members
eastl::fixed_allocator Class Reference

#include <fixed_allocator.h>

Inheritance diagram for eastl::fixed_allocator:
Inheritance graph
[legend]
Collaboration diagram for eastl::fixed_allocator:
Collaboration graph
[legend]

Public Member Functions

 fixed_allocator (const char *=EASTL_FIXED_POOL_DEFAULT_NAME)
 
 fixed_allocator (const fixed_allocator &)
 
fixed_allocatoroperator= (const fixed_allocator &)
 
void * allocate (size_t n, int=0)
 
void * allocate (size_t n, size_t, size_t, int flags=0)
 
void deallocate (void *p, size_t)
 
const char * get_name () const
 
void set_name (const char *)
 
bool can_allocate () const
 
- Public Member Functions inherited from eastl::fixed_pool_base
 fixed_pool_base (void *pMemory=NULL)
 
fixed_pool_baseoperator= (const fixed_pool_base &)
 
EASTL_API void init (void *pMemory, size_t memorySize, size_t nodeSize, size_t alignment, size_t alignmentOffset=0)
 
size_t peak_size () const
 
bool can_allocate () const
 

Additional Inherited Members

- Public Attributes inherited from eastl::fixed_pool_base
LinkmpHead
 
LinkmpNext
 
LinkmpCapacity
 
size_t mnNodeSize
 

Detailed Description

fixed_allocator

Implements an allocator which allocates a single fixed size where the size, alignment, and memory used for the pool is defined at runtime by the user. This is different from fixed containers such as fixed_list whereby the size and alignment are determined at compile time and the memory is directly built into the container's member data.

If the pool's memory is exhausted or was never initialized, the allocate function returns NULL. Consider the fixed_allocator_with_overflow class as an alternative in order to deal with this situation.

This class requires the user to call container.get_allocator().init() after constructing the container. There currently isn't a way to construct the container with the initialization parameters, though with some effort such a thing could probably be made possible. It's not as simple as it might first seem, due to the non-copyable nature of fixed allocators. A side effect of this limitation is that you cannot copy-construct a container using fixed_allocators.

Another side-effect is that you cannot swap two containers using a fixed_allocator, as a swap requires temporary memory allocated by an equivalent allocator, and such a thing cannot be done implicitly. A workaround for the swap limitation is that you can implement your own swap whereby you provide an explicitly created temporary object.

Note: Be careful to set the allocator's node size to the size of the container node and not the size of the contained object. Note that the example code below uses IntListNode.

Example usage: typedef eastl::list<int, fixed_allocator> IntList; typedef IntList::node_type IntListNode;

IntListNode buffer[200]; IntList intList; intList.get_allocator().init(buffer, sizeof(buffer), sizeof(IntListNode), __alignof(IntListNode));

Constructor & Destructor Documentation

◆ fixed_allocator() [1/2]

eastl::fixed_allocator::fixed_allocator ( const char *  = EASTL_FIXED_POOL_DEFAULT_NAME)
inline

fixed_allocator

Default constructor. The user usually will need to call init() after
constructing via this constructor.

◆ fixed_allocator() [2/2]

eastl::fixed_allocator::fixed_allocator ( const fixed_allocator )
inline

fixed_allocator

Copy constructor. The user usually will need to call init() after
constructing via this constructor. By their nature, fixed-allocators cannot be copied in any useful way, as by their nature the user must manually initialize them.

Member Function Documentation

◆ allocate() [1/2]

void* eastl::fixed_allocator::allocate ( size_t  n,
int  = 0 
)
inline

allocate

Allocates a new object of the size specified upon class initialization. Returns NULL if there is no more memory.

◆ allocate() [2/2]

void* eastl::fixed_allocator::allocate ( size_t  n,
size_t  ,
size_t  ,
int  flags = 0 
)
inline

allocate

◆ can_allocate()

bool eastl::fixed_pool_base::can_allocate
inline

can_allocate

Returns true if there are any free links.

◆ deallocate()

void eastl::fixed_allocator::deallocate ( void *  p,
size_t   
)
inline

deallocate

Frees the given object which was allocated by allocate(). If the given node was not allocated by allocate() then the behaviour is undefined.

◆ operator=()

fixed_allocator& eastl::fixed_allocator::operator= ( const fixed_allocator )
inline

operator=

By their nature, fixed-allocators cannot be copied in any useful way, as by their nature the user must manually initialize them.


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