CUBRID Engine  latest
memory_private_allocator.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 //
20 // memory_private_allocator.hpp - extension to memory_alloc.h private allocation
21 //
22 
23 #ifndef _MEMORY_PRIVATE_ALLOCATOR_HPP_
24 #define _MEMORY_PRIVATE_ALLOCATOR_HPP_
25 
26 #include "mem_block.hpp"
27 #include "memory_alloc.h"
28 
29 // forward definitions
30 namespace cubthread
31 {
32  class entry;
33 };
34 
35 namespace cubmem
36 {
37  /* private_allocator -
38  *
39  * Implementation of C++ allocator concept using CUBRID private allocator.
40  *
41  *
42  *
43  * Templates:
44  *
45  * T: base allocation type.
46  *
47  *
48  * How to use:
49  *
50  * Specialize template classes/functions that require dynamic memory allocation.
51  *
52  *
53  * note:
54  *
55  * 1. Cannot be used with typenames T that overload operator &
56  */
57  template <typename T>
59  {
60  public:
61  /* standard allocator type definitions */
62  typedef T value_type;
63  typedef value_type *pointer;
64  typedef const value_type *const_pointer;
65  typedef value_type &reference;
66  typedef const value_type &const_reference;
67  typedef size_t size_type;
68  typedef ptrdiff_t difference_type;
69 
70  /* convert an allocator<T> to allocator<U> */
71  template <typename U>
72  struct rebind
73  {
75  };
76 
77  inline explicit private_allocator (cubthread::entry *thread_p = NULL);
78  inline ~private_allocator ();
79  inline explicit private_allocator (const private_allocator &other);
80  template <typename U>
81  inline explicit private_allocator (const private_allocator<U> &other);
82 
83  /* address */
84  inline pointer address (reference r)
85  {
86  return &r;
87  }
88  inline const_pointer address (const_reference r)
89  {
90  return &r;
91  }
92 
93  /* memory allocation */
94  inline pointer allocate (size_type count);
95  inline void deallocate (pointer p, size_type ignored = 0);
96 
97  /* maximum number of allocations */
98  size_type max_size () const;
99 
100  /* construction/destruction */
101  inline void construct (pointer p, const_reference t);
102  inline void destroy (pointer p);
103 
104  /* db_private_alloc accessors */
105  cubthread::entry *get_thread_entry () const;
106  HL_HEAPID get_heapid () const;
107 
108  private:
109 
111  HL_HEAPID m_heapid;
112  };
113 
114  /* interchangeable specializations */
115  template <typename T, typename U>
116  bool
118  {
119  return true;
120  }
121 
122  template <typename T, typename U>
123  bool
125  {
126  return false;
127  }
128 
129  // private allocator helper functions
130  HL_HEAPID get_private_heapid (cubthread::entry *&thread_p);
131  void *private_heap_allocate (cubthread::entry *thread_p, HL_HEAPID heapid, size_t size);
132  void private_heap_deallocate (cubthread::entry *thread_p, HL_HEAPID heapid, void *ptr);
135 
136  // private_pointer_deleter - deleter for pointer allocated with private allocator
137  //
138  template <class T>
140  {
141  public:
144 
145  inline void operator () (T *ptr) const;
146 
147  private:
149  };
150 
151  // private_unique_ptr - unique pointer allocated with private allocator
152  //
153  template <class T>
155  {
156  public:
157  private_unique_ptr (T *ptr, cubthread::entry *thread_p);
158 
159  inline T *get () const; // get and keep ownership
160  inline T *release (); // get and release ownership
161 
162  inline void swap (private_unique_ptr <T> &other);
163 
164  inline void reset (T *ptr);
165 
166  inline T *operator-> () const;
167  inline T &operator* () const;
168 
169  private:
170  std::unique_ptr<T, private_pointer_deleter<T>> m_smart_ptr;
171  };
172 
174 
175  // Calls func with the global heap set for allocating memory
176  template <typename Func, typename ...Args>
177  inline void switch_to_global_allocator_and_call (Func &&func, Args &&... args);
178 
179 } // namespace cubmem
180 
182 //
183 // inline/template implementation
184 //
186 
187 namespace cubmem
188 {
189  //
190  // private_allocator
191  //
192  template <typename T>
194  : m_thread_p (thread_p)
195  {
198  }
199 
200  template <typename T>
202  {
203  m_thread_p = other.m_thread_p;
204  m_heapid = other.m_heapid;
206  }
207 
208  template <typename T>
209  template <typename U>
211  {
212  m_thread_p = other.get_thread_entry ();
213  m_heapid = other.get_heapid ();
215  }
216 
217  template <typename T>
219  {
221  }
222 
223  template <typename T>
226  {
227  return reinterpret_cast<T *> (private_heap_allocate (m_thread_p, m_heapid, count * sizeof (T)));
228  }
229 
230  template <typename T>
231  void
233  {
234  (void) ignored; // unused
236  }
237 
238  template <typename T>
241  {
242  const size_type DB_PRIVATE_ALLOCATOR_MAX_SIZE = 0x7FFFFFFF;
243  return DB_PRIVATE_ALLOCATOR_MAX_SIZE / sizeof (T);
244  }
245 
246  template <typename T>
247  void
249  {
250  new (p) value_type (t);
251  }
252 
253  template <typename T>
254  void
256  {
257  p->~value_type ();
258  }
259 
260  template <typename T>
263  {
264  return m_thread_p;
265  }
266 
267  template <typename T>
268  HL_HEAPID
270  {
271  return m_heapid;
272  }
273 
274  //
275  // private_pointer_deleter
276  //
277  template <class T>
279  : thread_p (NULL)
280  {
281  }
282 
283  template <class T>
285  : thread_p (thread_p)
286  {
287  }
288 
289  template <class T>
290  void
292  {
293  if (ptr != NULL)
294  {
295  db_private_free (thread_p, ptr);
296  }
297  }
298 
299  //
300  // private_unique_ptr
301  //
302  template <class T>
304  {
305  m_smart_ptr = std::unique_ptr<T, private_pointer_deleter<T>> (ptr, private_pointer_deleter<T> (thread_p));
306  }
307 
308  template <class T>
309  T *
311  {
312  return m_smart_ptr.get ();
313  }
314  template <class T>
315  T *
317  {
318  return m_smart_ptr.release ();
319  }
320 
321  template <class T>
322  void
324  {
325  m_smart_ptr.swap (other.m_smart_ptr);
326  }
327 
328  template <class T>
329  void
331  {
332  m_smart_ptr.reset (ptr);
333  }
334 
335  template <class T>
336  T *
338  {
339  return m_smart_ptr.get ();
340  }
341 
342  template <class T>
343  T &
345  {
346  return *m_smart_ptr.get ();
347  }
348 
349  template < typename Func, typename...Args >
350  void
351  switch_to_global_allocator_and_call (Func &&func, Args &&... args)
352  {
353  HL_HEAPID save_id;
354 
355  // Switch to global
356  save_id = db_change_private_heap (NULL, 0);
357 
358  func (std::forward < Args > (args)...);
359 
360  // Switch back
361  (void) db_change_private_heap (NULL, save_id);
362  }
363 } // namespace cubmem
364 
365 
366 
367 #endif // _MEMORY_PRIVATE_ALLOCATOR_HPP_
void deregister_private_allocator(cubthread::entry *thread_p)
std::unique_ptr< T, private_pointer_deleter< T > > m_smart_ptr
cubthread::entry * get_thread_entry() const
void private_heap_deallocate(cubthread::entry *thread_p, HL_HEAPID heapid, void *ptr)
void construct(pointer p, const_reference t)
bool operator==(const private_allocator< T > &, const private_allocator< U > &)
void * private_heap_allocate(cubthread::entry *thread_p, HL_HEAPID heapid, size_t size)
const block_allocator PRIVATE_BLOCK_ALLOCATOR
HL_HEAPID db_change_private_heap(THREAD_ENTRY *thread_p, HL_HEAPID heap_id)
Definition: memory_alloc.c:337
void switch_to_global_allocator_and_call(Func &&func, Args &&...args)
bool operator!=(const private_allocator< T > &, const private_allocator< U > &)
pointer allocate(size_type count)
private_allocator(cubthread::entry *thread_p=NULL)
#define NULL
Definition: freelistheap.h:34
#define db_private_free(thrd, ptr)
Definition: memory_alloc.h:229
HL_HEAPID get_private_heapid(cubthread::entry *&thread_p)
void register_private_allocator(cubthread::entry *thread_p)
int count(int &result, const cub_regex_object &reg, const std::string &src, const int position, const INTL_CODESET codeset)
void deallocate(pointer p, size_type ignored=0)
void swap(private_unique_ptr< T > &other)
private_unique_ptr(T *ptr, cubthread::entry *thread_p)
const char ** p
Definition: dynamic_load.c:945
const_pointer address(const_reference r)