memory.cpp 4.74 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
 * Copyright (C) 1998-2002 Tom Holroyd <tomh@kurage.nimh.nih.gov>
 * Copyright (C) 2006-2009 Stephan Kulow <coolo@kde.org>
 *
 * 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, see <http://www.gnu.org/licenses/>.
 */

19
#include "memory.h"
Frederik Schwarzer's avatar
Frederik Schwarzer committed
20

Laurent Montel's avatar
Laurent Montel committed
21
#include <QDebug>
22

Heiko Becker's avatar
Heiko Becker committed
23
#include <QtGlobal>
24

Frederik Schwarzer's avatar
Frederik Schwarzer committed
25
26
#include <cstdlib>
#include <cstring>
27
#include <sys/types.h>
Frederik Schwarzer's avatar
Frederik Schwarzer committed
28

29
30
31
32
33
34

#define BLOCKSIZE (32 * 4096)

/* Add it to the binary tree for this cluster.  The piles are stored
following the TREE structure. */

35
size_t MemoryManager::Mem_remain = 30 * 1000 * 1000;
36
37
38
39

MemoryManager::inscode MemoryManager::insert_node(TREE *n, int d, TREE **tree, TREE **node)
{
        int c;
Stephan Kulow's avatar
Stephan Kulow committed
40
	quint8 *key, *tkey;
41
42
	TREE *t;

Stephan Kulow's avatar
Stephan Kulow committed
43
	key = (quint8 *)n + sizeof(TREE);
44
45
46
47
48
49
50
51
52
	n->depth = d;
	n->left = n->right = NULL;
	*node = n;
	t = *tree;
	if (t == NULL) {
		*tree = n;
		return NEW;
	}
	while (1) {
Stephan Kulow's avatar
Stephan Kulow committed
53
		tkey = (quint8 *)t + sizeof(TREE);
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
		c = memcmp(key, tkey, Pilebytes);
		if (c == 0) {
			break;
		}
		if (c < 0) {
			if (t->left == NULL) {
				t->left = n;
				return NEW;
			}
			t = t->left;
		} else {
			if (t->right == NULL) {
				t->right = n;
				return NEW;
			}
			t = t->right;
		}
	}

	/* We get here if it's already in the tree.  Don't add it again.
	If the new path to this position was shorter, record the new depth
	so we can prune the original path. */

Stephan Kulow's avatar
Stephan Kulow committed
77
        return FOUND;
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
}

/* Given a cluster number, return a tree.  There are 14^4 possible
clusters, but we'll only use a few hundred of them at most.  Hash on
the cluster number, then locate its tree, creating it if necessary. */

#define TBUCKETS 499    /* a prime */

TREELIST *Treelist[TBUCKETS];

/* Clusters are also stored in a hashed array. */

void MemoryManager::init_clusters(void)
{
	memset(Treelist, 0, sizeof(Treelist));
	Block = new_block();                    /* @@@ */
}

96
TREELIST *MemoryManager::cluster_tree(unsigned int cluster)
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
{
	int bucket;
	TREELIST *tl, *last;

	/* Pick a bucket, any bucket. */

	bucket = cluster % TBUCKETS;

	/* Find the tree in this bucket with that cluster number. */

	last = NULL;
	for (tl = Treelist[bucket]; tl; tl = tl->next) {
		if (tl->cluster == cluster) {
			break;
		}
		last = tl;
	}

	/* If we didn't find it, make a new one and add it to the list. */

	if (tl == NULL) {
118
		tl = mm_allocate(TREELIST);
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
		if (tl == NULL) {
			return NULL;
		}
		tl->tree = NULL;
		tl->cluster = cluster;
		tl->next = NULL;
		if (last == NULL) {
			Treelist[bucket] = tl;
		} else {
			last->next = tl;
		}
	}

	return tl;
}

/* Block storage.  Reduces overhead, and can be freed quickly. */

BLOCK *MemoryManager::new_block(void)
{
	BLOCK *b;

141
	b = mm_allocate(BLOCK);
142
143
144
	if (b == NULL) {
		return NULL;
	}
Stephan Kulow's avatar
Stephan Kulow committed
145
	b->block = new_array(quint8, BLOCKSIZE);
146
147
148
149
150
151
152
153
154
155
156
157
158
	if (b->block == NULL) {
                MemoryManager::free_ptr(b);
		return NULL;
	}
	b->ptr = b->block;
	b->remain = BLOCKSIZE;
	b->next = NULL;

	return b;
}

/* Like new(), only from the current block.  Make a new block if necessary. */

Stephan Kulow's avatar
Stephan Kulow committed
159
quint8 *MemoryManager::new_from_block(size_t s)
160
{
Stephan Kulow's avatar
Stephan Kulow committed
161
	quint8 *p;
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
	BLOCK *b;

	b = Block;
	if (s > b->remain) {
		b = new_block();
		if (b == NULL) {
			return NULL;
		}
		b->next = Block;
		Block = b;
	}

	p = b->ptr;
	b->remain -= s;
	b->ptr += s;

	return p;
}

/* Return the previous result of new_from_block() to the block.  This
can ONLY be called once, immediately after the call to new_from_block().
That is, no other calls to new_from_block() are allowed. */

Stephan Kulow's avatar
Stephan Kulow committed
185
void MemoryManager::give_back_block(quint8 *p)
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
{
	size_t s;
	BLOCK *b;

	b = Block;
	s = b->ptr - p;
	b->ptr -= s;
	b->remain += s;
}

void MemoryManager::free_blocks(void)
{
	BLOCK *b, *next;

	b = Block;
	while (b) {
		next = b->next;
                MemoryManager::free_array(b->block, BLOCKSIZE);
                MemoryManager::free_ptr(b);
		b = next;
	}
}

void MemoryManager::free_clusters(void)
{
	int i;
	TREELIST *l, *n;

	for (i = 0; i < TBUCKETS; i++) {
		l = Treelist[i];
		while (l) {
			n = l->next;
                        MemoryManager::free_ptr(l);
			l = n;
		}
	}
}

/* Allocate some space and return a pointer to it.  See new() in util.h. */

void *MemoryManager::allocate_memory(size_t s)
{
	void *x;

	if (s > Mem_remain) {
		return NULL;
	}

	if ((x = (void *)malloc(s)) == NULL) {
		return NULL;
	}

238
        memset( x, 0, s );
239
240
241
	Mem_remain -= s;
	return x;
}