Pyrogenesis  trunk
allocator_adapters.h
Go to the documentation of this file.
1 /* Copyright (c) 2011 Wildfire Games
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining
4  * a copy of this software and associated documentation files (the
5  * "Software"), to deal in the Software without restriction, including
6  * without limitation the rights to use, copy, modify, merge, publish,
7  * distribute, sublicense, and/or sell copies of the Software, and to
8  * permit persons to whom the Software is furnished to do so, subject to
9  * the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 /*
24  * adapters for allocators; provides a minimal subset of the
25  * STL allocator interface.
26  */
27 
28 #ifndef ALLOCATOR_ADAPTERS
29 #define ALLOCATOR_ADAPTERS
30 
31 #include <memory>
32 
33 #include "lib/sysdep/rtl.h"
34 #include "lib/sysdep/vm.h"
35 
36 // NB: STL allocators are parameterized on the object type and indicate
37 // the number of elements to [de]allocate. however, these adapters are
38 // only used for allocating storage and receive the number of bytes.
39 
41 {
42  void* allocate(size_t size)
43  {
44  return malloc(size);
45  }
46 
47  void deallocate(void* p, size_t UNUSED(size))
48  {
49  return free(p);
50  }
51 };
52 
53 template<size_t alignment = allocationAlignment>
55 {
56  void* allocate(size_t size)
57  {
58  return rtl_AllocateAligned(size, alignment);
59  }
60 
61  void deallocate(void* p, size_t UNUSED(size))
62  {
63  return rtl_FreeAligned(p);
64  }
65 };
66 
67 template<vm::PageType pageType = vm::kDefault, int prot = PROT_READ|PROT_WRITE>
69 {
70  void* allocate(size_t size)
71  {
72  return vm::Allocate(size, pageType, prot);
73  }
74 
75  void deallocate(void* p, size_t size)
76  {
77  vm::Free(p, size);
78  }
79 };
80 
81 template<size_t commitSize = largePageSize, vm::PageType pageType = vm::kDefault, int prot = PROT_READ|PROT_WRITE>
83 {
84  void* allocate(size_t size)
85  {
86  return vm::ReserveAddressSpace(size, commitSize, pageType, prot);
87  }
88 
89  void deallocate(void* p, size_t size)
90  {
91  vm::ReleaseAddressSpace(p, size);
92  }
93 };
94 
95 
96 /**
97  * fully STL-compatible allocator that simply draws upon another Allocator.
98  * this allows a single allocator to serve multiple STL containers.
99  */
100 template<typename T, class Allocator>
102 {
103 public:
104  typedef T value_type;
105  typedef T* pointer;
106  typedef const T* const_pointer;
107  typedef T& reference;
108  typedef const T& const_reference;
109  typedef std::size_t size_type;
110  typedef std::ptrdiff_t difference_type;
111 
112  template<class U>
113  struct rebind
114  {
116  };
117 
118  // (required to be declared by boost::unordered_map, but should never be called)
119  explicit NOTHROW_DEFINE ProxyAllocator();
120 
122  : allocator(&allocator)
123  {
124  }
125 
126  template<typename U, class A>
128  : allocator(rhs.allocator)
129  {
130  }
131 
132  // (required by VC2010 std::vector)
133  bool operator==(const ProxyAllocator& rhs) const
134  {
135  return allocator == rhs.allocator;
136  }
137  bool operator!=(const ProxyAllocator& rhs) const
138  {
139  return !operator==(rhs);
140  }
141 
142  pointer address(reference r)
143  {
144  return &r;
145  }
146 
147  const_pointer address(const_reference s)
148  {
149  return &s;
150  }
151 
152  size_type max_size() const throw ()
153  {
154  return std::numeric_limits<std::size_t>::max() / sizeof(T);
155  }
156 
157  void construct(const pointer ptr, const value_type& t)
158  {
159  new(ptr) T(t);
160  }
161 
162  void destroy(pointer ptr)
163  {
164  ptr->~T();
165  UNUSED2(ptr); // silence MSVC warnings
166  }
167 
168  pointer allocate(size_type n)
169  {
170  // safely handle zero-sized allocations (happens with GCC STL - see ticket #909).
171  if(n == 0)
172  n = 1;
173  return (pointer)allocator->allocate(n*sizeof(T));
174  }
175 
176  pointer allocate(size_type n, const void* const)
177  {
178  return allocate(n);
179  }
180 
181  void deallocate(const pointer ptr, const size_type n)
182  {
183  return allocator->deallocate(ptr, n*sizeof(T));
184  }
185 
186 //private: // otherwise copy ctor cannot access it
188 };
189 
190 #endif // #ifndef ALLOCATOR_ADAPTERS
void * rtl_AllocateAligned(size_t size, size_t align)
Definition: gcc.cpp:66
std::size_t size_type
Definition: allocator_adapters.h:109
pointer allocate(size_type n)
Definition: allocator_adapters.h:168
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning. ...
Definition: code_annotation.h:38
void deallocate(void *p, size_t size)
Definition: allocator_adapters.h:75
bool operator==(const ProxyAllocator &rhs) const
Definition: allocator_adapters.h:133
Definition: allocator_adapters.h:68
Definition: file_cache.cpp:91
bool operator==(const FCDJointWeightPair &a, const FCDJointWeightPair &b)
Definition: GeomReindex.cpp:59
#define NOTHROW_DEFINE
Definition: code_annotation.h:86
T * pointer
Definition: allocator_adapters.h:105
const T & const_reference
Definition: allocator_adapters.h:108
void deallocate(void *p, size_t size)
Definition: allocator_adapters.h:89
void * allocate(size_t size)
Definition: allocator_adapters.h:42
Definition: allocator_adapters.h:54
Allocator * allocator
Definition: allocator_adapters.h:187
#define UNUSED2(param)
mark a function local variable or parameter as unused and avoid the corresponding compiler warning...
Definition: code_annotation.h:56
pointer allocate(size_type n, const void *const)
Definition: allocator_adapters.h:176
Definition: allocator_adapters.h:113
T value_type
Definition: allocator_adapters.h:104
void * allocate(size_t size)
Definition: allocator_adapters.h:84
void construct(const pointer ptr, const value_type &t)
Definition: allocator_adapters.h:157
void deallocate(void *p, size_t size)
Definition: allocator_adapters.h:61
size_type max_size() const
Definition: allocator_adapters.h:152
void * allocate(size_t size)
Definition: allocator_adapters.h:70
void deallocate(void *p, size_t size)
Definition: allocator_adapters.h:47
#define T(string_literal)
Definition: secure_crt.cpp:76
const T * const_pointer
Definition: allocator_adapters.h:106
void deallocate(const pointer ptr, const size_type n)
Definition: allocator_adapters.h:181
std::ptrdiff_t difference_type
Definition: allocator_adapters.h:110
void ReleaseAddressSpace(void *p, size_t size)
release address space and decommit any memory.
Definition: uvm.cpp:49
fully STL-compatible allocator that simply draws upon another Allocator.
Definition: allocator_adapters.h:101
void * allocate(size_t size)
Definition: allocator_adapters.h:56
void * Allocate(size_t size, PageType pageType, int prot)
reserve address space and commit memory.
Definition: uvm.cpp:98
void rtl_FreeAligned(void *alignedPointer)
Definition: gcc.cpp:93
T & reference
Definition: allocator_adapters.h:107
NOTHROW_DEFINE ProxyAllocator(const ProxyAllocator< U, A > &rhs)
Definition: allocator_adapters.h:127
void * ReserveAddressSpace(size_t size, size_t commitSize, PageType pageType, int prot)
reserve address space and set the parameters for any later on-demand commits.
Definition: uvm.cpp:40
bool operator!=(const ProxyAllocator &rhs) const
Definition: allocator_adapters.h:137
NOTHROW_DEFINE ProxyAllocator(Allocator &allocator)
Definition: allocator_adapters.h:121
pointer address(reference r)
Definition: allocator_adapters.h:142
void Free(void *p, size_t size)
decommit memory and release address space.
Definition: uvm.cpp:113
const_pointer address(const_reference s)
Definition: allocator_adapters.h:147
void destroy(pointer ptr)
Definition: allocator_adapters.h:162
ProxyAllocator< U, Allocator > other
Definition: allocator_adapters.h:115
Definition: allocator_adapters.h:40
Definition: allocator_adapters.h:82