epoc32/include/stdapis/boost/detail/quick_allocator.hpp
author William Roberts <williamr@symbian.org>
Wed, 31 Mar 2010 12:33:34 +0100
branchSymbian3
changeset 4 837f303aceeb
permissions -rw-r--r--
Current Symbian^3 public API header files (from PDK 3.0.h)
This is the epoc32/include tree with the "platform" subtrees removed, and
all but a selected few mbg and rsg files removed.
williamr@2
     1
#ifndef BOOST_DETAIL_QUICK_ALLOCATOR_HPP_INCLUDED
williamr@2
     2
#define BOOST_DETAIL_QUICK_ALLOCATOR_HPP_INCLUDED
williamr@2
     3
williamr@2
     4
// MS compatible compilers support #pragma once
williamr@2
     5
williamr@2
     6
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
williamr@2
     7
# pragma once
williamr@2
     8
#endif
williamr@2
     9
williamr@2
    10
//
williamr@2
    11
//  detail/quick_allocator.hpp
williamr@2
    12
//
williamr@2
    13
//  Copyright (c) 2003 David Abrahams
williamr@2
    14
//  Copyright (c) 2003 Peter Dimov
williamr@2
    15
//
williamr@2
    16
// Distributed under the Boost Software License, Version 1.0. (See
williamr@2
    17
// accompanying file LICENSE_1_0.txt or copy at
williamr@2
    18
// http://www.boost.org/LICENSE_1_0.txt)
williamr@2
    19
//
williamr@2
    20
williamr@2
    21
#include <boost/config.hpp>
williamr@2
    22
williamr@2
    23
#include <boost/detail/lightweight_mutex.hpp>
williamr@2
    24
#include <boost/type_traits/type_with_alignment.hpp>
williamr@2
    25
#include <boost/type_traits/alignment_of.hpp>
williamr@2
    26
williamr@2
    27
#include <new>              // ::operator new, ::operator delete
williamr@2
    28
#include <cstddef>          // std::size_t
williamr@2
    29
williamr@2
    30
