0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Allocator.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Hypertable. If not, see <http://www.gnu.org/licenses/>
18  */
19 
26 #ifndef HYPERTABLE_ALLOCATOR_H
27 #define HYPERTABLE_ALLOCATOR_H
28 
29 #include "Error.h"
30 
31 #include <utility>
32 
33 namespace Hypertable {
34 
46 inline void destruct(char *) {}
47 inline void destruct(unsigned char *) {}
48 inline void destruct(short *) {}
49 inline void destruct(unsigned short *) {}
50 inline void destruct(int *) {}
51 inline void destruct(unsigned int *) {}
52 inline void destruct(long *) {}
53 inline void destruct(unsigned long *) {}
54 inline void destruct(long long *) {}
55 inline void destruct(unsigned long long *) {}
56 inline void destruct(float *) {}
57 inline void destruct(double *) {}
58 inline void destruct(long double *) {}
59 
60 template <typename T>
61 inline void destruct(T *p) { p->~T(); }
62 
63 
66 inline size_t get_align_offset(void *p) {
67  if (sizeof(void *) == 8)
68  return 8 - (((uint64_t)p) & 0x7);
69 
70  return 4 - (((uint64_t)p) & 0x3);
71 }
72 
73 
78 template <typename T>
79 struct AllocatorBase {
80  typedef size_t size_type;
81  typedef ptrdiff_t difference_type;
82  typedef T* pointer;
83  typedef const T* const_pointer;
84  typedef T& reference;
85  typedef const T& const_reference;
86  typedef T value_type;
87 
93  pointer address(reference x) const { return &x; }
94 
100  const_pointer address(const_reference x) const { return &x; }
101 
102  // implement the following 2 methods
103  // pointer allocate(size_type sz);
104  // void deallocate(pointer p, size_type n);
105 
109  size_type max_size() const throw() {
110  return size_t(-1) / sizeof(value_type);
111  }
112 
120  template< class U, class... Args >
121  void construct( U* p, Args&&... args ) {
122  ::new((void *)p) U(std::forward<Args>(args)...);
123  }
124 
131  void construct(pointer p) {
132  new(static_cast<void*>(p)) T();
133  }
134 
136  void destroy(pointer p) { destruct(p); }
137 };
138 
139 
144 template <>
145 struct AllocatorBase<void> {
146  typedef size_t size_type;
147  typedef ptrdiff_t difference_type;
148  typedef void *pointer;
149  typedef const void *const_pointer;
150  // reference to void members are impossible.
151  typedef void value_type;
152 };
153 
158 template <typename T, class ArenaT>
161 
162  ArenaT *m_arenap;
163 
164  public:
165  typedef typename Base::pointer pointer;
166  typedef typename Base::size_type size_type;
167 
169  ArenaAllocatorBase() : m_arenap(NULL) {}
170 
172  ArenaAllocatorBase(ArenaT &arena) { m_arenap = &arena; }
173 
175  template <typename U>
177  : m_arenap(copy.m_arenap) {}
178 
179  // arena allocators only needs to implement
180  // pointer allocate(size_type sz);
181 
188  inline void check_allocate_size(size_type sz) const {
189  if (HT_UNLIKELY(sz > Base::max_size()))
191  }
192 
199  inline pointer default_allocate(size_type sz) {
200  return (pointer)(::operator new(sz * sizeof(T)));
201  }
202 
210  void deallocate(pointer p, size_type sz) { if (!m_arenap) ::operator delete(p); }
211 
216  template <typename U>
218  return m_arenap == x.m_arenap;
219  }
220 
225  template <typename U>
227  return m_arenap != x.m_arenap;
228  }
229 
235  template <typename U>
237  ArenaT *tmp = m_arenap;
238  m_arenap = other.m_arenap;
239  other.m_arenap = tmp;
240  }
241 
246  ArenaT *arena() const { return m_arenap; }
247 
252  void set_arena(ArenaT *arena) { m_arenap = arena; }
253 };
254 
257 } // namespace Hypertable
258 
259 #endif /* HYPERTABLE_ALLOCATOR_H */
void construct(U *p, Args &&...args)
Creates a new object instance using placement new and the copy constructor.
Definition: Allocator.h:121
Specialized Allocator class using a Memory Arena which manages the allocated memory; see PageArenaAll...
Definition: Allocator.h:159
Base classes for all Allocator classes.
Definition: Allocator.h:79
void deallocate(pointer p, size_type sz)
Deletes allocated objects unless an Arena Allocator was used (in this case the function is a nop) ...
Definition: Allocator.h:210
size_t get_align_offset(void *p)
Convenience function returning a size aligned to 8 or 4 bytes, depending on the system's architecture...
Definition: Allocator.h:66
pointer default_allocate(size_type sz)
Allocate an array of objects using the regular operator new.
Definition: Allocator.h:199
size_type max_size() const
Returns the maximum number of objects that this Allocator can allocate; used to check for bad allocat...
Definition: Allocator.h:109
AllocatorBase< T > Base
Definition: Allocator.h:160
ArenaAllocatorBase()
Default constructor; creates an empty object.
Definition: Allocator.h:169
bool operator!=(const ArenaAllocatorBase< U, ArenaT > &x) const
operator != returns true if the arena pointers are not equal
Definition: Allocator.h:226
void check_allocate_size(size_type sz) const
Sanity check the size of the requested buffer; if it's too large then most likely this is a bug in th...
Definition: Allocator.h:188
void swap(ArenaAllocatorBase< U, ArenaT > &other)
Swaps the memory arena from another allocator with the arena of 'this' allocator. ...
Definition: Allocator.h:236
ArenaAllocatorBase(ArenaT &arena)
Constructor; takes ownership of a pointer to a memory arena.
Definition: Allocator.h:172
#define HT_THROW_(_code_)
Definition: Error.h:481
Hypertable definitions
void copy(TableDumper &, CellsBuilder &)
Definition: TableDumper.cc:129
ArenaT * arena() const
Returns a pointer to the Arena.
Definition: Allocator.h:246
#define HT_UNLIKELY(x)
Definition: compat-c.h:70
bool operator==(const ArenaAllocatorBase< U, ArenaT > &x) const
operator == returns true if the arena pointers are equal
Definition: Allocator.h:217
const_pointer address(const_reference x) const
Returns a const pointer to an object of type T.
Definition: Allocator.h:100
ArenaAllocatorBase(const ArenaAllocatorBase< U, ArenaT > &copy)
Copy Constructor; copies the memory arena.
Definition: Allocator.h:176
void destroy(pointer p)
Calls the destructor of an object (unless it's a POD)
Definition: Allocator.h:136
Error codes, Exception handling, error logging.
void destruct(char *)
helper functions which call the destructor of an object; destructor is not called on POD types (using...
Definition: Allocator.h:46
void construct(pointer p)
Creates a new object instance using placement new and the default constructor.
Definition: Allocator.h:131
pointer address(reference x) const
Returns a pointer to an object of type T.
Definition: Allocator.h:93
void set_arena(ArenaT *arena)
Sets the Arena pointer.
Definition: Allocator.h:252