tsShapeAlloc.cpp
Engine/source/ts/tsShapeAlloc.cpp
Public Defines
define
IMPLEMENT_ALLOC(suffix, type)
define
readOnly() (mMode==, "TSShapeAlloc: write-only function called when reading")
define
writeOnly() (mMode==,"TSShapeAlloc: read-only function called when writing")
Detailed Description
Public Defines
IMPLEMENT_ALLOC(suffix, type)
readOnly() (mMode==, "TSShapeAlloc: write-only function called when reading")
writeOnly() (mMode==,"TSShapeAlloc: read-only function called when writing")
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#include "ts/tsShapeAlloc.h" 25 26#define readOnly() AssertFatal(mMode==TSShapeAlloc::ReadMode, "TSShapeAlloc: write-only function called when reading") 27#define writeOnly() AssertFatal(mMode==TSShapeAlloc::WriteMode,"TSShapeAlloc: read-only function called when writing") 28 29void TSShapeAlloc::setRead(S32 * memBuffer32, S16 * memBuffer16, S8 * memBuffer8, bool clear) 30{ 31 mMemBuffer32 = memBuffer32; 32 mMemBuffer16 = memBuffer16; 33 mMemBuffer8 = memBuffer8 ; 34 35 mMemGuard32 = 0; 36 mMemGuard16 = 0; 37 mMemGuard8 = 0; 38 39 mSaveGuard32 = 0; 40 mSaveGuard16 = 0; 41 mSaveGuard8 = 0; 42 43 if (clear) 44 { 45 mDest = NULL; 46 mSize = 0; 47 } 48 49 setSkipMode(false); 50 mMode = TSShapeAlloc::ReadMode; 51} 52 53void TSShapeAlloc::setWrite() 54{ 55 mMemBuffer32 = 0; 56 mMemBuffer16 = 0; 57 mMemBuffer8 = 0; 58 59 mSize32 = mFullSize32 = 0; 60 mSize16 = mFullSize16 = 0; 61 mSize8 = mFullSize8 = 0; 62 63 mMemGuard32 = 0; 64 mMemGuard16 = 0; 65 mMemGuard8 = 0; 66 67 setSkipMode(false); // doesn't really do anything here... 68 mMode = TSShapeAlloc::WriteMode; 69} 70 71void TSShapeAlloc::doAlloc() 72{ 73 readOnly(); 74 75 mDest = new S8[mSize]; 76 mSize = 0; 77} 78 79void TSShapeAlloc::align32() 80{ 81 readOnly(); 82 83 S32 aligned = mSize+3 & (~0x3); 84 allocShape8(aligned-mSize); 85} 86 87#define IMPLEMENT_ALLOC(suffix,type) \ 88 \ 89type TSShapeAlloc::get##suffix() \ 90{ \ 91 readOnly(); \ 92 return *(mMemBuffer##suffix++); \ 93} \ 94 \ 95void TSShapeAlloc::get##suffix(type * dest, S32 num) \ 96{ \ 97 readOnly(); \ 98 dMemcpy(dest,mMemBuffer##suffix,sizeof(type)*num); \ 99 mMemBuffer##suffix += num; \ 100} \ 101 \ 102type * TSShapeAlloc::allocShape##suffix(S32 num) \ 103{ \ 104 readOnly(); \ 105 type * ret = (type*) mDest; \ 106 if (mDest) \ 107 mDest += mMult*num*sizeof(type); \ 108 mSize += sizeof(type)*mMult*num; \ 109 return ret; \ 110} \ 111 \ 112type * TSShapeAlloc::getPointer##suffix(S32 num) \ 113{ \ 114 readOnly(); \ 115 type * ret = (type*)mMemBuffer##suffix; \ 116 mMemBuffer##suffix += num; \ 117 return ret; \ 118} \ 119 \ 120type * TSShapeAlloc::copyToShape##suffix(S32 num, bool returnSomething) \ 121{ \ 122 readOnly(); \ 123 type * ret = (!returnSomething || mDest) ? (type*)mDest : mMemBuffer##suffix; \ 124 if (mDest) \ 125 { \ 126 dMemcpy((S8*)mDest,(S8*)mMemBuffer##suffix, \ 127 mMult*num*sizeof(type)); \ 128 mDest += mMult*num*sizeof(type); \ 129 } \ 130 mMemBuffer##suffix += num; \ 131 mSize += sizeof(type)*mMult*num; \ 132 return ret; \ 133} \ 134 \ 135bool TSShapeAlloc::checkGuard##suffix() \ 136{ \ 137 readOnly(); \ 138 mSaveGuard##suffix=get##suffix(); \ 139 bool ret = (mSaveGuard##suffix==mMemGuard##suffix); \ 140 mMemGuard##suffix += 1; \ 141 return ret; \ 142} \ 143 \ 144type TSShapeAlloc::getPrevGuard##suffix() \ 145{ \ 146 readOnly(); \ 147 return mMemGuard##suffix - 1; \ 148} \ 149 \ 150type TSShapeAlloc::getSaveGuard##suffix() \ 151{ \ 152 readOnly(); \ 153 return mSaveGuard##suffix; \ 154} \ 155 \ 156type * TSShapeAlloc::getBuffer##suffix() \ 157{ \ 158 writeOnly(); \ 159 return mMemBuffer##suffix; \ 160} \ 161 \ 162S32 TSShapeAlloc::getBufferSize##suffix() \ 163{ \ 164 writeOnly(); \ 165 return mSize##suffix; \ 166} \ 167 \ 168type * TSShapeAlloc::extend##suffix(S32 add) \ 169{ \ 170 writeOnly(); \ 171 if (mSize##suffix+add>mFullSize##suffix) \ 172 { \ 173 S32 numPages = 1+(mFullSize##suffix+add)/TSShapeAlloc::PageSize; \ 174 mFullSize##suffix = numPages*TSShapeAlloc::PageSize; \ 175 type * temp = new type[mFullSize##suffix]; \ 176 dMemcpy(temp,mMemBuffer##suffix, mSize##suffix * sizeof(type)); \ 177 delete [] mMemBuffer##suffix; \ 178 mMemBuffer##suffix = temp; \ 179 } \ 180 type * ret = mMemBuffer##suffix + mSize##suffix; \ 181 mSize##suffix += add; \ 182 return ret; \ 183} \ 184 \ 185type TSShapeAlloc::set##suffix(type entry) \ 186{ \ 187 writeOnly(); \ 188 *extend##suffix(1) = entry; \ 189 return entry; \ 190} \ 191 \ 192void TSShapeAlloc::copyToBuffer##suffix(type * entries, S32 count) \ 193{ \ 194 writeOnly(); \ 195 if (entries) \ 196 dMemcpy((U8*)extend##suffix(count),(U8*)entries,count*sizeof(type)); \ 197 else \ 198 dMemset((U8*)extend##suffix(count),0,count*sizeof(type)); \ 199} \ 200 \ 201void TSShapeAlloc::setGuard##suffix() \ 202{ \ 203 writeOnly(); \ 204 set##suffix(mMemGuard##suffix); \ 205 mMemGuard##suffix += 1; \ 206} 207 208IMPLEMENT_ALLOC(32,S32) 209IMPLEMENT_ALLOC(16,S16) 210IMPLEMENT_ALLOC(8,S8) 211 212void TSShapeAlloc::checkGuard() 213{ 214 bool check32 = checkGuard32(); 215 bool check16 = checkGuard16(); 216 bool check8 = checkGuard8(); 217 AssertFatal(check32,avar("TSShapeAlloc::checkGuard32: found %i, wanted %i",getSaveGuard32(),getPrevGuard32())); 218 AssertFatal(check16,avar("TSShapeAlloc::checkGuard16: found %i, wanted %i",getSaveGuard16(),getPrevGuard16())); 219 AssertFatal(check8 ,avar("TSShapeAlloc::checkGuard8: found %i, wanted %i",getSaveGuard8() ,getPrevGuard8())); 220} 221 222void TSShapeAlloc::setGuard() 223{ 224 setGuard32(); 225 setGuard16(); 226 setGuard8(); 227} 228 229 230