Disk ARchive  2.5.5
Full featured and portable backup and archiving tool
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 #include "cygwin_adapt.hpp"
38 
39 
40 namespace libdar
41 {
42 
43 
46 
51 
52  class on_pool
53  {
54  public:
55 #ifdef LIBDAR_SPECIAL_ALLOC
56 
63  on_pool() { dynamic_init(); };
64 
69  on_pool(const on_pool & ref) { dynamic_init(); };
70 
75  const on_pool & operator = (const on_pool & ref) { return *this; };
76 
78  virtual ~on_pool() throw(Ebug) {};
79 #endif
80 
84  void *operator new(size_t n_byte) { void *ret = alloc(n_byte, nullptr); if(ret == nullptr) throw std::bad_alloc(); return ret; };
85 
86 
90  void *operator new(size_t n_byte, const std::nothrow_t & nothrow_value) { return alloc(n_byte, nullptr); };
91 
92 
96  void *operator new[](size_t n_byte) { void *ret = alloc(n_byte, nullptr); if(ret == nullptr) throw std::bad_alloc(); return ret; };
97 
98 
102  void *operator new[](size_t n_byte, const std::nothrow_t & nothrow_value) { return alloc(n_byte, nullptr); };
103 
108  void *operator new(size_t n_byte, memory_pool *p) { return alloc(n_byte, p); };
109 
110 
115  void *operator new[] (size_t n_byte, memory_pool *p) { return alloc(n_byte, p); };
116 
117 
119  void operator delete(void *ptr, memory_pool *p) { dealloc(ptr); };
120 
121 
123  void operator delete[](void *ptr, memory_pool *p) { dealloc(ptr); };
124 
125 
127  void operator delete(void *ptr) { dealloc(ptr); };
128 
129 
131  void operator delete[](void *ptr) { dealloc(ptr); };
132 
133  protected:
141 #ifdef LIBDAR_SPECIAL_ALLOC
142  memory_pool *get_pool() const { return dynamic ? (((pool_ptr *)this) - 1)->reserve : nullptr; };
143 #else
144  memory_pool *get_pool() const { return nullptr; };
145 #endif
146 
147  template <class T> void meta_new(T * & ptr, size_t num)
148  {
149 #ifdef LIBDAR_SPECIAL_ALLOC
150  size_t byte = num * sizeof(T);
151 
152  if(get_pool() != nullptr)
153  ptr = (T *)get_pool()->alloc(byte);
154  else
155  ptr = (T *)new (std::nothrow) char [byte];
156 #else
157  ptr = new (std::nothrow) T[num];
158 #endif
159  }
160 
161 
162 
163  template <class T> void meta_delete(T * ptr)
164  {
165 #ifdef LIBDAR_SPECIAL_ALLOC
166  if(get_pool() != nullptr)
167  get_pool()->release(ptr);
168  else
169  delete [] (char *)(ptr);
170 #else
171  delete [] ptr;
172 #endif
173  }
174 
175  private:
176 #ifdef LIBDAR_SPECIAL_ALLOC
177 
182  union pool_ptr
183  {
184  memory_pool *reserve; //< this is to be able to pass the pool object to the constructor if it requires dynamic memory allocation
185  U_I alignment__i; //< to properly align the allocated memory block that follows
186  U_32 alignment_32; //< to properly align the allocated memory block that follows
187  U_64 alignment_64; //< to properly align the allocated memory block that follows
188  };
189 
190  // this field is necessary to make distinction between object on the heap that have a pool_ptr header from those
191  // created as local or temporary variable (on the stack).
192  bool dynamic;
193 
195  void dynamic_init() const { const_cast<on_pool *>(this)->dynamic = (alloc_not_constructed == this); alloc_not_constructed = nullptr; };
196 #endif
197 
203  static void *alloc(size_t size, memory_pool *p);
204 
209  static void dealloc(void *ptr);
210 
211 #ifdef LIBDAR_SPECIAL_ALLOC
212 #ifdef CYGWIN_BUILD
213  static on_pool *alloc_not_constructed;
214  // under cygwin the thread_local directive does not work but
215  // as we build only command-line tools that are single threaded
216  // program, it does not hurt using global static field here
217  // well, as soon as another application than dar/dar_xform/...
218  // relies on a cygwin build of libdar, this trick should be changed
219 #else
220  thread_local static on_pool * alloc_not_constructed;
221 #endif
222 #endif
223  };
224 
226 
227 } // end of namespace
228 
229 #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:144
thin adaptation layer to Cygwin specifities
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 ...
libdar namespace encapsulate all libdar symbols
Definition: archive.hpp:47