MiniHeap.h 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #if !defined(MINIHEAP_H_INC)
  2. #define MINIHEAP_H_INC
  3. class CMiniHeap
  4. {
  5. char *mHeap;
  6. char *mCurrentHeap;
  7. int mSize;
  8. #if _DEBUG
  9. int mMaxAlloc;
  10. #endif
  11. public:
  12. // reset the heap back to the start
  13. void ResetHeap()
  14. {
  15. #if _DEBUG
  16. if ((int)mCurrentHeap - (int)mHeap>mMaxAlloc)
  17. {
  18. mMaxAlloc=(int)mCurrentHeap - (int)mHeap;
  19. }
  20. #endif
  21. mCurrentHeap = mHeap;
  22. }
  23. // initialise the heap
  24. CMiniHeap(int size)
  25. {
  26. mHeap = (char *)Z_Malloc(size, TAG_GHOUL2, qtrue);
  27. mSize = size;
  28. #if _DEBUG
  29. mMaxAlloc=0;
  30. #endif
  31. if (mHeap)
  32. {
  33. ResetHeap();
  34. }
  35. }
  36. // free up the heap
  37. ~CMiniHeap()
  38. {
  39. if (mHeap)
  40. {
  41. // the quake heap will be long gone, no need to free it Z_Free(mHeap);
  42. }
  43. }
  44. // give me some space from the heap please
  45. char *MiniHeapAlloc(int size)
  46. {
  47. if (size < (mSize - ((int)mCurrentHeap - (int)mHeap)))
  48. {
  49. char *tempAddress = mCurrentHeap;
  50. mCurrentHeap += size;
  51. return tempAddress;
  52. }
  53. return NULL;
  54. }
  55. };
  56. extern CMiniHeap *G2VertSpaceServer;
  57. #endif //MINIHEAP_H_INC