tsShapeAlloc.cpp 11 KB

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