LinearAllocator.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /* Copyright (c) 2002-2012 Croteam Ltd.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of version 2 of the GNU General Public License as published by
  4. the Free Software Foundation
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License along
  10. with this program; if not, write to the Free Software Foundation, Inc.,
  11. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
  12. #ifndef SE_INCL_LINEARALLOCATOR_H
  13. #define SE_INCL_LINEARALLOCATOR_H
  14. #ifdef PRAGMA_ONCE
  15. #pragma once
  16. #endif
  17. /*
  18. * Template class for dynamic allocation of objects in a linear fashion.
  19. */
  20. template<class Type>
  21. class CLinearAllocator {
  22. // implementation:
  23. public:
  24. CListHead la_lhBlocks; // list of allocated memory blocks
  25. INDEX la_ctAllocationStep; // number of objects allocated
  26. INDEX la_ctObjects; // number of objects allocated
  27. INDEX la_ctFree; // number of free objects in current block
  28. Type *la_ptNextFree; // pointer to next free object
  29. // allocate a new memory block
  30. void AllocBlock(INDEX iCount);
  31. // interface:
  32. public:
  33. // default constructor
  34. CLinearAllocator(void);
  35. // copy constructor
  36. CLinearAllocator(CLinearAllocator<Type> &laOriginal);
  37. // destructor -- frees all memory
  38. ~CLinearAllocator(void);
  39. // destroy all objects, and reset the allocator to initial (empty) state
  40. void Clear(void);
  41. /* Set how many elements to allocate when stack overflows. */
  42. inline void SetAllocationStep(INDEX ctStep);
  43. // allocate a new object
  44. inline Type &New(void);
  45. inline Type *New(INDEX ct);
  46. // free all objects but keep allocated space and relinearize it
  47. inline void Reset(void);
  48. // these are just for compatibility with CDynamicStackArray<>:
  49. inline Type &Push(void) { return New(); };
  50. inline Type *Push(INDEX ct) { return New(ct); };
  51. inline void PopAll(void) { Reset(); };
  52. };
  53. #endif /* include-once check. */