2 SuperCollider real time audio synthesis system
3 Copyright (c) 2002 James McCartney. All rights reserved.
4 http://www.audiosynth.com
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.
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.
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
22 #include "AdvancingAllocPool.h"
23 #include "SC_AllocPool.h"
27 AdvancingAllocPool::AdvancingAllocPool()
38 void AdvancingAllocPool::Init(AllocPool
*inAllocPool
, size_t initSize
, size_t growSize
, size_t tooBigSize
)
40 mAllocPool
= inAllocPool
;
47 //assert(SanityCheck());
50 void AdvancingAllocPool::AddChunk(size_t inSize
)
52 size_t chunkSize
= sizeof(AdvancingAllocPoolChunkHdr
) + inSize
;
53 AdvancingAllocPoolChunk
* chunk
= (AdvancingAllocPoolChunk
*)mAllocPool
->Alloc(chunkSize
);
55 chunk
->mNext
= mChunks
;
57 chunk
->mSize
= mGrowSize
;
61 void* AdvancingAllocPool::Alloc(size_t reqsize
)
63 //assert(SanityCheck());
65 size_t size
= (reqsize
+ 15) & ~15; // round up to 16 byte alignment
67 if (!mChunks
) AddChunk(mInitSize
);
68 else if (mCurSize
+ size
> mChunks
->mSize
) AddChunk(mGrowSize
);
69 char* space
= mChunks
->mSpace
+ mCurSize
;
72 //assert(SanityCheck());
75 size_t chunkSize
= sizeof(AdvancingAllocPoolChunkHdr
) + size
;
76 AdvancingAllocPoolChunk
* fatty
= (AdvancingAllocPoolChunk
*)mAllocPool
->Alloc(chunkSize
);
78 fatty
->mNext
= mFatties
;
82 //assert(SanityCheck());
83 return (void*)fatty
->mSpace
;
87 void AdvancingAllocPool::FreeAll()
89 //assert(SanityCheck());
90 AdvancingAllocPoolChunk
*chunk
, *next
;
91 for (chunk
= mChunks
; chunk
; chunk
= next
) {
93 mAllocPool
->Free(chunk
);
95 for (chunk
= mFatties
; chunk
; chunk
= next
) {
97 mAllocPool
->Free(chunk
);
102 //assert(SanityCheck());
105 bool AdvancingAllocPool::SanityCheck()
107 AdvancingAllocPoolChunk
*chunk
, *next
;
108 for (chunk
= mChunks
; chunk
; chunk
= next
) {
110 mAllocPool
->DoCheckInUseChunk(AllocPool::MemToChunk(chunk
));
112 for (chunk
= mFatties
; chunk
; chunk
= next
) {
114 mAllocPool
->DoCheckInUseChunk(AllocPool::MemToChunk(chunk
));