ON_FixedSizePool Class Reference

#include <opennurbs_fsp.h>

Inheritance diagram for ON_FixedSizePool:
ON_Internal_FontGlyphPool ON_SimpleFixedSizePool< T >

Public Member Functions

 ON_FixedSizePool ()
 
 ~ON_FixedSizePool ()
 
size_t ActiveElementCount () const
 
void * AllocateDirtyElement ()
 
void * AllocateElement ()
 
bool Create (size_t sizeof_element, size_t element_count_estimate, size_t block_element_capacity)
 Create a fixed size memory pool. More...
 
void Destroy ()
 Destroy the pool and free all the heap. The pool cannot be used again until Create() is called. More...
 
void * Element (size_t element_index) const
 Get the i-th elment in the fixed size pool. More...
 
void * ElementFromId (size_t id_offset, unsigned int id) const
 If you are certain that all elements in hte pool (active and returned) have an unsigned id that is unique and increasing, then you may use this function to find them. More...
 
size_t ElementIndex (const void *element_pointer) const
 Get the fixed size pool index of an element. More...
 
bool IsValid () const
 Primarily used for debugging. More...
 
void ReturnAll ()
 Return all allocated elements to the pool. No heap is freed and the pool remains initialized and ready for AllocateElement() to be called. More...
 
void ReturnElement (void *p)
 Return an element to the pool. More...
 
size_t SizeofElement () const
 
size_t TotalElementCount () const
 

Friends

class ON_FixedSizePoolIterator
 

Constructor & Destructor Documentation

◆ ON_FixedSizePool()

ON_FixedSizePool::ON_FixedSizePool ( )

◆ ~ON_FixedSizePool()

ON_FixedSizePool::~ON_FixedSizePool ( )

Member Function Documentation

◆ ActiveElementCount()

size_t ON_FixedSizePool::ActiveElementCount ( ) const
Returns
Number of active elements. (Elements that have been returned are not active.)

◆ AllocateDirtyElement()

void* ON_FixedSizePool::AllocateDirtyElement ( )
Returns
A pointer to sizeof_element bytes. The values in the returned block are undefined.

◆ AllocateElement()

void* ON_FixedSizePool::AllocateElement ( )
Returns
A pointer to sizeof_element bytes. The memory is zeroed.

◆ Create()

bool ON_FixedSizePool::Create ( size_t  sizeof_element,
size_t  element_count_estimate,
size_t  block_element_capacity 
)

Create a fixed size memory pool.

Parameters
sizeof_element[in] number of bytes in each element. This parameter must be greater than zero. In general, use sizeof(element type). If you pass a "raw" number as sizeof_element, then be certain that it is the right size to insure the fields in your elements will be properly aligned.
element_count_estimate[in] (0 = good default) If you know how many elements you will need, pass that number here. It is better to slightly overestimate than to slightly underestimate. If you do not have a good estimate, then use zero.
block_element_capacity[in] (0 = good default) If block_element_capacity is zero, Create() will calculate a block size that is efficent for most applications. If you are an expert user and want to specify the number of elements per block, then pass the number of elements per block here. When block_element_capacity > 0 and element_count_estimate > 0, the first block will have a capacity of at least element_count_estimate; in this case do not ask for extraordinarly large amounts of contiguous heap.
Returns
True if successful and the pool can be used.

You must call Create() on an unused ON_FixedSizePool or call Destroy() before calling create.

◆ Destroy()

void ON_FixedSizePool::Destroy ( )

Destroy the pool and free all the heap. The pool cannot be used again until Create() is called.

◆ Element()

void* ON_FixedSizePool::Element ( size_t  element_index) const

Get the i-th elment in the fixed size pool.

Parameters
element_index[in]
Returns
A pointer to the element with the specified index. The first element has element_index = 0 and is the element returned by the first call to AllocateElement(). The last element has element_index = ElementCount()-1. If element_index is out of range, nullptr is returned.

It is faster to use ON_FixedSizePoolIterator.FirstElement() and ON_FixedSizePoolIterator.NextElement() to iterate through the entire list of elements. This function is relatively efficient when there are a few large blocks in the pool or element_index is small compared to the number of elements in the first few blocks.

If ReturnElement() is not used or no AllocateElement() calls are made after any use of ReturnElement(), then the i-th element is the one returned by the (i+1)-th call to AllocateElement()

◆ ElementFromId()

void* ON_FixedSizePool::ElementFromId ( size_t  id_offset,
unsigned int  id 
) const

If you are certain that all elements in hte pool (active and returned) have an unsigned id that is unique and increasing, then you may use this function to find them.

Parameters
id_offset[in] offset into the element where the id is stored.
id[in] id to search for

◆ ElementIndex()

size_t ON_FixedSizePool::ElementIndex ( const void *  element_pointer) const

Get the fixed size pool index of an element.

Parameters
element_pointer[in]
Returns
An index >= 0 and < ON_MAX_SIZE_T if the element_pointer points to an element managed by the this fixed size pool. ON_MAX_SIZE_T otherwise.

It is faster to use ON_FixedSizePoolIterator.FirstElement() and ON_FixedSizePoolIterator.NextElement() to iterate through the entire list of elements. This function is relatively efficient when there are a few large blocks in the pool or element_pointer is an element in the first few blocks.

If ReturnElement() is not used or no AllocateElement() calls are made after any use of ReturnElement(), then the i-th element is the one returned by the (i+1)-th call to AllocateElement().

◆ IsValid()

bool ON_FixedSizePool::IsValid ( ) const

Primarily used for debugging.

◆ ReturnAll()

void ON_FixedSizePool::ReturnAll ( )

Return all allocated elements to the pool. No heap is freed and the pool remains initialized and ready for AllocateElement() to be called.

◆ ReturnElement()

void ON_FixedSizePool::ReturnElement ( void *  p)

Return an element to the pool.

Parameters
p[in] A pointer returned by AllocateElement(). It is critical that p be from this pool and that you return a pointer no more than one time.

If you find the following remarks confusing, but you really want to use ReturnElement(), then here are some simple guidelines. 1) SizeofElement() must be >= 16 2) SizeofElement() must be a multiple of 8. 3) Do not use FirstElement() and NextElement() to iterate through the pool.

If 1 to 3 don't work for you, then you need to understand the following information before using ReturnElement().

ON_FixedMemoryPool uses the first sizeof(void*) bytes of the returned element for bookkeeping purposes. Therefore, if you are going to use ReturnElement(), then SizeofElement() must be at least sizeof(void*). If you are using a platform that requires pointers to be aligned on sizeof(void*) boundaries, then SizeofElement() must be a multiple of sizeof(void*). If you are going to use ReturnElement() and then use FirstElement() and NextElement() to iterate through the list of elements, then you need to set a value in the returned element to indicate that it needs to be skipped during the iteration. This value cannot be located in the fist sizeof(void*) bytes of the element. If the element is a class with a vtable, you cannot call a virtual function on a returned element because the vtable pointer is trashed when ReturnElement() modifies the fist sizeof(void*) bytes.

◆ SizeofElement()

size_t ON_FixedSizePool::SizeofElement ( ) const
Returns
Size of the elements in this pool.

◆ TotalElementCount()

size_t ON_FixedSizePool::TotalElementCount ( ) const
Returns
Total number of elements = number of active elements + number of returned elements.

Friends And Related Function Documentation

◆ ON_FixedSizePoolIterator

friend class ON_FixedSizePoolIterator
friend