Code::Blocks  SVN r11506
blockallocated.h
Go to the documentation of this file.
1 /*
2  * This file is part of the Code::Blocks IDE and licensed under the GNU Lesser General Public License, version 3
3  * http://www.gnu.org/licenses/lgpl-3.0.html
4  */
5 
6 #ifndef __BLOCKALLOC_H__
7 #define __BLOCKALLOC_H__
8 
9 #undef new
10 
11 #include <vector>
12 #include <wx/file.h>
13 #include <wx/string.h>
14 #include <typeinfo>
15 #include "globals.h"
16 #include "prep.h"
17 
18 namespace BlkAllc
19 {
20  void DebugLog(wxString cn, int blockSize, int poolSize, int max_refs, int total_refs, int ref_count);
21 
22  const bool enable_global_debug = false;
23  const bool verbose = false;
24 }
25 
26 
27 template <class T, unsigned int pool_size, const bool debug>
29 {
30  template <class U>
32  {
34  char data[sizeof(U)];
35  };
36 
37  std::vector<LinkedBlock<T>*> allocBlocks;
38 
40  int ref_count;
41  int max_refs;
43 
45  {
46  LinkedBlock<T> *ptr = new LinkedBlock<T>[pool_size];
47 
48  allocBlocks.push_back(ptr);
49 
50  for(unsigned int i = 0; i < pool_size - 1; ++i)
51  ptr[i].next = &ptr[i+1];
52 
53  ptr[pool_size - 1].next = 0;
54 
55  first = ptr;
56  };
57 
58 
60  {
61  p->next = first;
62  first = p;
63  };
64 
65 public:
66 
67  BlockAllocator() : first(0), ref_count(0), max_refs(0), total_refs(0)
68  {
69  #if defined(__GNUC__)
70  assert(__builtin_constant_p(debug));
71  #endif
72  };
73 
75  {
76  if(debug)
77  BlkAllc::DebugLog(cbC2U(typeid(T).name()), allocBlocks.size(), pool_size, max_refs, total_refs, ref_count);
78  else if(BlkAllc::enable_global_debug && (BlkAllc::verbose || ref_count != 0))
79  BlkAllc::DebugLog(cbC2U(typeid(T).name()), allocBlocks.size(), pool_size, max_refs, total_refs, ref_count);
80 
81  for(unsigned int i = 0; i < allocBlocks.size(); ++i)
82  delete[] allocBlocks[i];
83  };
84 
85  inline void* New()
86  {
87  if(BlkAllc::enable_global_debug || debug)
88  {
89  ++ref_count;
90  ++total_refs;
91  max_refs = ref_count > max_refs ? ref_count : max_refs;
92  }
93 
94  if(first == 0)
95  AllocBlockPushBack();
96 
97  void *p = first;
98  first = first->next;
99  return p;
100  };
101 
102  inline void Delete(void *ptr)
103  {
104  if(BlkAllc::enable_global_debug || debug)
105  --ref_count;
106 
107  PushFront((LinkedBlock<T> *) ptr);
108  };
109 };
110 
111 
112 template <class T, unsigned int pool_size, const bool debug = 0>
114 {
116 
117 public:
118 
119  inline void* operator new(cb_unused size_t size)
120  {
121  return allocator.New();
122  };
123 
124  inline void operator delete(void *ptr)
125  {
126  if(ptr == 0) // C++ standard requires this
127  return;
128  allocator.Delete(ptr);
129  };
130 };
131 template<class T, unsigned int pool_size, const bool debug>
133 
134 #endif
const bool verbose
void DebugLog(wxString cn, int blockSize, int poolSize, int max_refs, int total_refs, int ref_count)
DLLIMPORT wxString cbC2U(const char *str)
Return str as a proper unicode-compatible string.
Definition: globals.cpp:733
std::vector< LinkedBlock< T > * > allocBlocks
const bool enable_global_debug
void AllocBlockPushBack()
LinkedBlock< T > * first
LinkedBlock< U > * next
static BlockAllocator< T, pool_size, debug > allocator
const wxString ptr(_T("*"))
void Delete(void *ptr)
void PushFront(LinkedBlock< T > *p)