summaryrefslogtreecommitdiff
path: root/colm/pool.h
blob: 0e77399763eb918c13b508fcc077e5775485fd35 (plain)
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
 *  Copyright 2010 Adrian Thurston <thurston@complang.org>
 */

/*  This file is part of Colm.
 *
 *  Colm 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.
 * 
 *  Colm 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 Colm; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 */

#ifndef _POOL_H
#define _POOL_H

#include <iostream>

using std::cerr;
using std::endl;
using std::ostream;

/* Allocation, number of items. */
#define FRESH_BLOCK 8128                    

struct PoolItem
{
	PoolItem *next;
};

template <class T> struct PoolBlock
{
	T data[FRESH_BLOCK];
	PoolBlock<T> *next;
};

template <class T> struct PoolAlloc
{
	PoolAlloc() : 
		head(0), nextel(FRESH_BLOCK), pool(0)
	{}

	T *_allocate();
	void _free( T *el );
	void _clear();
	long _numLost();

private:

	PoolBlock<T> *head;
	long nextel;
	PoolItem *pool;
};

template <class T> T *PoolAlloc<T>::_allocate()
{
	//#ifdef COLM_LOG_BYTECODE
	//cerr << "allocating in: " << __PRETTY_FUNCTION__ << endl;
	//#endif
	T *newEl = 0;
	if ( pool == 0 ) {
		if ( nextel == FRESH_BLOCK ) {
			#ifdef COLM_LOG_BYTECODE
			if ( colm_log_bytecode )
				cerr << "allocating " << FRESH_BLOCK << " Elements of type T" << endl;
			#endif

			PoolBlock<T> *newBlock = new PoolBlock<T>;
			newBlock->next = head;
			head = newBlock;
			nextel = 0;
		}
		newEl = &head->data[nextel++];
	}
	else {
		newEl = (T*)pool;
		pool = pool->next;
	}
	memset( newEl, 0, sizeof(T) );
	return newEl;
}

template <class T> void PoolAlloc<T>::_free( T *el )
{
	#if 0
	/* Some sanity checking. Best not to normally run with this on. */
	char *p = (char*)el + sizeof(PoolItem*);
	char *pe = (char*)el + sizeof(T);
	for ( ; p < pe; p++ )
		assert( *p != 0xcc );
	memset( el, 0xcc, sizeof(T) );
	#endif

	PoolItem *pi = (PoolItem*) el;
	pi->next = pool;
	pool = pi;
}

template <class T> void PoolAlloc<T>::_clear()
{
	PoolBlock<T> *block = head;
	while ( block != 0 ) {
		PoolBlock<T> *next = block->next;
		delete block;
		block = next;
	}

	head = 0;
	nextel = 0;
	pool = 0;
}

template <class T> long PoolAlloc<T>::_numLost()
{
	/* Count the number of items allocated. */
	long lost = 0;
	PoolBlock<T> *block = head;
	if ( block != 0 ) {
		lost = nextel;
		block = block->next;
		while ( block != 0 ) {
			lost += FRESH_BLOCK;
			block = block->next;
		}
	}

	/* Subtract. Items that are on the free list. */
	PoolItem *pi = pool;
	while ( pi != 0 ) {
		lost -= 1;
		pi = pi->next;
	}

	return lost;
}

struct Program;
struct Kid;
struct Tree;
struct ParseTree;
struct ListEl;
struct MapEl;
struct Head;
struct Location;

Kid *kidAllocate( Program *prg );
void kidFree( Program *prg, Kid *el );
void kidClear( Program *prg );
long kidNumLost( Program *prg );

Tree *treeAllocate( Program *prg );
void treeFree( Program *prg, Tree *el );
void treeClear( Program *prg );
long treeNumLost( Program *prg );

ParseTree *parseTreeAllocate( Program *prg );
void parseTreeFree( Program *prg, ParseTree *el );
void parseTreeClear( Program *prg );
long parseTreeNumLost( Program *prg );

ListEl *listElAllocate( Program *prg );
void listElFree( Program *prg, ListEl *el );
void listElClear( Program *prg );
long listElNumLost( Program *prg );

MapEl *mapElAllocate( Program *prg );
void mapElFree( Program *prg, MapEl *el );
void mapElClear( Program *prg );
long mapElNumLost( Program *prg );

Head *headAllocate( Program *prg );
void headFree( Program *prg, Head *el );
void headClear( Program *prg );
long headNumLost( Program *prg );

Location *locationAllocate( Program *prg );
void locationFree( Program *prg, Location *el );
void locationClear( Program *prg );
long locationNumLost( Program *prg );

#endif