forked from schani/SimpLang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pools.c
119 lines (97 loc) · 2.93 KB
/
pools.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* pools.c
*
* lispreader
*
* Copyright (C) 2002-2007 Mark Probst
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "pools.h"
bool
pool_init (pool_t *pool)
{
int i;
pool->active_chunk = 0;
pool->fill_ptr = 0;
for (i = 0; i < POOLS_NUM_CHUNKS; ++i)
pool->chunks[i] = 0;
pool->chunks[0] = (long*)malloc(POOLS_GRANULARITY * POOLS_FIRST_CHUNK_SIZE);
if (pool->chunks[0] == 0)
return false;
memset(pool->chunks[0], 0, POOLS_GRANULARITY * POOLS_FIRST_CHUNK_SIZE);
return true;
}
#ifndef __GNUC__
void
pool_reset (pool_t *pool)
{
pool->active_chunk = 0;
pool->fill_ptr = 0;
}
#endif
void
pool_free (pool_t *pool)
{
int i;
/* printf("alloced %d pools\n", active_chunk + 1); */
for (i = 0; i < POOLS_NUM_CHUNKS; ++i)
if (pool->chunks[i] != 0)
free(pool->chunks[i]);
}
#ifdef __GNUC__
void*
_pool_alloc (pool_t *pool, size_t byte_size)
#else
void*
pool_alloc (pool_t *pool, size_t byte_size)
#endif
{
size_t pool_size, size;
void *p;
pool_size = POOLS_FIRST_CHUNK_SIZE << pool->active_chunk;
size = (byte_size + POOLS_GRANULARITY - 1) / POOLS_GRANULARITY;
while (pool->fill_ptr + size >= pool_size)
{
size_t new_pool_size;
++pool->active_chunk;
assert(pool->active_chunk < POOLS_NUM_CHUNKS);
pool->fill_ptr = 0;
new_pool_size = POOLS_FIRST_CHUNK_SIZE << pool->active_chunk;
/* TODO: if the requested block is too big to fit into the
pool to be allocated, it should simply be skipped, which
would save memory. */
if (pool->chunks[pool->active_chunk] == 0)
{
size_t new_pool_byte_size = POOLS_GRANULARITY * new_pool_size;
/* printf("allocing pool %d with size %ld\n", pool->active_chunk, (long)new_pool_byte_size); */
pool->chunks[pool->active_chunk] = (long*)malloc(new_pool_byte_size);
if (pool->chunks[pool->active_chunk] == 0)
return 0;
/* FIXME: either remove the memset here or memset the pool
even if it's not newly allocated, because pools can be
reset. */
memset(pool->chunks[pool->active_chunk], 0, new_pool_byte_size);
}
pool_size = new_pool_size;
}
assert(pool->fill_ptr + size < pool_size);
p = pool->chunks[pool->active_chunk] + pool->fill_ptr;
pool->fill_ptr += size;
return p;
}