# Loki::SmallObjAllocator Class Reference
[Internals]

`#include <SmallObj.h>`

Inheritance diagram for Loki::SmallObjAllocator:
[legend]
Collaboration diagram for Loki::SmallObjAllocator:
[legend]

List of all members.

## Public Member Functions

void * | Allocate (std::size_t size, bool doThrow)
void | Deallocate (void *p, std::size_t size)
void | Deallocate (void *p)
std::size_t | GetMaxObjectSize () const
| Returns max # of bytes which this can allocate.
std::size_t | GetAlignment () const
| Returns # of bytes between allocation boundaries.
bool | TrimExcessMemory (void)
bool | IsCorrupt (void) const

## Protected Member Functions

| SmallObjAllocator (std::size_t pageSize, std::size_t maxObjectSize, std::size_t objectAlignSize)
| ~SmallObjAllocator (void)

## Detailed Description

Manages pool of fixed-size allocators. Designed to be a non-templated base class of AllocatorSingleton so that implementation details can be safely hidden in the source code file.

## Constructor & Destructor Documentation

Loki::SmallObjAllocator::SmallObjAllocator | ( | std::size_t | pageSize,
|  | std::size_t | maxObjectSize,
|  | std::size_t | objectAlignSize |
| ) |  |  | `[protected]`

The only available constructor needs certain parameters in order to initialize all the FixedAllocator's. This throws only if

Parameters:

  | pageSize | # of bytes in a page of memory.
  | maxObjectSize | Max # of bytes which this may allocate.
  | objectAlignSize | # of bytes between alignment boundaries.

References Loki::GetOffset().

Here is the call graph for this function:

Loki::SmallObjAllocator::~SmallObjAllocator | ( | void |  | ) | `[protected]`

Destructor releases all blocks, all Chunks, and FixedAllocator's. Any outstanding blocks are unavailable, and should not be used after this destructor is called. The destructor is deliberately non-virtual because it is protected, not public.

## Member Function Documentation

void * Loki::SmallObjAllocator::Allocate | ( | std::size_t | size,
|  | bool | doThrow |
| ) |  |  |

Allocates a block of memory of requested size. Complexity is often constant-time, but might be O(C) where C is the number of Chunks in a FixedAllocator.

Exception Safety Level
  Provides either strong-exception safety, or no-throw exception-safety level depending upon doThrow parameter. The reason it provides two levels of exception safety is because it is used by both the nothrow and throwing new operators. The underlying implementation will never throw of its own accord, but this can decide to throw if it does not allocate. The only exception it should emit is std::bad_alloc.

Allocation Failure
  If it does not allocate, it will call TrimExcessMemory and attempt to allocate again, before it decides to throw or return NULL. Many allocators loop through several new_handler functions, and terminate if they can not allocate, but not this one. It only makes one attempt using its own implementation of the new_handler, and then returns NULL or throws so that the program can decide what to do at a higher level. (Side note: Even though the C++ Standard allows allocators and new_handlers to terminate if they fail, the Loki allocator does not do that since that policy is not polite to a host program.)

Parameters:

  | size | # of bytes needed for allocation.
  | doThrow | True if this should throw if unable to allocate, false if it should provide no-throw exception safety level.

Returns:
  NULL if nothing allocated and doThrow is false. Else the pointer to an available block of memory.

References Loki::FixedAllocator::Allocate(), Loki::FixedAllocator::BlockSize(), Loki::DefaultAllocator(), GetAlignment(), GetMaxObjectSize(), Loki::GetOffset(), and TrimExcessMemory().

Here is the call graph for this function:

void Loki::SmallObjAllocator::Deallocate | ( | void * | p | ) |

Deallocates a block of memory at a given place but of unknown size size. Complexity is O(F + C) where F is the count of FixedAllocator's in the pool, and C is the number of Chunks in all FixedAllocator's. This does not throw exceptions. This overloaded version of Deallocate is called by the nothow delete operator - which is called when the nothrow new operator is used, but a constructor throws an exception.

References Loki::FixedAllocator::Deallocate(), Loki::DefaultDeallocator(), GetAlignment(), GetMaxObjectSize(), Loki::GetOffset(), and Loki::FixedAllocator::HasBlock().

Here is the call graph for this function:

void Loki::SmallObjAllocator::Deallocate | ( | void * | p,
|  | std::size_t | size |
| ) |  |  |

Deallocates a block of memory at a given place and of a specific size. Complexity is almost always constant-time, and is O(C) only if it has to search for which Chunk deallocates. This never throws.

References Loki::FixedAllocator::BlockSize(), Loki::FixedAllocator::Deallocate(), Loki::DefaultDeallocator(), GetAlignment(), GetMaxObjectSize(), and Loki::GetOffset().

Here is the call graph for this function:

bool Loki::SmallObjAllocator::IsCorrupt | ( | void |  | ) | const

Returns true if anything in implementation is corrupt. Complexity is O(F + C + B) where F is the count of FixedAllocator's in the pool, C is the number of Chunks in all FixedAllocator's, and B is the number of blocks in all Chunks. If it determines any data is corrupted, this will return true in release version, but assert in debug version at the line where it detects the corrupted data. If it does not detect any corrupted data, it returns false.

References GetAlignment(), GetMaxObjectSize(), and Loki::GetOffset().

Referenced by Loki::AllocatorSingleton< ThreadingModel, chunkSize, maxSmallObjectSize, objectAlignSize, LifetimePolicy, MutexPolicy >::IsCorrupted().

Here is the call graph for this function:

bool Loki::SmallObjAllocator::TrimExcessMemory | ( | void |  | ) |

Releases empty Chunks from memory. Complexity is O(F + C) where F is the count of FixedAllocator's in the pool, and C is the number of Chunks in all FixedAllocator's. This will never throw. This is called by AllocatorSingleto::ClearExtraMemory, the new_handler function for Loki's allocator, and is called internally when an allocation fails.

Returns:
  True if any memory released, or false if none released.

References GetAlignment(), GetMaxObjectSize(), and Loki::GetOffset().

Referenced by Allocate(), and Loki::AllocatorSingleton< ThreadingModel, chunkSize, maxSmallObjectSize, objectAlignSize, LifetimePolicy, MutexPolicy >::ClearExtraMemory().

Here is the call graph for this function:

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

- SmallObj.h
- SmallObj.cpp