Boost  v1.57.0
doxygen for www.boost.org
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
boost::simple_segregated_storage< SizeType > Class Template Reference

Simple Segregated Storage is the simplest, and probably the fastest, memory allocation/deallocation algorithm. More...

#include <simple_segregated_storage.hpp>

Public Types

typedef SizeType size_type
 

Public Member Functions

 simple_segregated_storage ()
 
void add_block (void *const block, const size_type nsz, const size_type npartition_sz)
 
void add_ordered_block (void *const block, const size_type nsz, const size_type npartition_sz)
 
bool empty () const
 
void *malloc BOOST_PREVENT_MACRO_SUBSTITUTION ()
 
void free BOOST_PREVENT_MACRO_SUBSTITUTION (void *const chunk)
 
void ordered_free (void *const chunk)
 
void * malloc_n (size_type n, size_type partition_size)
 Attempts to find a contiguous sequence of n partition_sz-sized chunks. More...
 
void free_n (void *const chunks, const size_type n, const size_type partition_size)
 
void ordered_free_n (void *const chunks, const size_type n, const size_type partition_size)
 

Static Public Member Functions

static void * segregate (void *block, size_type nsz, size_type npartition_sz, void *end=0)
 Segregate block into chunks. More...
 

Protected Member Functions

void * find_prev (void *ptr)
 Traverses the free list referred to by "first", and returns the iterator previous to where "ptr" would go if it was in the free list. More...
 

Static Protected Member Functions

static void *& nextof (void *const ptr)
 

Protected Attributes

void * first
 This data member is the free list. More...
 

Detailed Description

template<typename SizeType>
class boost::simple_segregated_storage< SizeType >

Simple Segregated Storage is the simplest, and probably the fastest, memory allocation/deallocation algorithm.

It is responsible for partitioning a memory block into fixed-size chunks: where the block comes from is determined by the client of the class.

Template class simple_segregated_storage controls access to a free list of memory chunks. Please note that this is a very simple class, with preconditions on almost all its functions. It is intended to be the fastest and smallest possible quick memory allocator - e.g., something to use in embedded systems. This class delegates many difficult preconditions to the user (i.e., alignment issues).

An object of type simple_segregated_storage<SizeType> is empty if its free list is empty. If it is not empty, then it is ordered if its free list is ordered. A free list is ordered if repeated calls to malloc() will result in a constantly-increasing sequence of values, as determined by std::less<void *>. A member function is order-preserving if the free list maintains its order orientation (that is, an ordered free list is still ordered after the member function call).

Member Typedef Documentation

template<typename SizeType >
typedef SizeType boost::simple_segregated_storage< SizeType >::size_type

Constructor & Destructor Documentation

template<typename SizeType >
boost::simple_segregated_storage< SizeType >::simple_segregated_storage ( )
inline

Construct empty storage area.

Postcondition
empty()

Member Function Documentation

template<typename SizeType >
void boost::simple_segregated_storage< SizeType >::add_block ( void *const  block,
const size_type  nsz,
const size_type  npartition_sz 
)
inline

Add block Segregate this block and merge its free list into the free list referred to by "first".

Precondition
Same as segregate.
Postcondition
!empty()

References BOOST_POOL_VALIDATE_INTERNALS, and boost::simple_segregated_storage< SizeType >::segregate().

Referenced by boost::simple_segregated_storage< SizeType >::add_ordered_block(), and boost::simple_segregated_storage< SizeType >::free_n().

template<typename SizeType >
void boost::simple_segregated_storage< SizeType >::add_ordered_block ( void *const  block,
const size_type  nsz,
const size_type  npartition_sz 
)
inline
template<typename SizeType >
void* malloc boost::simple_segregated_storage< SizeType >::BOOST_PREVENT_MACRO_SUBSTITUTION ( )
inline

Create a chunk.

Precondition
!empty() Increment the "first" pointer to point to the next chunk.

References BOOST_POOL_VALIDATE_INTERNALS, boost::simple_segregated_storage< SizeType >::first, and boost::simple_segregated_storage< SizeType >::nextof().

template<typename SizeType >
void free boost::simple_segregated_storage< SizeType >::BOOST_PREVENT_MACRO_SUBSTITUTION ( void *const  chunk)
inline

Free a chunk.

Precondition
chunk was previously returned from a malloc() referring to the same free list.
Postcondition
!empty()

References BOOST_POOL_VALIDATE_INTERNALS, boost::simple_segregated_storage< SizeType >::first, and boost::simple_segregated_storage< SizeType >::nextof().

template<typename SizeType >
bool boost::simple_segregated_storage< SizeType >::empty ( void  ) const
inline
Returns
true only if simple_segregated_storage is empty.
template<typename SizeType >
void * boost::simple_segregated_storage< SizeType >::find_prev ( void *  ptr)
protected

