1 /*
2     SuperCollider real time audio synthesis system
3     Copyright (c) 2002 James McCartney. All rights reserved.
4     http://www.audiosynth.com
5 
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10 
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
19 */
20 /*
21 
22 AdvancingAllocPool implements a simple advancing pointer allocation scheme.
23 There is no Free(). All objects in the pool are freed at once with FreeAll().
24 Thus it is very fast.
25 
26 */
27 
28 #pragma once
29 
30 #include <stdexcept>
31 #include <stdlib.h>
32 
33 class AllocPool;
34 
35 struct AdvancingAllocPoolChunk;
36 
37 typedef int int32;
38 
FailNil(void * ptr)39 inline void FailNil(void* ptr) {
40     if (!ptr)
41         throw std::runtime_error("alloc failed");
42 }
43 
44 struct AdvancingAllocPoolChunkHdr {
45     AdvancingAllocPoolChunk* mNext;
46     size_t mSize;
47     int32 mPad1, mPad2;
48 };
49 
50 struct AdvancingAllocPoolChunk {
51     AdvancingAllocPoolChunk* mNext;
52     size_t mSize;
53     int32 mPad1, mPad2;
54     char mSpace[16];
55 };
56 
57 class AdvancingAllocPool {
58 public:
59     AdvancingAllocPool();
~AdvancingAllocPool()60     ~AdvancingAllocPool() { FreeAll(); }
61 
62     void Init(AllocPool* inAllocPool, size_t initSize, size_t growSize, size_t tooBigSize);
63 
64     void* Alloc(size_t inBytes);
65     void FreeAll();
66 
67     bool SanityCheck();
68 
69 private:
70     void AddChunk(size_t inSize);
71 
72     AllocPool* mAllocPool;
73     size_t mInitSize;
74     size_t mGrowSize;
75     size_t mCurSize;
76     size_t mTooBig;
77     AdvancingAllocPoolChunk* mChunks;
78     AdvancingAllocPoolChunk* mFatties;
79 };
80