File: PoolMemoryAllocator.cpp

package info (click to toggle)
bandage 0.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 15,684 kB
  • sloc: cpp: 45,359; sh: 491; makefile: 12
file content (448 lines) | stat: -rw-r--r-- 10,171 bytes parent folder | download | duplicates (3)
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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*
 * $Revision: 2549 $
 *
 * last checkin:
 *   $Author: gutwenger $
 *   $Date: 2012-07-04 23:09:19 +0200 (Mi, 04. Jul 2012) $
 ***************************************************************/

/** \file
 * \brief Implementation of memory manager for more efficiently
 *        allocating small pieces of memory
 *
 * \author Carsten Gutwenger
 *
 * \par License:
 * This file is part of the Open Graph Drawing Framework (OGDF).
 *
 * \par
 * Copyright (C)<br>
 * See README.txt in the root directory of the OGDF installation for details.
 *
 * \par
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * Version 2 or 3 as published by the Free Software Foundation;
 * see the file LICENSE.txt included in the packaging of this file
 * for details.
 *
 * \par
 * 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.
 *
 * \par
 * 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., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 * \see  http://www.gnu.org/copyleft/gpl.html
 ***************************************************************/


#include "../../basic/basic.h"


namespace ogdf {


struct PoolMemoryAllocator::PoolVector
{
	MemElemPtr m_pool[ePoolVectorLength];
	PoolVector *m_prev;
};

struct PoolMemoryAllocator::PoolElement
{
	PoolVector *m_currentVector;
	MemElemPtr m_restHead;
	MemElemPtr m_restTail;
	__int16 m_index;
	__int16 m_restCount;
};

struct PoolMemoryAllocator::BlockChain
{
	char m_fill[eBlockSize-sizeof(void*)];
	BlockChain *m_next;
};


PoolMemoryAllocator::PoolElement PoolMemoryAllocator::s_pool[eTableSize];
PoolMemoryAllocator::MemElemPtr PoolMemoryAllocator::s_freeVectors;
PoolMemoryAllocator::BlockChainPtr PoolMemoryAllocator::s_blocks;

#ifdef OGDF_MEMORY_POOL_NTS
PoolMemoryAllocator::MemElemPtr PoolMemoryAllocator::s_tp[eTableSize];
#elif defined(OGDF_NO_COMPILER_TLS)
CriticalSection *PoolMemoryAllocator::s_criticalSection;
pthread_key_t PoolMemoryAllocator::s_tpKey;
#else
CriticalSection *PoolMemoryAllocator::s_criticalSection;
OGDF_DECL_THREAD PoolMemoryAllocator::MemElemPtr PoolMemoryAllocator::s_tp[eTableSize];
#endif


void PoolMemoryAllocator::init()
{
#ifndef OGDF_MEMORY_POOL_NTS
#ifdef OGDF_NO_COMPILER_TLS
	pthread_key_create(&s_tpKey,NULL);
#endif
	s_criticalSection = new CriticalSection(500);
#endif
	initThread();
}


void PoolMemoryAllocator::initThread() {
#if !defined(OGDF_MEMORY_POOL_NTS) && defined(OGDF_NO_COMPILER_TLS)
		pthread_setspecific(s_tpKey,calloc(eTableSize,sizeof(MemElemPtr)));
#endif
}


void PoolMemoryAllocator::cleanup()
{
	BlockChainPtr p = s_blocks;
	while(p != 0) {
		BlockChainPtr pNext = p->m_next;
		free(p);
		p = pNext;
	}

#ifndef OGDF_MEMORY_POOL_NTS
#ifdef OGDF_NO_COMPILER_TLS
	pthread_key_delete(s_tpKey);
#endif
	delete s_criticalSection;
#endif
}


bool PoolMemoryAllocator::checkSize(size_t nBytes) {
	return nBytes < eTableSize;
}


void *PoolMemoryAllocator::allocate(size_t nBytes) {
#if !defined(OGDF_MEMORY_POOL_NTS) && defined(OGDF_NO_COMPILER_TLS)
	MemElemPtr *pFreeBytes = ((MemElemPtr*)pthread_getspecific(s_tpKey))+nBytes;
#else
	MemElemPtr *pFreeBytes = s_tp+nBytes;
#endif
	if (OGDF_LIKELY(*pFreeBytes != 0)) {
		MemElemPtr p = *pFreeBytes;
		*pFreeBytes = p->m_next;
		return p;
	} else {
		return fillPool(*pFreeBytes,__uint16(nBytes));
	}
}


void PoolMemoryAllocator::deallocate(size_t nBytes, void *p) {
#if !defined(OGDF_MEMORY_POOL_NTS) && defined(OGDF_NO_COMPILER_TLS)
	MemElemPtr *pFreeBytes = ((MemElemPtr*)pthread_getspecific(s_tpKey))+nBytes;
#else
	MemElemPtr *pFreeBytes = s_tp+nBytes;
#endif
	MemElemPtr(p)->m_next = *pFreeBytes;
	*pFreeBytes = MemElemPtr(p);
}


void PoolMemoryAllocator::deallocateList(size_t nBytes, void *pHead, void *pTail) {
#if !defined(OGDF_MEMORY_POOL_NTS) && defined(OGDF_NO_COMPILER_TLS)
	MemElemPtr *pFreeBytes = ((MemElemPtr*)pthread_getspecific(s_tpKey))+nBytes;
#else
	MemElemPtr *pFreeBytes = s_tp+nBytes;
#endif
	MemElemPtr(pTail)->m_next = *pFreeBytes;
	*pFreeBytes = MemElemPtr(pHead);
}


PoolMemoryAllocator::MemElemExPtr
PoolMemoryAllocator::collectGroups(
	__uint16 nBytes,
	MemElemPtr &pRestHead,
	MemElemPtr &pRestTail,
	int &nRest)
{
	int n = slicesPerBlock(nBytes);
	pRestHead = 0;

#if !defined(OGDF_MEMORY_POOL_NTS) && defined(OGDF_NO_COMPILER_TLS)
	MemElemPtr p = ((MemElemPtr*)pthread_getspecific(s_tpKey))[nBytes];
#else
	MemElemPtr p = s_tp[nBytes];
#endif
	MemElemExPtr pStart = 0, pLast = 0;
	while(p != 0)
	{
		int i = 0;
		MemElemPtr pHead = p, pTail;
		do {
			pTail = p;
			p = p->m_next;
		} while(++i < n && p != 0);

		pTail->m_next = 0;
		if(i == n) {
			if(pStart == 0)
				pStart = MemElemExPtr(pHead);
			else
				pLast->m_down = MemElemExPtr(pHead);
			pLast = MemElemExPtr(pHead);

		} else {
			pRestHead = pHead;
			pRestTail = pTail;
			nRest = i;
		}
	}
	if (pLast)
		pLast->m_down = 0;

	return pStart;
}


void PoolMemoryAllocator::flushPoolSmall(__uint16 nBytes)
{
    int n = slicesPerBlock(nBytes < eMinBytes ? int(eMinBytes) : int(nBytes));
	PoolElement &pe = s_pool[nBytes];

#if !defined(OGDF_MEMORY_POOL_NTS) && defined(OGDF_NO_COMPILER_TLS)
	MemElemPtr p = ((MemElemPtr*)pthread_getspecific(s_tpKey))[nBytes];
#else
	MemElemPtr p = s_tp[nBytes];
#endif
	if(pe.m_restHead != 0) {
		pe.m_restTail->m_next = p;
		p = pe.m_restHead;
		pe.m_restHead = 0;
	}

	while(p != 0)
	{
		int i = 0;
		MemElemPtr pHead = p, pTail;
		do {
			pTail = p;
			p = p->m_next;
		} while(++i < n && p != 0);

		if(i == n) {
			incVectorSlot(pe);
			pe.m_currentVector->m_pool[pe.m_index] = pHead;

		} else {
			pe.m_restHead = pHead;
			pe.m_restTail = pTail;
			pe.m_restCount = i;
		}
	}
}


void PoolMemoryAllocator::incVectorSlot(PoolElement &pe)
{
	if(pe.m_currentVector == 0 || ++pe.m_index == ePoolVectorLength) {
		if(s_freeVectors == 0)
			s_freeVectors = allocateBlock(sizeof(PoolVector));

		PoolVector *pv = (PoolVector *)s_freeVectors;
		s_freeVectors = MemElemPtr(pv)->m_next;
		pe.m_currentVector = pv;
		pe.m_index = 0;
	}
}


void PoolMemoryAllocator::flushPool(__uint16 nBytes)
{
#ifndef OGDF_MEMORY_POOL_NTS
	if(nBytes >= sizeof(MemElemEx)) {
		MemElemPtr pRestHead, pRestTail;
		int nRest;
		MemElemExPtr pStart = collectGroups(nBytes, pRestHead, pRestTail, nRest);

		s_criticalSection->enter();
		PoolElement &pe = s_pool[nBytes];

		while(pStart != 0) {
			incVectorSlot(pe);
			pe.m_currentVector->m_pool[pe.m_index] = MemElemPtr(pStart);
			pStart = pStart->m_down;
		}
		if(pRestHead != 0) {
			int n = slicesPerBlock(nBytes);
			pRestTail->m_next = pe.m_restTail;
			int nTotal = nRest + pe.m_restCount;
			if(nTotal >= n) {
				MemElemPtr p = pe.m_restHead;
				int i = n-nRest;
				while(--i > 0)
					p = p->m_next;
				pe.m_restHead = p->m_next;
				pe.m_restCount = nTotal-n;
				incVectorSlot(pe);
				pe.m_currentVector->m_pool[pe.m_index] = pRestHead;
			} else {
				pe.m_restHead = pRestHead;
				pe.m_restCount = nTotal;
			}
		}
		s_criticalSection->leave();

	} else {
		s_criticalSection->enter();
		flushPoolSmall(nBytes);
		s_criticalSection->leave();
	}
#endif
}


void PoolMemoryAllocator::flushPool()
{
#ifndef OGDF_MEMORY_POOL_NTS
	for(__uint16 nBytes = 1; nBytes < eTableSize; ++nBytes) {
#ifdef OGDF_NO_COMPILER_TLS
	MemElemPtr p = ((MemElemPtr*)pthread_getspecific(s_tpKey))[nBytes];
#else
	MemElemPtr p = s_tp[nBytes];
#endif
		if(p != 0)
			flushPool(nBytes);
	}
#endif
}


void *PoolMemoryAllocator::fillPool(MemElemPtr &pFreeBytes, __uint16 nBytes)
{
#ifdef OGDF_MEMORY_POOL_NTS
	pFreeBytes = allocateBlock(nBytes);
#else

	s_criticalSection->enter();

	PoolElement &pe = s_pool[nBytes];
	if(pe.m_currentVector != 0) {
		pFreeBytes = pe.m_currentVector->m_pool[pe.m_index];
		if(--pe.m_index < 0) {
			PoolVector *pV = pe.m_currentVector;
			pe.m_currentVector = pV->m_prev;
			pe.m_index = ePoolVectorLength-1;
			MemElemPtr(pV)->m_next = s_freeVectors;
			s_freeVectors = MemElemPtr(pV);
		}
		s_criticalSection->leave();

	} else {
		s_criticalSection->leave();
		pFreeBytes = allocateBlock(nBytes);
	}
#endif

	MemElemPtr p = pFreeBytes;
	pFreeBytes = p->m_next;
	return p;
}


// __asm __volatile ("":::"memory")      GLIBC


PoolMemoryAllocator::MemElemPtr
PoolMemoryAllocator::allocateBlock(__uint16 nBytes)
{
	if(nBytes < eMinBytes)
		nBytes = eMinBytes;

	MemElemPtr pBlock = (MemElemPtr) malloc(eBlockSize);

	// we altogether create nSlices slices
	int nWords;
	int nSlices = slicesPerBlock(nBytes,nWords);

	MemElemPtr pHead = MemElemPtr(pBlock);
	BlockChainPtr(pBlock)->m_next = s_blocks;
	s_blocks = BlockChainPtr(pBlock);

	do {
		pBlock = pBlock->m_next = pBlock+nWords;
	} while(--nSlices > 1);
	MemElemPtr(pBlock)->m_next = 0;

	return pHead;
}


size_t PoolMemoryAllocator::memoryAllocatedInBlocks()
{
#ifndef OGDF_MEMORY_POOL_NTS
	s_criticalSection->enter();
#endif

	size_t nBlocks = 0;
	for (BlockChainPtr p = s_blocks; p != 0; p = p->m_next)
		++nBlocks;

#ifndef OGDF_MEMORY_POOL_NTS
	s_criticalSection->leave();
#endif

	return nBlocks * eBlockSize;
}


size_t PoolMemoryAllocator::memoryInGlobalFreeList()
{
#ifndef OGDF_MEMORY_POOL_NTS
	s_criticalSection->enter();
#endif

	size_t bytesFree = 0;
	for (int sz = 1; sz < eTableSize; ++sz)
	{
		const PoolElement &pe = s_pool[sz];
		PoolVector *pv = pe.m_currentVector;
		for(; pv != 0; pv = pv->m_prev)
			bytesFree += ePoolVectorLength*sz;
		if(pe.m_restHead != 0)
			bytesFree += pe.m_restCount;
	}

#ifndef OGDF_MEMORY_POOL_NTS
	s_criticalSection->leave();
#endif

	return bytesFree;
}


size_t PoolMemoryAllocator::memoryInThreadFreeList()
{
	size_t bytesFree = 0;
	for (int sz = 1; sz < eTableSize; ++sz)
	{
#if !defined(OGDF_MEMORY_POOL_NTS) && defined(OGDF_NO_COMPILER_TLS)
		MemElemPtr p = ((MemElemPtr*)pthread_getspecific(s_tpKey))[sz];
#else
		MemElemPtr p = s_tp[sz];
#endif
		for(; p != 0; p = p->m_next)
			bytesFree += sz;
	}

	return bytesFree;
}


}