Loki: Loki::SmallObjectBase< ThreadingModel, chunkSize, maxSmallObjectSize, objectAlignSize, LifetimePolicy, MutexPolicy > Class Template Reference

# Loki::SmallObjectBase< ThreadingModel, chunkSize, maxSmallObjectSize, objectAlignSize, LifetimePolicy, MutexPolicy > Class Template Reference
[Small objects]

`#include <SmallObj.h>`

Inheritance diagram for Loki::SmallObjectBase< ThreadingModel, chunkSize, maxSmallObjectSize, objectAlignSize, LifetimePolicy, MutexPolicy >:
[legend]

List of all members.

## Public Types
typedef AllocatorSingleton < ThreadingModel, chunkSize, maxSmallObjectSize, objectAlignSize, LifetimePolicy > | ObjAllocatorSingleton
## Static Public Member Functions
static void * | operator new (std::size_t size) throw ( std::bad_alloc )
| Throwing single-object new throws bad_alloc when allocation fails.
static void * | operator new (std::size_t size, const std::nothrow_t &) throw ()
| Non-throwing single-object new returns NULL if allocation fails.
static void * | operator new (std::size_t size, void *place)
| Placement single-object new merely calls global placement new.
static void | operator delete (void *p, std::size_t size) throw ()
| Single-object delete.
static void | operator delete (void *p, const std::nothrow_t &) throw ()
static void | operator delete (void *p, void *place)
| Placement single-object delete merely calls global placement delete.

## Detailed Description

### template<template< class, class > class ThreadingModel, std::size_t chunkSize, std::size_t maxSmallObjectSize, std::size_t objectAlignSize, template< class > class LifetimePolicy, class MutexPolicy>
class Loki::SmallObjectBase< ThreadingModel, chunkSize, maxSmallObjectSize, objectAlignSize, LifetimePolicy, MutexPolicy >

Base class for small object allocation classes. The shared implementation of the new and delete operators are here instead of being duplicated in both SmallObject or SmallValueObject, later just called Small-Objects. This class is not meant to be used directly by clients, or derived from by clients. Class has no data members so compilers can use Empty-Base-Optimization.

ThreadingModel
  This class doesn't support ObjectLevelLockable policy for ThreadingModel. The allocator is a singleton, so a per-instance mutex is not necessary. Nor is using ObjectLevelLockable recommended with SingletonHolder since the SingletonHolder::MakeInstance function requires a mutex that exists prior to when the object is created - which is not possible if the mutex is inside the object, such as required for ObjectLevelLockable. If you attempt to use ObjectLevelLockable, the compiler will emit errors because it can't use the default constructor in ObjectLevelLockable. If you need a thread-safe allocator, use the ClassLevelLockable policy.

Lifetime Policy

The SmallObjectBase template needs a lifetime policy because it owns a singleton of SmallObjAllocator which does all the low level functions. When using a Small-Object in combination with the SingletonHolder template you have to choose two lifetimes, that of the Small-Object and that of the singleton. The rule is: The Small-Object lifetime must be greater than the lifetime of the singleton hosting the Small-Object. Violating this rule results in a crash on exit, because the hosting singleton tries to delete the Small-Object which is then already destroyed.

The lifetime policies recommended for use with Small-Objects hosted by a SingletonHolder template are

- LongevityLifetime::DieAsSmallObjectParent / LongevityLifetime::DieAsSmallObjectChild
- SingletonWithLongevity
- FollowIntoDeath (not supported by MSVC 7.1)
- NoDestroy

The default lifetime of Small-Objects is LongevityLifetime::DieAsSmallObjectParent to insure that memory is not released before a object with the lifetime LongevityLifetime::DieAsSmallObjectChild using that memory is destroyed. The LongevityLifetime::DieAsSmallObjectParent lifetime has the highest possible value of a SetLongevity lifetime, so you can use it in combination with your own lifetime not having also the highest possible value.

The DefaultLifetime and PhoenixSingleton policies are *not* recommended since they can cause the allocator to be destroyed and release memory for singletons hosting a object which inherit from either SmallObject or SmallValueObject.

Lifetime usage

- LongevityLifetime: The Small-Object has LongevityLifetime::DieAsSmallObjectParent policy and the Singleton hosting the Small-Object has LongevityLifetime::DieAsSmallObjectChild. The child lifetime has a hard coded SetLongevity lifetime which is shorter than the lifetime of the parent, thus the child dies before the parent.

- Both Small-Object and Singleton use SingletonWithLongevity policy. The longevity level for the singleton must be lower than that for the Small-Object. This is why the AllocatorSingleton's GetLongevity function returns the highest value.

- FollowIntoDeath lifetime: The Small-Object has FollowIntoDeath::With<LIFETIME>::AsMasterLiftime policy and the Singleton has FollowIntoDeath::AfterMaster<MASTERSINGLETON>::IsDestroyed policy, where you could choose the LIFETIME.

- Both Small-Object and Singleton use NoDestroy policy. Since neither is ever destroyed, the destruction order does not matter. Note: you will get memory leaks!

- The Small-Object has NoDestroy policy but the Singleton has SingletonWithLongevity policy. Note: you will get memory leaks!

You should *not* use NoDestroy for the singleton, and then use SingletonWithLongevity for the Small-Object.

Examples:

- test/SmallObj/SmallSingleton.cpp
- test/Singleton/Dependencies.cpp

## Member Typedef Documentation

template<template< class, class > class ThreadingModel, std::size_t chunkSize, std::size_t maxSmallObjectSize, std::size_t objectAlignSize, template< class > class LifetimePolicy, class MutexPolicy>

typedef AllocatorSingleton< ThreadingModel, chunkSize, maxSmallObjectSize, objectAlignSize, LifetimePolicy > Loki::SmallObjectBase< ThreadingModel, chunkSize, maxSmallObjectSize, objectAlignSize, LifetimePolicy, MutexPolicy >::ObjAllocatorSingleton

Defines type of allocator singleton, must be public to handle singleton lifetime dependencies.

## Member Function Documentation

template<template< class, class > class ThreadingModel, std::size_t chunkSize, std::size_t maxSmallObjectSize, std::size_t objectAlignSize, template< class > class LifetimePolicy, class MutexPolicy>

static void Loki::SmallObjectBase< ThreadingModel, chunkSize, maxSmallObjectSize, objectAlignSize, LifetimePolicy, MutexPolicy >::operator delete | ( | void * | p,
|  | const std::nothrow_t & |  |
| ) |  |  | throw ()`[inline, static]`

Non-throwing single-object delete is only called when nothrow new operator is used, and the constructor throws an exception.

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

- SmallObj.h