Lhogho
0.0.028
Main Page
Related Pages
Data Structures
Files
File List
Globals
All
Data Structures
Files
Functions
Variables
Typedefs
Macros
Pages
pools.c
Go to the documentation of this file.
1
//
2
// Project: Lhogho
3
// File: pools.c
4
//
5
// Copyright (C) 2007 P.Boytchev
6
//
7
// Revision history:
8
// 2006-09-29 - Pools module created
9
// 2006-09-30 - Separated free atoms from blocks
10
// 2006-10-10 - More detailed statistics
11
// 2007-02-27 - Module renamed from \b Pool to \b Pools
12
// 2007-05-17 - Added license info
13
// 2007-05-21 - Added doxygen-friendly documentation
14
// 2007-06-02 - STATISTICS merged into ADVANCED
15
// 2007-06-05 - Simplified interface
16
// 2007-06-05 - definitions spread to where they belong
17
// 2007-06-13 - fixed bug #1736021 "Alloc/dealloc statistics"
18
// 2007-10-09 - fixed bug #1806186 Atoms no returned to the pool
19
// 2007-12-12 - dump_pool()
20
// 2008-01-18 - data_pool_ex
21
// 2008-01-23 - Fixed bug #1836433 Example with IFTRUE fails
22
//
23
//
24
// This program is free software; you can redistribute it and/or modify
25
// it under the terms of the GNU General Public License as published by
26
// the Free Software Foundation; either version 2 of the License, or
27
// (at your option) any later version.
28
//
29
// This program is distributed in the hope that it will be useful,
30
// but WITHOUT ANY WARRANTY; without even the implied warranty of
31
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32
// GNU General Public License for more details.
33
//
34
// You should have received a copy of the GNU General Public License
35
// along with this program; if not, write to the Free Software
36
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
37
//
38
39
40
41
42
#include <stdlib.h>
43
#include <stdio.h>
44
#include <assert.h>
// assert()
45
#include "
globals.h
"
46
#include "
pools.h
"
47
#include "
atoms.h
"
48
#include "
lists.h
"
49
#include "
words.h
"
50
#include "
options.h
"
51
#include "
compiler.h
"
52
#include "
unicode.h
"
53
#include "
atoms.h
"
54
#include "
numbers.h
"
55
56
#define DEFAULT_BLOCK_SIZE 8192
57
58
pool_t
data_pool
;
59
pool_t
data_pool_ex
;
60
61
62
typedef
struct
{
63
void
*
next
;
64
}
free_atom_t
;
65
66
67
68
//===================================================
78
//===================================================
79
void
init_pool
(
pool_t
* pool,
int
atom_size )
80
{
81
static
int
id
= 0;
82
pool->
id
= ++id;
83
pool->
atom_size
= atom_size;
84
pool->
block_size
=
DEFAULT_BLOCK_SIZE
;
85
pool->
free_block_atoms
= 0;
86
pool->
free_atom
=
NULL
;
87
pool->
free_block
=
NULL
;
88
pool->
first_atom
=
NULL
;
89
90
#ifdef DEBUG_POOL
91
printf(
"<POOL> Create pool %d (size=%d)\n"
,
92
pool->
id
, atom_size );
93
#endif //DEBUG_POOL
94
}
95
96
97
98
99
//===================================================
106
//===================================================
107
void
expand_pool
(
pool_t
* pool )
108
{
109
pool->
first_atom
=
ALLOC
( pool->
atom_size
* pool->
block_size
);
110
pool->
free_block
= pool->
first_atom
;
111
112
#ifdef DEBUG_CLEAR_FREED_MEM
113
memset( pool->
first_atom
, 0xFF, pool->
atom_size
* pool->
block_size
);
114
#endif //DEBUG_CLEAR_FREE_MEM
115
116
117
#ifdef SAFEMODE
118
assert( pool->
free_block
);
119
#endif //SAFEMODE
120
121
pool->
free_block_atoms
= pool->
block_size
;
122
123
#ifdef DEBUG_POOL
124
printf(
"<POOL> Expand pool %d by %d bytes (%d atoms)\n"
,
125
pool->
id
, pool->
atom_size
*pool->
block_size
,pool->
block_size
);
126
#endif //DEBUG_POOL
127
128
#ifdef ADVANCED
129
stats_free
+= pool->
block_size
;
130
stats_allocs
--;
// do not count pool allocs
131
#endif //ADVANCED
132
}
133
134
135
136
137
//===================================================
147
//===================================================
148
void
return_to_pool
(
pool_t
* pool,
void
* a )
149
{
150
//printf("!");
151
#ifdef DEBUG_ATOM
152
printf(
"<ATOM> -[%08x]\n"
,(
int
)a);
153
#endif //DEBUG_ATOM
154
155
#ifdef DEBUG_RUNTIME_ATOMS
156
if
(
running_compiled_code
)
157
{
158
outter
(
TEXT
(
"<RUNTIME> free "
), -1 );
159
dump_atom_address( a );
160
//dump_atom( a, 1 );
161
outter
(
TEXT
(
"\n"
), -1 );
162
}
163
#endif
164
#ifdef DEBUG_COMPILETIME_ATOMS
165
if
(
compiling_code
)
166
{
167
outter
(
TEXT
(
"<COMPILETIME> free "
), -1 );
168
dump_atom_address( a );
169
//dump_atom( a, 1 );
170
outter
(
TEXT
(
"\n"
), -1 );
171
}
172
#endif
173
174
#ifdef DEBUG_CLEAR_FREED_MEM
175
memset( a, 0xFF, pool->
atom_size
);
176
#endif //DEBUG_CLEAR_FREE_MEM
177
178
free_atom_t
* b = a;
179
b->
next
= pool->
free_atom
;
180
pool->
free_atom
= b;
181
182
//printf("delete %x\n",(int)b);
183
184
//*((void**)a) = pool->free_atom;
185
//pool->free_atom = a;
186
187
}
188
189
190
191
192
//===================================================
202
//===================================================
203
void
*
take_from_pool
(
pool_t
* pool )
204
{
205
// take atom from the list
206
free_atom_t
*
new
= pool->
free_atom
;
207
//printf(",");
208
if
(
new
)
209
{
// there is free atom in the list
210
//printf("alloc %x (next free is %x)\n",(int)new,(int)new->next);
211
pool->
free_atom
=
new
->next;
212
}
213
else
214
{
// take atom from the block
215
if
( !pool->
free_block
)
expand_pool
( pool );
216
217
new
= (
free_atom_t
*)((
char
*)pool->
free_block
+ (-- pool->
free_block_atoms
)*pool->
atom_size
);
218
if
( pool->
free_block_atoms
== 0 )
219
pool->
free_block
=
NULL
;
220
}
221
222
#ifdef DEBUG_ATOM
223
printf(
"<ATOM> +[%08x]\n"
,(
int
)
new
);
224
#endif //ATOM
225
226
#ifdef DEBUG_RUNTIME_ATOMS
227
if
(
running_compiled_code
)
228
{
229
outter
(
TEXT
(
"<RUNTIME> alloc"
), -1 );
230
dump_atom_address( (
atom_t
)
new
);
231
outter
(
TEXT
(
"\n"
), -1 );
232
}
233
#endif
234
#ifdef DEBUG_COMPILETIME_ATOMS
235
if
(
compiling_code
)
236
{
237
outter
(
TEXT
(
"<COMPILETIME> alloc"
), -1 );
238
dump_atom_address( (
atom_t
)
new
);
239
outter
(
TEXT
(
"\n"
), -1 );
240
}
241
#endif
242
243
return
new
;
244
}
245
246
247
248
#ifdef DEBUG_MEMORY_LEAKS
249
//===================================================
256
//===================================================
257
void
dump_pool( )
258
{
259
void
dump_list
(
atom_t
a )
260
{
261
if
(
ID
(a)==0xFF )
262
printf(
"???"
);
263
else
264
if
(
IS_LIST
(a) )
265
{
266
printf(
"[ "
);
267
while
(
ID
(a)!=0xFF )
268
{
269
if
(
ID
(
CAR
(a))!=0xFF )
270
{
271
printf(
"|%d|{%x="
,
REF
(a),(
int
)
CAR
(a));
272
dump_list
(
CAR
(a));
273
printf(
"} "
);
274
}
275
else
276
printf(
"??? "
);
277
a =
CDR
(a);
278
}
279
printf(
"]"
);
280
}
281
else
282
dump
(a);
283
}
284
285
void
_dump_pool(
pool_t
p )
286
{
287
int
i;
288
atom_t
a = p.
first_atom
;
289
if
( !a )
return
;
290
for
( i=p.
block_size
; i; i--,a++ )
291
{
292
if
(
ID
(a)!=0xFF )
293
{
294
switch
(
ID
(a) )
295
{
296
case
INTEGER_ID
: printf(
"INT "
);
break
;
297
case
FLOAT_ID
: printf(
"FLP "
);
break
;
298
case
LIST_ID
: printf(
"LST "
);
break
;
299
case
WORD_ID
: printf(
"WRD "
);
break
;
300
case
SUBWORD_ID
: printf(
"SUB "
);
break
;
301
case
ERROR_ID
: printf(
"ERR "
);
break
;
302
case
VAR_ID
: printf(
"VAR "
);
break
;
303
case
MEM_ID
: printf(
"MEM "
);
break
;
304
default
: printf(
"??? "
);
break
;
305
}
306
printf(
"#%d[%x]="
,
REF
(a),(
int
)a);
307
dump_list
(a);
308
printf(
"\n"
);
309
}
310
}
311
}
312
313
printf(
"\nUNUSED ATOMS:\n"
);
314
_dump_pool( data_pool );
315
printf(
"\nUNUSED EXTENDED ATOMS:\n"
);
316
_dump_pool( data_pool_ex );
317
}
318
#endif //DEBUG_MEMORY_LEAKS
[
HOME
|
INDEX
|
ATOMS
|
VARS
|
REFERENCE
]
Lhogho Developer's Documentation
Wed Jul 10 2013