Traverses the free list referred to by "first", and returns the iterator previous to where "ptr" would go if it was in the free list.

Returns 0 if "ptr" would go at the beginning of the free list (i.e., before "first").

Note
Note that this function finds the location previous to where ptr would go if it was in the free list. It does not find the entry in the free list before ptr (unless ptr is already in the free list). Specifically, find_prev(0) will return 0, not the last entry in the free list.
Returns
location previous to where ptr would go if it was in the free list.

References boost::xpressive::first.

Referenced by boost::simple_segregated_storage< SizeType >::add_ordered_block(), and boost::simple_segregated_storage< SizeType >::ordered_free().

template<typename SizeType >
void boost::simple_segregated_storage< SizeType >::free_n ( void *const  chunks,
const size_type  n,
const size_type  partition_size 
)
inline
Precondition
chunks was previously allocated from *this with the same values for n and partition_size.
Postcondition
!empty()
Note
If you're allocating/deallocating n a lot, you should be using an ordered pool.

References boost::simple_segregated_storage< SizeType >::add_block(), and BOOST_POOL_VALIDATE_INTERNALS.

template<typename SizeType >
void * boost::simple_segregated_storage< SizeType >::malloc_n ( size_type  n,
size_type  partition_size 
)

Attempts to find a contiguous sequence of n partition_sz-sized chunks.

If found, removes them all from the free list and returns a pointer to the first. If not found, returns 0. It is strongly recommended (but not required) that the free list be ordered, as this algorithm will fail to find a contiguous sequence unless it is contiguous in the free list as well. Order-preserving. O(N) with respect to the size of the free list.

References BOOST_POOL_VALIDATE_INTERNALS, and boost::xpressive::first.

template<typename SizeType >
static void* & boost::simple_segregated_storage< SizeType >::nextof ( void *const  ptr)
inlinestaticprotected

The return value is just *ptr cast to the appropriate type. ptr must not be 0. (For the sake of code readability :)

As an example, let us assume that we want to truncate the free list after the first chunk. That is, we want to set *first to 0; this will result in a free list with only one entry. The normal way to do this is to first cast first to a pointer to a pointer to void, and then dereference and assign (*static_cast<void **>(first) = 0;). This can be done more easily through the use of this convenience function (nextof(first) = 0;).

Returns
dereferenced pointer.

References boost::python::ptr().

Referenced by boost::simple_segregated_storage< SizeType >::add_ordered_block(), boost::simple_segregated_storage< SizeType >::BOOST_PREVENT_MACRO_SUBSTITUTION(), and boost::simple_segregated_storage< SizeType >::ordered_free().

template<typename SizeType >
void boost::simple_segregated_storage< SizeType >::ordered_free ( void *const  chunk)
inline

This (slower) implementation of 'free' places the memory back in the list in its proper order.

Precondition
chunk was previously returned from a malloc() referring to the same free list
Postcondition
!empty().

References BOOST_POOL_VALIDATE_INTERNALS, boost::simple_segregated_storage< SizeType >::find_prev(), and boost::simple_segregated_storage< SizeType >::nextof().

template<typename SizeType >
void boost::simple_segregated_storage< SizeType >::ordered_free_n ( void *const  chunks,
const size_type  n,
const size_type  partition_size 
)
inline

Free n chunks from order list.

Precondition
chunks was previously allocated from *this with the same values for n and partition_size.
n should not be zero (n == 0 has no effect).

References boost::simple_segregated_storage< SizeType >::add_ordered_block(), and BOOST_POOL_VALIDATE_INTERNALS.

template<typename SizeType >
void * boost::simple_segregated_storage< SizeType >::segregate ( void *  block,
size_type  nsz,
size_type  npartition_sz,
void *  end = 0 
)
static

Segregate block into chunks.

Precondition
npartition_sz >= sizeof(void *)
npartition_sz = sizeof(void *) * i, for some integer i
nsz >= npartition_sz
Block is properly aligned for an array of object of size npartition_sz and array of void *. The requirements above guarantee that any pointer to a chunk (which is a pointer to an element in an array of npartition_sz) may be cast to void **.

References boost::end.

Referenced by boost::simple_segregated_storage< SizeType >::add_block(), and boost::simple_segregated_storage< SizeType >::add_ordered_block().

Member Data Documentation

template<typename SizeType >
void* boost::simple_segregated_storage< SizeType >::first
protected

This data member is the free list.

It points to the first chunk in the free list, or is equal to 0 if the free list is empty.

Referenced by boost::simple_segregated_storage< SizeType >::BOOST_PREVENT_MACRO_SUBSTITUTION().


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