fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. template<class type, int blockSize>
  7. class idBlockAlloc {
  8. public:
  9. idBlockAlloc( void );
  10. ~idBlockAlloc( void );
  11.  
  12. void Shutdown( void );
  13.  
  14. type * Alloc( void );
  15. void Free( type *element );
  16.  
  17. int GetTotalCount( void ) const { return total; }
  18. int GetAllocCount( void ) const { return active; }
  19. int GetFreeCount( void ) const { return total - active; }
  20.  
  21. private:
  22. typedef struct element_s {
  23. struct element_s * next;
  24. type t;
  25. } element_t;
  26. typedef struct block_s {
  27. element_t elements[blockSize];
  28. struct block_s * next;
  29. } block_t;
  30.  
  31. block_t * blocks;
  32. element_t * free;
  33. int total;
  34. int active;
  35. };
  36.  
  37. template<class type, int blockSize>
  38. idBlockAlloc<type,blockSize>::idBlockAlloc( void ) {
  39. blocks = NULL;
  40. free = NULL;
  41. total = active = 0;
  42. }
  43.  
  44. template<class type, int blockSize>
  45. idBlockAlloc<type,blockSize>::~idBlockAlloc( void ) {
  46. Shutdown();
  47. }
  48.  
  49. template<class type, int blockSize>
  50. type *idBlockAlloc<type,blockSize>::Alloc( void ) {
  51. if ( !free ) {
  52. block_t *block = new block_t;
  53. block->next = blocks;
  54. blocks = block;
  55. for ( int i = 0; i < blockSize; i++ ) {
  56. block->elements[i].next = free;
  57. free = &block->elements[i];
  58. }
  59. total += blockSize;
  60. }
  61. active++;
  62. element_t *element = free;
  63. free = free->next;
  64. element->next = NULL;
  65. return &element->t;
  66. }
  67.  
  68. template<class type, int blockSize>
  69. void idBlockAlloc<type,blockSize>::Free( type *t ) {
  70. element_t *element = (element_t *)( ( (unsigned char *) t ) - ( (int) &((element_t *)0)->t ) );
  71. element->next = free;
  72. free = element;
  73. active--;
  74. }
  75.  
  76. template<class type, int blockSize>
  77. void idBlockAlloc<type,blockSize>::Shutdown( void ) {
  78. while( blocks ) {
  79. block_t *block = blocks;
  80. blocks = blocks->next;
  81. delete block;
  82. }
  83. blocks = NULL;
  84. free = NULL;
  85. total = active = 0;
  86. }
  87.  
  88. struct SomeClass{
  89. SomeClass(){
  90. cout << "SomeClass()" << endl;
  91. }
  92.  
  93. std::vector<int> data;
  94. };
  95.  
  96. int main() {
  97. idBlockAlloc<SomeClass,32> eventAllocator;
  98. SomeClass* event = eventAllocator.Alloc();
  99. event->data.push_back(1);
  100.  
  101. eventAllocator.Free( event );
  102.  
  103. event = eventAllocator.Alloc();
  104. event->data.push_back(2);
  105.  
  106. cout << event->data.size() << endl;
  107. eventAllocator.Free( event );
  108. }
  109.  
Success #stdin #stdout 0.01s 2816KB
stdin
Standard input is empty
stdout
SomeClass()
SomeClass()
SomeClass()
SomeClass()
SomeClass()
SomeClass()
SomeClass()
SomeClass()
SomeClass()
SomeClass()
SomeClass()
SomeClass()
SomeClass()
SomeClass()
SomeClass()
SomeClass()
SomeClass()
SomeClass()
SomeClass()
SomeClass()
SomeClass()
SomeClass()
SomeClass()
SomeClass()
SomeClass()
SomeClass()
SomeClass()
SomeClass()
SomeClass()
SomeClass()
SomeClass()
SomeClass()
2