aboutsummaryrefslogtreecommitdiff
path: root/sc4pd/headers/common/SC_AllocPool.h
blob: fc9d72aa4fb234c36d9a75cc4d1b926de1634039 (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
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

/*
	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
*/
/*
This is based on Doug Lea's allocator but rewritten so I can read and understand it...
also features of free all-at-once pools are added.
Now uses 16 byte alignment, which does increase the minimum allocation size to 32 bytes
including the overhead.
Improved bit block scanning by using a count leading zeroes instruction.

*/

#ifndef _AllocPool_
#define _AllocPool_

#include "SC_List.h"
#include "clz.h"
#include <stdlib.h>

const int kNumAllocBins = 128;
const int kNumSmallBins = 64;
const int kMaxSmallBin = kNumSmallBins - 1;
const int kBinWidth = 8;
const int kMaxSmallBinSize = kNumSmallBins * kBinWidth;
const int kBinBlockWidth = 4;
const int kBinBlockMask = kBinBlockWidth - 1;

const size_t kAlign = 16;
const size_t kAlignMask = kAlign - 1;
const size_t kChunkFree = 0;
const size_t kChunkInUse = 1;
const size_t kSizeBits = ~kChunkInUse;

class AllocChunk;
class AllocPool;
typedef AllocChunk *AllocChunkPtr;
typedef Link<AllocChunk> AllocBin;
typedef AllocBin* AllocBinPtr;

class AllocChunk : public Link<AllocChunk>
{
	friend class AllocPool;

	size_t Size() 
		{ return mSize & kSizeBits; }
		
	size_t PrevSize() 
		{ return mPrevSize & kSizeBits; }
		
	AllocChunkPtr ChunkAtOffset(size_t inSize) 
		{ return AllocChunkPtr((char*)this + inSize); }
		
	AllocChunkPtr NextChunk() 
		{ return ChunkAtOffset(Size()); }
		
	AllocChunkPtr PrevChunk() 
		{ return ChunkAtOffset(-PrevSize()); }
		
	bool InUse() 
		{ return (bool)(mSize & kChunkInUse); }
	
	bool PrevInUse() 
		{ return (bool)(mPrevSize & kChunkInUse); }
	
	void SetSizeFree(size_t inSize) 
		{ mSize = ChunkAtOffset(inSize)->mPrevSize = inSize; }
		
	void SetSizeInUse(size_t inSize) 
		{ mSize = ChunkAtOffset(inSize)->mPrevSize = inSize | kChunkInUse; }
		
	void SetNeighborsInUse(size_t inOffset)
		{ mPrevSize = ChunkAtOffset(inOffset)->mSize = kChunkInUse; }
	
	bool IsArea()
		{ return mPrevSize == kChunkInUse && NextChunk()->mSize == kChunkInUse; }
	
	void* ToPtr() 
		{ return (void*)((char*)this + sizeof(AllocChunk)); }
	
	size_t mPrevSize;
	size_t mSize;
};

class AllocArea;
typedef AllocArea* AllocAreaPtr;

class AllocAreaHdr /* for size calculations */
{
protected:
	friend class AllocPool;
	
	AllocAreaPtr mPrev, mNext;
	size_t mSize;
	void *mUnalignedPointerToThis;
}; 

class AllocArea : public AllocAreaHdr
{
public:
	void SanityCheck();
	
private:
	friend class AllocPool;
	
	AllocChunk mChunk;
};

const size_t kMinAllocSize = 2 * kAlign;
// FIXME: add kAlign to overhead? <sk>
const size_t kAreaOverhead = sizeof(AllocAreaHdr) + 2 * sizeof(AllocChunk);

typedef void* (*NewAreaFunc)(size_t size);
typedef void (*FreeAreaFunc)(void *);

class AllocPool 
{
public:
	AllocPool(NewAreaFunc allocArea, FreeAreaFunc freeArea, 
					size_t areaInitSize, size_t areaMoreSize);
	~AllocPool();
	
	void Reinit();
	
	void *Alloc(size_t inBytes);
	void *Realloc(void* inPtr, size_t inBytes);
	void Free(void* inPtr);
	void FreeAll();
	void FreeAllInternal();
	
	// debugging
	size_t TotalFree();
	size_t LargestFreeChunk();
	
	void DoCheckPool();
	void DoCheckInUseChunk(AllocChunkPtr p);

	static AllocChunkPtr MemToChunk(void *inPtr)
		{ return (AllocChunkPtr)((char*)(inPtr) - sizeof(AllocChunk)); }
	
private:
	void InitAlloc();
	void InitBins();
	AllocAreaPtr NewArea(size_t inAreaSize);
	void FreeArea(AllocChunkPtr chunk);
	
	// debugging
	void DoCheckArea(AllocAreaPtr area);
	void DoCheckBin(AllocChunkPtr bin, long idx);
	void DoCheckChunk(AllocChunkPtr p);
	void DoCheckFreeChunk(AllocChunkPtr p);
	void DoCheckAllocedChunk(AllocChunkPtr p, size_t s);
	void DoGarbageFill(AllocChunkPtr p);
	void DoGarbageFill(AllocChunkPtr p, long size);

	// inlines
	
	static size_t RequestToSize(size_t inReqSize) 
	{
		size_t sizePlusOverhead = inReqSize + sizeof(AllocChunk);
		if (sizePlusOverhead <= kMinAllocSize) return kMinAllocSize;
		else return (sizePlusOverhead + kAlignMask) & ~kAlignMask;
	}

	static int SmallBinIndex(size_t inSize)
		{ return inSize >> 4; }

	static int BinIndex2(size_t inSize) 
	{
		return
		((inSize <   1024) ?       (inSize>>4):
		 (inSize <   2048) ?  56 + (inSize>>7):
		 (inSize <   4096) ?  64 + (inSize>>8):
		 (inSize <   8192) ?  72 + (inSize>>9):
		 (inSize <  16384) ?  80 + (inSize>>10):
		 (inSize <  32768) ?  88 + (inSize>>11):
		 (inSize <  65536) ?  96 + (inSize>>12):
		 (inSize < 131072) ? 104 + (inSize>>13):
		 (inSize < 262144) ? 112 + (inSize>>14):127);
	}        
	
	static int BinIndex(size_t inSize) 
	{
		if (inSize < 1024) return inSize >> 4;
		if (inSize >= 262144) return 127;
		int bits = 28 - CLZ(inSize);
		return (bits << 3) + (inSize >> bits) ;
	}

	void MarkBinBlock(int inIndex)
		{
			unsigned long word = inIndex >> 5;
			unsigned long bitPosition = inIndex & 31;
			unsigned long bitValue = 1L << bitPosition;
			mBinBlocks[word] |= bitValue; 
		}
	
	void ClearBinBlock(int inIndex)
		{
			unsigned long word = inIndex >> 5;
			unsigned long bitPosition = inIndex & 31;
			unsigned long bitValue = 1L << bitPosition;
			mBinBlocks[word] &= ~bitValue; 
		}
		
	int NextFullBin(int inStartingBinIndex)
		{
			if (inStartingBinIndex >= 128) return -1;
			int word = inStartingBinIndex >> 5;
			int bitPosition = inStartingBinIndex & 31;
			unsigned long bitValue = 1L << bitPosition;
			unsigned long binBits = mBinBlocks[word];
			
			if (binBits >= bitValue) {
				binBits = (~(bitValue - 1) & binBits);
			} else {
				for (++word; word<4 && !mBinBlocks[word]; ++word) {}
				if (word==4) return -1;
				binBits = mBinBlocks[word];
			}
			bitPosition = CTZ((int32)binBits);

			return (word << 5) + bitPosition;
		}

	void LinkFree(AllocChunkPtr inChunk);
	/*
		{	
			size_t size = inChunk->Size();
			int index = BinIndex(size);
			
			AllocChunkPtr bin = mBins + index;

			if (index < kNumSmallBins || bin->IsEmpty()) {
				inChunk->InsertAfter(bin);
				MarkBinBlock(index);
			} else {
				AllocChunkPtr link = bin->Next();
				while (link != bin && size < link->Size()) link = link->Next();
				inChunk->InsertBefore(link);
			}
		}
	*/
	
	void UnlinkFree(AllocChunkPtr inChunk)
		{
			inChunk->RemoveLeaveDangling();
			size_t size = inChunk->Size();
			int index = BinIndex(size);
			AllocChunkPtr bin = mBins + index;
			if (bin->IsEmpty()) ClearBinBlock(index);
		}
	
	AllocChunk mBins[kNumAllocBins];
	AllocAreaPtr mAreas;
	NewAreaFunc mAllocArea;
	FreeAreaFunc mFreeArea;
	size_t mAreaInitSize, mAreaMoreSize;
	unsigned long mBinBlocks[4];
};

#endif