Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
Allocator.hxx
Go to the documentation of this file.
1
35#ifndef _UTILS_ALLOCATOR_HXX_
36#define _UTILS_ALLOCATOR_HXX_
37
38#include "utils/macros.h"
39
46template <typename T> class Allocator
47{
48public:
52 {
53 T data;
55 };
56
59
61 bool init;
62
64 size_t entries;
65
66 typedef T value_type;
68 typedef const value_type* const_pointer;
70 typedef const value_type& const_reference;
71 typedef std::size_t size_type;
72 typedef std::ptrdiff_t difference_type;
75 template <typename U> struct rebind
76 {
79 };
80
84 explicit Allocator(size_t e)
85 : freeList(NULL),
86 init(false),
87 entries(e)
88 {
89 }
90
94 explicit Allocator(Allocator const& a)
95 : freeList(a.freeList),
96 init(a.init),
98 {
99 }
100
104 {
105 }
106
110 template <typename U> Allocator(Allocator<U> const& o)
111 : freeList(NULL),
112 init(o.init),
114 {
115 }
116
121 T *address(T &r)
122 {
123 return &r;
124 }
125
130 const T *address(const T &r)
131 {
132 return &r;
133 }
134
139 T *allocate(size_t cnt, const void* = 0)
140 {
141 if (init == false)
142 {
143 HASSERT(entries != 0);
144 init = true;
145 FreeList *newList = (FreeList*)malloc(sizeof(FreeList) * max_size());
146 for (size_t i = 0; i < max_size(); ++i)
147 {
148 newList[i].next = freeList;
149 freeList = newList + i;
150 }
151 }
152 HASSERT(freeList != NULL);
153 HASSERT(cnt == 1);
154
155 T *newT = &(freeList->data);
157 return newT;
158 }
159
164 void deallocate(T *p, size_t n)
165 {
166 HASSERT(n == 1);
167 FreeList *pFreeList = (FreeList*)p;
168 pFreeList->next = freeList;
169 freeList = pFreeList;
170 }
171
174 size_t max_size() const
175 {
176 return entries;
177 }
178
179
184 void construct(T *p, const T& t)
185 {
186 new(p) T(t);
187 }
188
192 void destroy(T *p)
193 {
194 p->~T();
195 }
196
201 {
202 return true;
203 }
204
209 bool operator!=(Allocator const& a)
210 {
211 return !operator==(a);
212 }
213private:
217 : init(false),
218 entries(0)
219 {
220 }
221};
222
223#endif /* _UTILS_ALLOCATOR_HXX_ */
This is a custom allocator that limits the number of mappings.
Definition Allocator.hxx:47
Allocator(Allocator const &a)
Copy constructor.
Definition Allocator.hxx:94
void construct(T *p, const T &t)
Placement constructor.
T * allocate(size_t cnt, const void *=0)
Allocate item(s) out of the pool.
value_type & reference
reference required by stl
Definition Allocator.hxx:69
const T * address(const T &r)
Const address of item.
const value_type * const_pointer
const_pointer required by stl
Definition Allocator.hxx:68
T value_type
value_type required by stl
Definition Allocator.hxx:66
bool init
flag that tell us if we have initilized our selves or not
Definition Allocator.hxx:61
const value_type & const_reference
const_reference required by stl
Definition Allocator.hxx:70
std::size_t size_type
size_type required by stl
Definition Allocator.hxx:71
size_t max_size() const
Maximum number of items that can be allocated.
Allocator()
Default Constructor.
bool operator==(Allocator const &)
Overloaded operator ==.
Allocator(Allocator< U > const &o)
template copy constructor.
bool operator!=(Allocator const &a)
Overloaded operator !=.
Allocator(size_t e)
Constructor.
Definition Allocator.hxx:84
void deallocate(T *p, size_t n)
Free itme.
~Allocator()
Destructor.
value_type * pointer
pointer required by stl
Definition Allocator.hxx:67
void destroy(T *p)
Destruct.
FreeList * freeList
Start of free list.
Definition Allocator.hxx:58
T * address(T &r)
Address of item.
std::ptrdiff_t difference_type
difference_type required by stl
Definition Allocator.hxx:72
size_t entries
number of elements in the fixed size pool
Definition Allocator.hxx:64
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.
Definition macros.h:138
typedef for allocator specialization
Definition Allocator.hxx:76
Allocator< U > other
typedef for allocator specialization
Definition Allocator.hxx:78
List of unused elements.
Definition Allocator.hxx:52
T data
unused item
Definition Allocator.hxx:53
FreeList * next
next item in the list
Definition Allocator.hxx:54