aboutsummaryrefslogtreecommitdiff
path: root/sc4pd/headers/lang/PowerOfTwoAllocPool.h
blob: 8dde18f2e9e3da0678cb5866992351504dcc940a (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
/*
	SuperCollider real time audio synthesis system
    Copyright (c) 2002 James McCartney. All rights reserved.
	http://www.audiosynth.com

    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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

// Implements a power of two size class allocator.
// There is no consolidation of free space. Once a chunk is allocated it
// remains at that size from then on whether free or allocated.
// It uses AdvancingAllocPool as its parent allocator.
// It is very fast. This is used to allocate Unit output buffers.

#ifndef _PowerOfTwoAllocPool_
#define _PowerOfTwoAllocPool_

#include <stdexcept>
#include <stdlib.h>
#include "clz.h"
#include "AdvancingAllocPool.h"
#include "SC_AllocPool.h"
void post(const char *fmt, ...);
void postbuf(const char *fmt, ...);

template<class Hdr, class Obj, class Elem, int LargeObjSizeClass, int Align>
class PowerOfTwoAllocPool 
{
public:
	PowerOfTwoAllocPool(::AllocPool *inPool, 
		size_t initSize = 64*1024, 
		size_t growSize = 64*1024
	)
		{
			mLargeObjPool = inPool;
			mNumLargeObjects = 0;
			mNumAlloc = 0;
			mNumFree = 0;
			size_t tooBigSize = (sizeof(Hdr) + (sizeof(Elem) << (LargeObjSizeClass-1))) + 1;
			mSmallObjPool.Init(inPool, initSize, growSize, tooBigSize);
			Init();
			assert(SanityCheck());
		}
	~PowerOfTwoAllocPool()
		{
			assert(SanityCheck());
			assert(mNumLargeObjects == 0); // you have to free the big ones yourself
			mSmallObjPool.FreeAll();
		}
		
	Obj* Alloc(int32 inNumElems)
		{
			//mNumAlloc++;
			assert(SanityCheck());
			int sizeclass = LOG2CEIL(inNumElems);
			if (sizeclass >= LargeObjSizeClass) {
				mNumLargeObjects++;
				size_t size = sizeof(Hdr) + (sizeof(Elem) * inNumElems);
				return (Obj*)mLargeObjPool->Alloc(size);
			}
			
			// get from free list
			Obj* obj = mFreeLists[sizeclass];
			if (obj != NULL) {
				// set free list to next element.
				mFreeLists[sizeclass] = *(Obj**)obj;
			} else {
				// have to allocate it
				size_t size = mSizes[sizeclass];
				obj = (Obj*)mSmallObjPool.Alloc(size);
				if (!obj) throw runtime_error("PowerOfTwoAllocPool out of memory");
			}
			//obj->mMagic = 'magk';
			assert(SanityCheck());
			return obj;
		}
	void Free(Obj* inObjPtr)
		{
			//mNumFree++;
			assert(SanityCheck());
			if (inObjPtr == 0) return;                   /* free(0) has no effect */
			/*if (inObjPtr->mMagic != 'magk') {
				postbuf("bad object\n");
				throw runtime_error("bad object");
			}*/
			int sizeclass = inObjPtr->SizeClass();
			if (sizeclass >= LargeObjSizeClass) {
				mLargeObjPool->Free(inObjPtr);
				mNumLargeObjects--;
			} else {
				Obj* nextfree = mFreeLists[sizeclass];
				mFreeLists[sizeclass] = inObjPtr;
				*(Obj**)inObjPtr = nextfree;
			}
			assert(SanityCheck());
		}
	void FreeAll()
		{
			assert(mNumLargeObjects == 0); // you have to free the big ones yourself
			mSmallObjPool.FreeAll();
			Init();
		}
	
	bool SanityCheck()
		{
			//postbuf("PowerOfTwoAllocPool::SanityCheck %d %d\n", mNumAlloc, mNumFree);
			mLargeObjPool->DoCheckPool();
			mSmallObjPool.SanityCheck();
			for (int i=0; i<LargeObjSizeClass; ++i) {
				Obj* obj = mFreeLists[i];
				for (int j=0; obj; ++j) {
					if (j>=1000) {
						post("linked loop??\n");
						throw runtime_error("linked loop??\n");
						return false;
					}
					obj = *(Obj**)obj;
				}
			}
			return true;
		}
private:
	void Init()
		{
			for (int i=0; i<LargeObjSizeClass; ++i) {
				mFreeLists[i] = NULL;
				size_t size = sizeof(Hdr) + (sizeof(Elem) << i);
				mSizes[i] = (size + (Align-1)) & ~(Align-1); // alignment
			}
		}
	
	Obj* mFreeLists[LargeObjSizeClass];
	size_t mSizes[LargeObjSizeClass];
	AllocPool* mLargeObjPool;
	AdvancingAllocPool mSmallObjPool;
	int mNumLargeObjects;
	int mNumAlloc, mNumFree;
};


#endif