I l@ve RuBoard Previous Section Next Section

4.9 Administrivia

This section discusses how to use the SmallObj.h file in your applications.

To use SmallObject, you must provide appropriate parameters to SmallObjAllocator's constructor: the chunk size and the maximum size of a small object. How is a small object defined? What is the size under which an object can be considered small?

To find an answer to this question, let's go back to the purpose of building a small-object allocator. We wanted to mitigate the inefficiencies in size and time introduced by the default allocator.

The size overhead imposed by the default allocator varies largely. After all, the default allocator can use similar strategies to the ones discussed in this chapter. For most generic allocators, however, you can expect a per-object overhead that varies between 4 and 32 bytes per object in typical desktop machines. For an allocator with 16 bytes of overhead per object, an object of 64 bytes wastes 25% of memory; thus, a 64-byte object should be considered small.

On the other hand, if SmallObjAllocator handles objects that are too large, you end up allocating much more memory than needed (don't forget that FixedAllocator tends to keep one chunk allocated even after you have freed all small objects).

Loki gives you a choice and tries to provide appropriate defaults. The SmallObj.h file uses three preprocessor symbols, described in Table 4.1. You should compile all the source files in a given project with the same preprocessor symbols defined (or don't define them at all and rely on the defaults). If you don't do this, nothing lethal happens; you just end up creating more FixedAllocators tuned to different sizes.

The defaults are targeted toward a desktop machine with a reasonable amount of physical memory. If you #define either MAX_SMALL_OBJECT_SIZE or DEFAULT_CHUNK_SIZE to be zero, then SmallObj.h uses conditional compilation to generate code that simply uses the default ::operator new and ::operator delete, without incurring any overhead at all. The interface of the objects defined remains the same, but their functions are inline stubs that forward to the default free store allocator.

The class template SmallObject used to have one parameter. To support different chunk sizes and object sizes, SmallObject gets two more template parameters. They default to DEFAULT_CHUNK_SIZE and MAX_SMALL_OBJECT_SIZE, respectively.



template


<


   template <class T>


      class ThreadingModel = DEFAULT_THREADING,


   std::size_t chunkSize = DEFAULT_CHUNK_SIZE,


   std::size_t maxSmallObjectSize = MAX_SMALL_OBJECT_SIZE


>


class SmallObject;


If you just say SmallObject<>, you get a class that can work with your default threading model, garnished with the default choices concerning memory management.

Table 4.1. Preprocessor Symbols Used by SmallObj.h
Symbol Meaning Default Value
DEFAULT_CHUNK_SIZE The default size (in bytes) of a memory chunk. 4096
MAX_SMALL_OBJECT_SIZE The maximum value that is handled by SmallObjAllocator. 64
DEFAULT_THREADING The default threading model used by the application. A multithreaded application should define this symbol to ClassLevelLockable. Inherited from Threads.h

    I l@ve RuBoard Previous Section Next Section