Pol  Revision:4b29d2b
fixalloc.h
Go to the documentation of this file.
1 
16 #ifndef __CLIB_FIXALLOC_H
17 #define __CLIB_FIXALLOC_H
18 
19 #include "pol_global_config.h"
20 
21 #include <assert.h>
22 #include <stddef.h>
23 #include <stdlib.h>
24 
25 #ifdef MEMORYLEAK
26 #include "logfacility.h"
27 #endif
28 namespace Pol
29 {
30 namespace Clib
31 {
32 template <size_t N, size_t B>
34 {
35 public:
36  union Buffer {
38  char data[N];
39  };
40  void* allocate();
41  void deallocate( void* );
42 
43  void* allocate( size_t size );
44  void deallocate( void* size, size_t n );
45 
46 #ifdef MEMORYLEAK
48  ~fixed_allocator();
49  void log_stuff( const std::string& detail );
50 #endif
51 
52 protected:
53  void* refill( void );
54 
55 private:
57 #ifdef MEMORYLEAK
58  int buffers;
59  int requests;
60  int max_requests;
61 #endif
62 };
63 
64 #ifdef MEMORYLEAK
65 template <size_t N, size_t B>
67 {
68  freelist_ = NULL;
69  buffers = 0;
70  requests = 0;
71  max_requests = 0;
72 };
73 
74 template <size_t N, size_t B>
76 {
77  log_stuff( "destructor" );
78 }
79 
80 template <size_t N, size_t B>
81 void fixed_allocator<N, B>::log_stuff( const std::string& detail )
82 {
83  DEBUGLOG << "fixed_allocator[" << detail << "]: " << buffers << " Buffer with "
84  << sizeof( Buffer[B] ) << " Bytes allocated [" << requests << " Requests of "
85  << max_requests << "]\n";
86 
87  LEAKLOG << buffers << ";" << sizeof( Buffer[B] ) << ";" << requests << ";" << max_requests << ";";
88 }
89 #endif
90 
91 template <size_t N, size_t B>
93 {
94 #ifdef LEAK_DEBUG
95  return ::operator new( N );
96 #endif
97 #ifdef MEMORYLEAK
98  requests++;
99  if ( max_requests < requests )
100  max_requests = requests;
101 #endif
102 
103  Buffer* p = freelist_;
104  if ( p != NULL )
105  {
106  freelist_ = p->next;
107  return p;
108  }
109  else
110  {
111  return refill();
112  }
113 }
114 
115 template <size_t N, size_t B>
117 {
118  size_t nbytes = sizeof( Buffer[B] );
119 
120  Buffer* morebuf = static_cast<Buffer*>(::operator new( nbytes ) );
121 
122 #ifdef MEMORYLEAK
123  buffers++;
124 #endif
125 
126  Buffer* walk = morebuf + 1;
127  int count = B - 2;
128  while ( count-- )
129  {
130  Buffer* next = walk + 1;
131  walk->next = next;
132  walk++;
133  }
134  walk->next = NULL;
135  freelist_ = morebuf + 1;
136  return morebuf;
137 }
138 
139 template <size_t N, size_t B>
141 {
142 #ifdef LEAK_DEBUG
143  return ::operator delete( vp );
144 #endif
145 #ifdef MEMORYLEAK
146  requests--;
147 #endif
148 
149  Buffer* buf = static_cast<Buffer*>( vp );
150  buf->next = freelist_;
151  freelist_ = buf;
152 }
153 
154 template <size_t N, size_t B>
156 {
157 #ifdef LEAK_DEBUG
158  return ::operator new( size );
159 #endif
160  assert( size == B );
161  if ( size == B )
162  return allocate();
163  else
164  return ::operator new( size );
165 }
166 
167 template <size_t N, size_t B>
168 void fixed_allocator<N, B>::deallocate( void* vp, size_t size )
169 {
170 #ifdef LEAK_DEBUG
171  return ::operator delete( vp );
172 #endif
173  assert( size == B );
174  if ( size == B )
175  deallocate( vp );
176  else
177  ::operator delete( vp );
178 }
179 }
180 }
181 #endif
void deallocate(void *)
Definition: fixalloc.h:140
#define LEAKLOG
Definition: logfacility.h:241
#define DEBUGLOG
Definition: logfacility.h:237
Definition: berror.cpp:12