# User Allocators

## Introduction

Pool objects need to request memory blocks from the system, which the Pool then splits into chunks to allocate to the user. By specifying a UserAllocator template parameter to various Pool interfaces, users can control how those system memory blocks are allocated.

## Semantics

Symbol Table
Symbol | Meaning
--- | ---
UserAllocator | A User Allocator type
block | value of type char *
n | value of type UserAllocator::size_type

Typedefs
Expression | Type
--- | ---
UserAllocator::size_type | An unsigned integral type that can represent the size of the largest object to be allocated
UserAllocator::difference_type | A signed integral type that can represent the difference of any two pointers

Allocation and Deallocation
Expression | Return Type | Pre-Condition/Notes
--- | --- | ---
UserAllocator::malloc(n) | char * | Attempts to allocate n bytes from the system. Returns 0 if out-of-memory.
UserAllocator::free(block) | void | block must have been previously returned from a call to UserAllocator::malloc.

## Provided Implementations

There are two UserAllocator classes provided. Both of them are in pool.hpp (see pool). The default value for the template parameter UserAllocator is always default_user_allocator_new_delete.

### Synopsis

    struct default_user_allocator_new_delete
    {
      typedef std::size_t size_type;
      typedef std::ptrdiff_t difference_type;

      static char * malloc(const size_type bytes)
      { return new (std::nothrow) char[bytes]; }
      static void free(char * const block)
      { delete [] block; }
    };

    struct default_user_allocator_malloc_free
    {
      typedef std::size_t size_type;
      typedef std::ptrdiff_t difference_type;

      static char * malloc(const size_type bytes)
      { return reinterpret_cast<char *>(std::malloc(bytes)); }
      static void free(char * const block)
      { std::free(block); }
    };

Revised 05 December, 2006

Copyright © 2000, 2001 Stephen Cleary (scleary AT jerviswebb DOT com)

Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)