Disk ARchive  2.5.0
Full featured and portable backup and archiving tool
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
on_pool.hpp
Go to the documentation of this file.
1 /*********************************************************************/
2 // dar - disk archive - a backup/restoration program
3 // Copyright (C) 2002-2052 Denis Corbin
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 //
19 // to contact the author : http://dar.linux.free.fr/email.html
20 /*********************************************************************/
21 
25 
26 
27 
28 #ifndef ON_POOL_HPP
29 #define ON_POOL_HPP
30 
31 #include "../my_config.h"
32 #include <vector>
33 #include <new>
34 #include "integers.hpp"
35 #include "memory_pool.hpp"
36 #include "mem_allocator.hpp"
37 
38 
39 namespace libdar
40 {
41 
42 
45 
50 
51  class on_pool
52  {
53  public:
54 #ifdef LIBDAR_SPECIAL_ALLOC
55 
62  on_pool() { dynamic_init(); };
63 
68  on_pool(const on_pool & ref) { dynamic_init(); };
69 
74  const on_pool & operator = (const on_pool & ref) { return *this; };
75 
77  virtual ~on_pool() throw(Ebug) {};
78 #endif
79 
83  void *operator new(size_t n_byte) { void *ret = alloc(n_byte, nullptr); if(ret == nullptr) throw std::bad_alloc(); return ret; };
84 
85 
89  void *operator new(size_t n_byte, const std::nothrow_t & nothrow_value) { return alloc(n_byte, nullptr); };
90 
91 
95  void *operator new[](size_t n_byte) { void *ret = alloc(n_byte, nullptr); if(ret == nullptr) throw std::bad_alloc(); return ret; };
96 
97 
101  void *operator new[](size_t n_byte, const std::nothrow_t & nothrow_value) { return alloc(n_byte, nullptr); };
102 
107  void *operator new(size_t n_byte, memory_pool *p) { return alloc(n_byte, p); };
108 
109 
114  void *operator new[] (size_t n_byte, memory_pool *p) { return alloc(n_byte, p); };
115 
116 
118  void operator delete(void *ptr, memory_pool *p) { dealloc(ptr); };
119 
120 
122  void operator delete[](void *ptr, memory_pool *p) { dealloc(ptr); };
123 
124 
126  void operator delete(void *ptr) { dealloc(ptr); };
127 
128 
130  void operator delete[](void *ptr) { dealloc(ptr); };
131 
132  protected:
140 #ifdef LIBDAR_SPECIAL_ALLOC
141  memory_pool *get_pool() const { return dynamic ? (((pool_ptr *)this) - 1)->reserve : nullptr; };
142 #else
143  memory_pool *get_pool() const { return nullptr; };
144 #endif
145 
146  template <class T> void meta_new(T * & ptr, size_t num)
147  {
148 #ifdef LIBDAR_SPECIAL_ALLOC
149  size_t byte = num * sizeof(T);
150 
151  if(get_pool() != nullptr)
152  ptr = (T *)get_pool()->alloc(byte);
153  else
154  ptr = (T *)new (std::nothrow) char [byte];
155 #else
156  ptr = new (std::nothrow) T[num];
157 #endif
158  }
159 
160 
161 
162  template <class T> void meta_delete(T * ptr)
163  {
164 #ifdef LIBDAR_SPECIAL_ALLOC
165  if(get_pool() != nullptr)
166  get_pool()->release(ptr);
167  else
168  delete [] (char *)(ptr);
169 #else
170  delete [] ptr;
171 #endif
172  }
173 
174  private:
175 #ifdef LIBDAR_SPECIAL_ALLOC
176 
181  union pool_ptr
182  {
183  memory_pool *reserve; //< this is to be able to pass the pool object to the constructor if it requires dynamic memory allocation
184  U_I alignment__i; //< to properly align the allocated memory block that follows
185  U_32 alignment_32; //< to properly align the allocated memory block that follows
186  U_64 alignment_64; //< to properly align the allocated memory block that follows
187  };
188 
189  // this field is necessary to make distinction between object on the head that have a pool_ptr header from those
190  // created as local or temporary variable (on the stack).
191  bool dynamic;
192 
194  void dynamic_init() const { const_cast<on_pool *>(this)->dynamic = (alloc_not_constructed == this); alloc_not_constructed = nullptr; };
195 #endif
196 
202  static void *alloc(size_t size, memory_pool *p);
203 
208  static void dealloc(void *ptr);
209 
210 #ifdef LIBDAR_SPECIAL_ALLOC
211  thread_local static on_pool * alloc_not_constructed;
212 #endif
213  };
214 
216 
217 } // end of namespace
218 
219 #endif
are defined here basic integer types that tend to be portable
class memory_pool allocates and recycles blocks of memory for better performances it is expected to b...
memory_pool * get_pool() const
Definition: on_pool.hpp:143
exception used to signal a bug. A bug is triggered when reaching some code that should never be reach...
Definition: erreurs.hpp:137
this is the base class of object that can provide dynamically allocated memory blocks ...