summaryrefslogtreecommitdiffstats
path: root/src/third-party/ArenaAlloc/arenaalloc.h
blob: dfd648dcb7f4986c5ead820eddf332a57984667f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// -*- c++ -*-
/******************************************************************************
 *  arenaalloc.h
 *  
 *  Arena allocator based on the example logic provided by Nicolai Josuttis
 *  and available at http://www.josuttis.com/libbook/examples.html.
 *  This enhanced work is provided under the terms of the MIT license.
 *  
 *****************************************************************************/

#ifndef _ARENA_ALLOC_H
#define _ARENA_ALLOC_H

#include <limits>
#include <memory>

#if __cplusplus >= 201103L
#include <type_traits>
#include <utility>
#endif

// Define macro ARENA_ALLOC_DEBUG to enable some tracing of the allocator
#include "arenaallocimpl.h"

namespace ArenaAlloc 
{  
  
  struct _newAllocatorImpl
  {
    // these two functions should be supported by a specialized
    // allocator for shared memory or another source of specialized
    // memory such as device mapped memory.
    void* allocate( size_t numBytes ) { return new char[ numBytes ]; }
    void deallocate( void* ptr ) { delete[]( (char*)ptr ); }
  };
  
  template <class T, 
	    class AllocatorImpl = _newAllocatorImpl, 
	    class MemblockImpl = _memblockimpl<AllocatorImpl> >
  class Alloc {
    
  private:        
    MemblockImpl* m_impl;    
    
  public:
    // type definitions
    typedef T        value_type;
    typedef T*       pointer;
    typedef const T* const_pointer;
    typedef T&       reference;
    typedef const T& const_reference;
    typedef std::size_t    size_type;
    typedef std::ptrdiff_t difference_type;
    
#if __cplusplus >= 201103L
    // when containers are swapped, (i.e. vector.swap)
    // swap the allocators also.  This was not specified in c++98
    // thus users of this code not using c++11 must
    // exercise caution when using the swap algorithm or
    // specialized swap member function.  Specifically,
    // don't swap containers not sharing the same
    // allocator internal implementation in c++98.  This is ok
    // in c++11.
    typedef std::true_type propagate_on_container_swap;

    // container moves should move the allocator also.
    typedef std::true_type propagate_on_container_move_assignment;    
#endif
    
    // rebind allocator to type U
    template <class U>
    struct rebind {
      typedef Alloc<U,AllocatorImpl,MemblockImpl> other;
    };
   
    // return address of values
    pointer address (reference value) const {
      return &value;
    }
    const_pointer address (const_reference value) const {
      return &value;
    }

    Alloc( std::size_t defaultSize = 32768, AllocatorImpl allocImpl = AllocatorImpl() ) throw():
      m_impl( MemblockImpl::create( defaultSize, allocImpl ) )
    {      
    }
    
    Alloc(const Alloc& src)  throw(): 
      m_impl( src.m_impl )
    {
      m_impl->incrementRefCount();
    }
    
    template <class U>
    Alloc (const Alloc<U,AllocatorImpl,MemblockImpl>& src) throw(): 
      m_impl( 0 )
    {
      MemblockImpl::assign( src, m_impl );
      m_impl->incrementRefCount();
    }
    
    ~Alloc() throw() 
    {
      m_impl->decrementRefCount();
    }

    // return maximum number of elements that can be allocated
    size_type max_size () const throw() 
    {
      return std::numeric_limits<std::size_t>::max() / sizeof(T);
    }

    // allocate but don't initialize num elements of type T
    pointer allocate (size_type num, const void* = 0) 
    {
      return reinterpret_cast<pointer>( m_impl->allocate(num*sizeof(T)) );
    }

    // initialize elements of allocated storage p with value value
#if __cplusplus >= 201103L

    // use c++11 style forwarding to construct the object    
    template< typename P, typename... Args>
    void construct( P* obj, Args&&... args )
    {
      ::new((void*) obj ) P( std::forward<Args>( args )... );
    }

    template< typename P >
    void destroy( P* obj ) { obj->~P(); }
    
#else
    void construct (pointer p, const T& value) 
    {
      new((void*)p)T(value);
    }
    void destroy (pointer p) { p->~T(); }
#endif

    // deallocate storage p of deleted elements
    void deallocate (pointer p, size_type num) 
    {
      m_impl->deallocate( p );
    }
    
    bool equals( const MemblockImpl * impl ) const
    {
      return impl == m_impl;
    }
  
    bool operator == ( const Alloc& t2 ) const
    {
      return m_impl == t2.m_impl;
    }
  
    friend MemblockImpl;
    
    template< typename Other >
    bool operator == ( const Alloc< Other, AllocatorImpl, MemblockImpl >& t2 )
    {
      return t2.equals( m_impl );
    }
  
    template< typename Other >
    bool operator != ( const Alloc< Other, AllocatorImpl, MemblockImpl >& t2 )
    {
      return !t2.equals( m_impl );
    }
  
    // These are extension functions not required for an stl allocator
    size_t getNumAllocations() { return m_impl->getNumAllocations(); }
    size_t getNumDeallocations() { return m_impl->getNumDeallocations(); }
    size_t getNumBytesAllocated() { return m_impl->getNumBytesAllocated(); }    
  };
  
  template<typename A>
  template<typename T>
  void _memblockimpl<A>::assign( const Alloc<T,A, _memblockimpl<A> >& src, _memblockimpl<A> *& dest )
  {
    dest = const_cast<_memblockimpl<A>* >(src.m_impl);
  }
  
}

#endif