Pol
Revision:cb584c9
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
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>
33
class
fixed_allocator
34
{
35
public
:
36
union
Buffer
{
37
Buffer
*
next
;
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
47
fixed_allocator
();
48
~
fixed_allocator
();
49
void
log_stuff(
const
std::string& detail );
50
#endif
51
52
protected
:
53
void
*
refill
(
void
);
54
55
private
:
56
Buffer
*
freelist_
;
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>
66
fixed_allocator<N, B>::fixed_allocator
()
67
{
68
freelist_
= NULL;
69
buffers = 0;
70
requests = 0;
71
max_requests = 0;
72
};
73
74
template
<
size_t
N,
size_t
B>
75
fixed_allocator<N, B>::~fixed_allocator
()
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>
92
void
*
fixed_allocator<N, B>::allocate
()
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>
116
void
*
fixed_allocator<N, B>::refill
()
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>
140
void
fixed_allocator<N, B>::deallocate
(
void
* vp )
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>
155
void
*
fixed_allocator<N, B>::allocate
(
size_t
size )
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
Pol::Clib::fixed_allocator::Buffer
Definition:
fixalloc.h:36
Pol::Clib::fixed_allocator::deallocate
void deallocate(void *)
Definition:
fixalloc.h:140
logfacility.h
Pol::Clib::fixed_allocator::freelist_
Buffer * freelist_
Definition:
fixalloc.h:56
Pol::Clib::fixed_allocator::refill
void * refill(void)
Definition:
fixalloc.h:116
Pol::Clib::fixed_allocator::Buffer::next
Buffer * next
Definition:
fixalloc.h:37
Pol::Clib::fixed_allocator::allocate
void * allocate()
Definition:
fixalloc.h:92
LEAKLOG
#define LEAKLOG
Definition:
logfacility.h:241
DEBUGLOG
#define DEBUGLOG
Definition:
logfacility.h:237
Pol::Clib::fixed_allocator
Definition:
fixalloc.h:33
Pol::Clib::fixed_allocator::Buffer::data
char data[N]
Definition:
fixalloc.h:38
Pol
Definition:
berror.cpp:12
clib
fixalloc.h
Generated on Wed Oct 10 2018 02:41:48 for Pol by
1.8.11