Guest User

Untitled

a guest
Jun 20th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 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. }
  58.  
  59.  
  60. template <class Base, class Object, int BUFFER_POOL_SIZE>
  61. void Object_pool<Base, Object, BUFFER_POOL_SIZE>::batch_alloc()
  62. {
  63. batch_num++;
  64.  
  65. Object* ar = new Object[BUFFER_POOL_SIZE];
  66.  
  67. #if OBJECT_POOL_DEBUG
  68. for(int i=0; i<BUFFER_POOL_SIZE; i++) ar[i].allocated = 0;
  69. #endif
  70.  
  71. first = &ar[0];
  72.  
  73. for(int i=0;i<BUFFER_POOL_SIZE-1; i++)
  74. {
  75. ar[i].next = &ar[i+1];
  76. }
  77. ar[BUFFER_POOL_SIZE-1].next = NULL;
  78.  
  79. //static char* _name = name();
  80. printf("%s: Batch Alloc: %i n_elements: %i \n", Base::name(), batch_num, BUFFER_POOL_SIZE);
  81.  
  82.  
  83. alloc_list[alloc_list_index] = ar;
  84. alloc_list_index++;
  85.  
  86. if( alloc_list_index == alloc_list_max_size)
  87. {
  88. printf("void Object_pool<Base, Object, BUFFER_POOL_SIZE>::batch_alloc(), possible memory leak!\n");
  89. Object** tmp = new Object*[2*alloc_list_max_size];
  90. for(int i=0; i < alloc_list_max_size; i++) tmp[i] = alloc_list[i];
  91. delete[] alloc_list;
  92. alloc_list = tmp;
  93. alloc_list_max_size *= 2;
  94. }
  95.  
  96. }
  97.  
  98. template <class Base, class Object, int BUFFER_POOL_SIZE>
  99. Object* Object_pool<Base, Object, BUFFER_POOL_SIZE>::acquire()
  100. {
  101. #if OBJECT_POOL_DEBUG_BATCH
  102. Object* tmp2 = (Object*) malloc(sizeof(Object));
  103. tmp2->next = NULL; //debug
  104. return tmp2;
  105. #endif
  106.  
  107. if(first == NULL)
  108. {
  109. batch_alloc();
  110. }
  111. Object* tmp = first;
  112. first = first->next;
  113.  
  114. #if OBJECT_POOL_DEBUG
  115. if(tmp->allocated != 0)
  116. {
  117. //static const char* _name = name();
  118. printf("%s: Memory Pool Acquire Error, allocated= %i, object= %lx \n", Base::name(), tmp->allocated, (long) tmp );
  119. //int segfault = *((int*) NULL);
  120. }
  121. tmp->allocated++;
  122. #endif
  123. return tmp;
  124. }
  125.  
  126. template <class Base, class Object, int BUFFER_POOL_SIZE>
  127. void Object_pool<Base, Object, BUFFER_POOL_SIZE>::retire(Object* nmb)
  128. {
  129. #if OBJECT_POOL_DEBUG_BATCH
  130. free(nmb);
  131. return;
  132. #endif
  133.  
  134. #if OBJECT_POOL_DEBUG
  135. if(nmb->allocated != 1)
  136. {
  137. //static const char* _name = name();
  138. printf("%s: Memory Pool Retire Error, allocated= %i, object= %lx \n", Base::name(), nmb->allocated, (long) nmb );
  139. int segfault = *((int*) NULL);
  140. printf("sefault= %i", segfault);
  141. }
  142. nmb->allocated--;
  143. #endif
  144. nmb->next = first;
  145. first = nmb;
  146. }
Add Comment
Please, Sign In to add comment