namespace boost
williamr@2
    31
{
williamr@2
    32
williamr@2
    33
namespace detail
williamr@2
    34
{
williamr@2
    35
williamr@2
    36
template<unsigned size, unsigned align_> union freeblock
williamr@2
    37
{
williamr@2
    38
    typedef typename boost::type_with_alignment<align_>::type aligner_type;
williamr@2
    39
    aligner_type aligner;
williamr@2
    40
    char bytes[size];
williamr@2
    41
    freeblock * next;
williamr@2
    42
};
williamr@2
    43
williamr@2
    44
template<unsigned size, unsigned align_> struct allocator_impl
williamr@2
    45
{
williamr@2
    46
    typedef freeblock<size, align_> block;
williamr@2
    47
williamr@2
    48
    // It may seem odd to use such small pages.
williamr@2
    49
    //
williamr@2
    50
    // However, on a typical Windows implementation that uses
williamr@2
    51
    // the OS allocator, "normal size" pages interact with the
williamr@2
    52
    // "ordinary" operator new, slowing it down dramatically.
williamr@2
    53
    //
williamr@2
    54
    // 512 byte pages are handled by the small object allocator,
williamr@2
    55
    // and don't interfere with ::new.
williamr@2
    56
    //
williamr@2
    57
    // The other alternative is to use much bigger pages (1M.)
williamr@2
    58
    //
williamr@2
    59
    // It is surprisingly easy to hit pathological behavior by
williamr@2
    60
    // varying the page size. g++ 2.96 on Red Hat Linux 7.2,
williamr@2
    61
    // for example, passionately dislikes 496. 512 seems OK.
williamr@2
    62
williamr@2
    63
#if defined(BOOST_QA_PAGE_SIZE)
williamr@2
    64
williamr@2
    65
    enum { items_per_page = BOOST_QA_PAGE_SIZE / size };
williamr@2
    66
williamr@2
    67
#else
williamr@2
    68
williamr@2
    69
    enum { items_per_page = 512 / size }; // 1048560 / size
williamr@2
    70
williamr@2
    71
#endif
williamr@2
    72
williamr@2
    73
#ifdef BOOST_HAS_THREADS
williamr@2
    74
williamr@2
    75
    static lightweight_mutex & mutex()
williamr@2
    76
    {
williamr@2
    77
        static lightweight_mutex m;
williamr@2
    78
        return m;
williamr@2
    79
    }
williamr@2
    80
williamr@2
    81
    static lightweight_mutex * mutex_init;
williamr@2
    82
williamr@2
    83
#endif
williamr@2
    84
williamr@2
    85
    static block * free;
williamr@2
    86
    static block * page;
williamr@2
    87
    static unsigned last;
williamr@2
    88
williamr@2
    89
    static inline void * alloc()
williamr@2
    90
    {
williamr@2
    91
#ifdef BOOST_HAS_THREADS
williamr@2
    92
        lightweight_mutex::scoped_lock lock( mutex() );
williamr@2
    93
#endif
williamr@2
    94
        if(block * x = free)
williamr@2
    95
        {
williamr@2
    96
            free = x->next;
williamr@2
    97
            return x;
williamr@2
    98
        }
williamr@2
    99
        else
williamr@2
   100
        {
williamr@2
   101
            if(last == items_per_page)
williamr@2
   102
            {
williamr@2
   103
                // "Listen to me carefully: there is no memory leak"
williamr@2
   104
                // -- Scott Meyers, Eff C++ 2nd Ed Item 10
williamr@2
   105
                page = ::new block[items_per_page];
williamr@2
   106
                last = 0;
williamr@2
   107
            }
williamr@2
   108
williamr@2
   109
            return &page[last++];
williamr@2
   110
        }
williamr@2
   111
    }
williamr@2
   112
williamr@2
   113
    static inline void * alloc(std::size_t n)
williamr@2
   114
    {
williamr@2
   115
        if(n != size) // class-specific new called for a derived object
williamr@2
   116
        {
williamr@2
   117
            return ::operator new(n);
williamr@2
   118
        }
williamr@2
   119
        else
williamr@2
   120
        {
williamr@2
   121
#ifdef BOOST_HAS_THREADS
williamr@2
   122
            lightweight_mutex::scoped_lock lock( mutex() );
williamr@2
   123
#endif
williamr@2
   124
            if(block * x = free)
williamr@2
   125
            {
williamr@2
   126
                free = x->next;
williamr@2
   127
                return x;
williamr@2
   128
            }
williamr@2
   129
            else
williamr@2
   130
            {
williamr@2
   131
                if(last == items_per_page)
williamr@2
   132
                {
williamr@2
   133
                    page = ::new block[items_per_page];
williamr@2
   134
                    last = 0;
williamr@2
   135
                }
williamr@2
   136
williamr@2
   137
                return &page[last++];
williamr@2
   138
            }
williamr@2
   139
        }
williamr@2
   140
    }
williamr@2
   141
williamr@2
   142
    static inline void dealloc(void * pv)
williamr@2
   143
    {
williamr@2
   144
        if(pv != 0) // 18.4.1.1/13
williamr@2
   145
        {
williamr@2
   146
#ifdef BOOST_HAS_THREADS
williamr@2
   147
            lightweight_mutex::scoped_lock lock( mutex() );
williamr@2
   148
#endif
williamr@2
   149
            block * pb = static_cast<block *>(pv);
williamr@2
   150
            pb->next = free;
williamr@2
   151
            free = pb;
williamr@2
   152
        }
williamr@2
   153
    }
williamr@2
   154
williamr@2
   155
    static inline void dealloc(void * pv, std::size_t n)
williamr@2
   156
    {
williamr@2
   157
        if(n != size) // class-specific delete called for a derived object
williamr@2
   158
        {
williamr@2
   159
            ::operator delete(pv);
williamr@2
   160
        }
williamr@2
   161
        else if(pv != 0) // 18.4.1.1/13
williamr@2
   162
        {
williamr@2
   163
#ifdef BOOST_HAS_THREADS
williamr@2
   164
            lightweight_mutex::scoped_lock lock( mutex() );
williamr@2
   165
#endif
williamr@2
   166
            block * pb = static_cast<block *>(pv);
williamr@2
   167
            pb->next = free;
williamr@2
   168
            free = pb;
williamr@2
   169
        }
williamr@2
   170
    }
williamr@2
   171
};
williamr@2
   172
williamr@2
   173
#ifdef BOOST_HAS_THREADS
williamr@2
   174
williamr@2
   175
template<unsigned size, unsigned align_>
williamr@2
   176
  lightweight_mutex * allocator_impl<size, align_>::mutex_init = &allocator_impl<size, align_>::mutex();
williamr@2
   177
williamr@2
   178
#endif
williamr@2
   179
williamr@2
   180
template<unsigned size, unsigned align_>
williamr@2
   181
  freeblock<size, align_> * allocator_impl<size, align_>::free = 0;
williamr@2
   182
williamr@2
   183
template<unsigned size, unsigned align_>
williamr@2
   184
  freeblock<size, align_> * allocator_impl<size, align_>::page = 0;
williamr@2
   185
williamr@2
   186
template<unsigned size, unsigned align_>
williamr@2
   187
  unsigned allocator_impl<size, align_>::last = allocator_impl<size, align_>::items_per_page;
williamr@2
   188
williamr@2
   189
template<class T>
williamr@2
   190
struct quick_allocator: public allocator_impl< sizeof(T), boost::alignment_of<T>::value >
williamr@2
   191
{
williamr@2
   192
};
williamr@2
   193
williamr@2
   194
} // namespace detail
williamr@2
   195
williamr@2
   196
} // namespace boost
williamr@2
   197
williamr@2
   198
#endif  // #ifndef BOOST_DETAIL_QUICK_ALLOCATOR_HPP_INCLUDED