Skip to content
This repository
tree: e28db25801
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 119 lines (103 sloc) 3.19 kb
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
/*
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/


#include "AdvancingAllocPool.h"
#include "SC_AllocPool.h"
//#include <assert.h>


AdvancingAllocPool::AdvancingAllocPool()
{
mAllocPool = 0;
mInitSize = 0;
mGrowSize = 0;
mTooBig = 0;
mCurSize = 0;
mChunks = NULL;
mFatties = NULL;
}

void AdvancingAllocPool::Init(AllocPool *inAllocPool, size_t initSize, size_t growSize, size_t tooBigSize)
{
mAllocPool = inAllocPool;
mInitSize = initSize;
mGrowSize = growSize;
mTooBig = tooBigSize;
mChunks = NULL;
AddChunk(initSize);
mFatties = NULL;
//assert(SanityCheck());
}

void AdvancingAllocPool::AddChunk(size_t inSize)
{
size_t chunkSize = sizeof(AdvancingAllocPoolChunkHdr) + inSize;
AdvancingAllocPoolChunk* chunk = (AdvancingAllocPoolChunk*)mAllocPool->Alloc(chunkSize);
FailNil(chunk);
chunk->mNext = mChunks;
mChunks = chunk;
chunk->mSize = mGrowSize;
mCurSize = 0;
}

void* AdvancingAllocPool::Alloc(size_t reqsize)
{
//assert(SanityCheck());
//assert(mAllocPool);
size_t size = (reqsize + 15) & ~15; // round up to 16 byte alignment
if (size < mTooBig) {
if (!mChunks) AddChunk(mInitSize);
else if (mCurSize + size > mChunks->mSize) AddChunk(mGrowSize);
char* space = mChunks->mSpace + mCurSize;
mCurSize += size;

//assert(SanityCheck());
return (void*)space;
} else {
size_t chunkSize = sizeof(AdvancingAllocPoolChunkHdr) + size;
AdvancingAllocPoolChunk* fatty = (AdvancingAllocPoolChunk*)mAllocPool->Alloc(chunkSize);
FailNil(fatty);
fatty->mNext = mFatties;
mFatties = fatty;
fatty->mSize = size;

//assert(SanityCheck());
return (void*)fatty->mSpace;
}
}

void AdvancingAllocPool::FreeAll()
{
//assert(SanityCheck());
AdvancingAllocPoolChunk *chunk, *next;
for (chunk = mChunks; chunk; chunk = next) {
next = chunk->mNext;
mAllocPool->Free(chunk);
}
for (chunk = mFatties; chunk; chunk = next) {
next = chunk->mNext;
mAllocPool->Free(chunk);
}
mChunks = NULL;
mFatties = NULL;
mCurSize = 0;
//assert(SanityCheck());
}

bool AdvancingAllocPool::SanityCheck()
{
AdvancingAllocPoolChunk *chunk, *next;
for (chunk = mChunks; chunk; chunk = next) {
next = chunk->mNext;
mAllocPool->DoCheckInUseChunk(AllocPool::MemToChunk(chunk));
}
for (chunk = mFatties; chunk; chunk = next) {
next = chunk->mNext;
mAllocPool->DoCheckInUseChunk(AllocPool::MemToChunk(chunk));
}
return true;
}

Something went wrong with that request. Please try again.