Guest User

Untitled

a guest
Jun 20th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. #pragma once
  2.  
  3. //keeps track of alloc and dealloc count for objects
  4. #define OBJECT_POOL_DEBUG 0
  5.  
  6. //enabling this will turn off object pools and use malloc and free
  7. #define OBJECT_POOL_DEBUG_BATCH 0
  8.  
  9. #if OBJECT_POOL_DEBUG
  10. #define OBJECT_POOL_OBJECT_MACRO int allocated;
  11. #else
  12. #define OBJECT_POOL_OBJECT_MACRO
  13. #endif
  14.  
  15. template <class Base, class Object, int BUFFER_POOL_SIZE>
  16. class Object_pool {
  17. private:
  18. int batch_num;
  19. int instance_used;
  20. //batch malloc/link method
  21. void batch_alloc();
  22.  
  23. //garabage collection
  24. Object** alloc_list;
  25. int alloc_list_index;
  26. int alloc_list_max_size;
  27. //virtual char* name() { static char* x = (char*) "Error: generic object pool"; return x; }
  28.  
  29. public:
  30.  
  31. Object* first;
  32. Object* last;
  33.  
  34. inline Object* acquire() __attribute__((always_inline));
  35.  
  36. inline void retire(Object* nmb) __attribute__((always_inline));
  37.  
  38. Object_pool();
  39. ~Object_pool();
  40. };
  41.  
  42. template <class Base, class Object, int BUFFER_POOL_SIZE>
  43. Object_pool<Base, Object, BUFFER_POOL_SIZE>::Object_pool()
  44. {
  45. batch_num = 0;
  46. first = NULL;
  47.  
  48. alloc_list = new Object*[16];
  49. alloc_list_max_size = 16;
  50. alloc_list_index = 0;
  51. }
  52.  
  53. template <class Base, class Object, int BUFFER_POOL_SIZE>
  54. Object_pool<Base, Object, BUFFER_POOL_SIZE>::~Object_pool()
  55. {
  56. for(int i=0; i<alloc_list_index; i++ ) delete[] alloc_list[i];
  57. delete[] alloc_list;
  58. }
  59.  
  60.  
  61. template <class Base, class Object, int BUFFER_POOL_SIZE>
  62. void Object_pool<Base, Object, BUFFER_POOL_SIZE>::batch_alloc()
  63. {
  64. batch_num++;
  65.  
  66. Object* ar = new Object[BUFFER_POOL_SIZE];
  67.  
  68. #if OBJECT_POOL_DEBUG
  69. for(int i=0; i<BUFFER_POOL_SIZE; i++) ar[i].allocated = 0;
  70. #endif
  71.  
  72. first = &ar[0];
  73.  
  74. for(int i=0;i<BUFFER_POOL_SIZE-1; i++)
  75. {
  76. ar[i].next = &ar[i+1];
  77. }
  78. ar[BUFFER_POOL_SIZE-1].next = NULL;
  79.  
  80. //static char* _name = name();
  81. printf("%s: Batch Alloc: %i n_elements: %i \n", Base::name(), batch_num, BUFFER_POOL_SIZE);
  82.  
  83.  
  84. alloc_list[alloc_list_index] = ar;
  85. alloc_list_index++;
  86.  
  87. if( alloc_list_index == alloc_list_max_size)
  88. {
  89. printf("void Object_pool<Base, Object, BUFFER_POOL_SIZE>::batch_alloc(), possible memory leak!\n");
  90. Object** tmp = new Object*[2*alloc_list_max_size];
  91. for(int i=0; i < alloc_list_max_size; i++) tmp[i] = alloc_list[i];
  92. delete[] alloc_list;
  93. alloc_list = tmp;
  94. alloc_list_max_size *= 2;
  95. }
  96.  
  97. }
  98.  
  99. template <class Base, class Object, int BUFFER_POOL_SIZE>
  100. Object* Object_pool<Base, Object, BUFFER_POOL_SIZE>::acquire()
  101. {
  102. #if OBJECT_POOL_DEBUG_BATCH
  103. Object* tmp2 = (Object*) malloc(sizeof(Object));
  104. tmp2->next = NULL; //debug
  105. return tmp2;
  106. #endif
  107.  
  108. if(first == NULL)
  109. {
  110. batch_alloc();
  111. }
  112. Object* tmp = first;
  113. first = first->next;
  114.  
  115. #if OBJECT_POOL_DEBUG
  116. if(tmp->allocated != 0)
  117. {
  118. //static const char* _name = name();
  119. printf("%s: Memory Pool Acquire Error, allocated= %i, object= %lx \n", Base::name(), tmp->allocated, (long) tmp );
  120. //int segfault = *((int*) NULL);
  121. }
  122. tmp->allocated++;
  123. #endif
  124. return tmp;
  125. }
  126.  
  127. template <class Base, class Object, int BUFFER_POOL_SIZE>
  128. void Object_pool<Base, Object, BUFFER_POOL_SIZE>::retire(Object* nmb)
  129. {
  130. #if OBJECT_POOL_DEBUG_BATCH
  131. free(nmb);
  132. return;
  133. #endif
  134.  
  135. #if OBJECT_POOL_DEBUG
  136. if(nmb->allocated != 1)
  137. {
  138. //static const char* _name = name();
  139. printf("%s: Memory Pool Retire Error, allocated= %i, object= %lx \n", Base::name(), nmb->allocated, (long) nmb );
  140. int segfault = *((int*) NULL);
  141. printf("sefault= %i", segfault);
  142. }
  143. nmb->allocated--;
  144. #endif
  145. nmb->next = first;
  146. first = nmb;
  147. }
Add Comment
Please, Sign In to add comment