mem_pool.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #include "mem_pool.h"
  2. #define TEST int
  3. #define OFFSET_SIZE sizeof(int)
  4. #define OFFSET_NEXT_PTR_SIZE sizeof(void *)
  5. #define OFFSET_PRE_PTR_BEGIN OFFSET_SIZE + OFFSET_NEXT_PTR_SIZE
  6. #define OFFSET_PRE_PTR_SIZE sizeof(void *)
  7. #define MIN_ALLOC_CNT OFFSET_SIZE + OFFSET_NEXT_PTR_SIZE + OFFSET_PRE_PTR_SIZE
  8. struct msg_pool *
  9. create_pool(int max_size) {
  10. struct msg_pool* p = (struct msg_pool*)malloc(sizeof(*p));
  11. memset(p, 0, sizeof(*p));
  12. p->max_size = max_size;
  13. p->free_cnt = max_size;
  14. p->list_cnt = 1;
  15. p->data_ptr = malloc(max_size);
  16. p->free_list = p->data_ptr;
  17. *(TEST*)p->free_list = max_size;
  18. void** next = (void**)((char *)p->data_ptr + OFFSET_SIZE);
  19. *next = NULL;
  20. void** pre = (void**)((char *)p->data_ptr + OFFSET_PRE_PTR_BEGIN);
  21. *pre = NULL;
  22. return p;
  23. }
  24. static struct msg_pool *
  25. msg_pool_expand(struct msg_pool* p) {
  26. assert(p && p->max_size > 0);
  27. while (p->next) {
  28. p = p->next;
  29. }
  30. p->next = create_pool(p->max_size);
  31. return p->next;
  32. }
  33. void *
  34. msg_pool_alloc(struct msg_pool* p, size_t size) {
  35. size_t real_size = size + OFFSET_SIZE;
  36. real_size = real_size < MIN_ALLOC_CNT ? MIN_ALLOC_CNT : real_size;
  37. assert(real_size <= p->max_size);
  38. void* ret = NULL;
  39. if (p->free_cnt >= real_size) {
  40. void* block = p->free_list;
  41. assert(block != NULL);
  42. while (block) {
  43. void* next = *(void **)((char*)block + OFFSET_SIZE);
  44. TEST block_cnt = *(TEST*)block;
  45. assert(block_cnt >= MIN_ALLOC_CNT && block_cnt <= p->max_size);
  46. if (block_cnt < real_size) {
  47. block = next;
  48. continue;
  49. }
  50. if (block_cnt - real_size < MIN_ALLOC_CNT) {
  51. real_size = block_cnt;
  52. }
  53. void **pre_next = NULL;
  54. void *pre_block = *(void **)((char *)block + OFFSET_PRE_PTR_BEGIN);
  55. if (pre_block) {
  56. pre_next = (void **)((char*)pre_block + OFFSET_SIZE);
  57. assert(*pre_next == block);
  58. }
  59. if (0 == block_cnt - real_size) {
  60. if (pre_next && *pre_next) {
  61. *pre_next = next;
  62. }
  63. else {
  64. p->free_list = next;
  65. }
  66. if (next) {
  67. *(void **)((char *)next + OFFSET_PRE_PTR_BEGIN) = pre_block;
  68. }
  69. p->list_cnt -= 1;
  70. }
  71. else {
  72. void *rest_block = (char *)block + real_size;
  73. *(TEST*)rest_block = block_cnt - real_size;
  74. *(void **)((char *)rest_block + OFFSET_PRE_PTR_BEGIN) = pre_block;
  75. *(void **)((char *)rest_block + OFFSET_SIZE) = *(void **)((char *)block + OFFSET_SIZE);
  76. if (next) {
  77. *(void **)((char *)next + OFFSET_PRE_PTR_BEGIN) = rest_block;
  78. }
  79. if (pre_next && *pre_next) {
  80. *pre_next = rest_block;
  81. }
  82. else {
  83. p->free_list = rest_block;
  84. }
  85. }
  86. *(TEST*)block = real_size;
  87. ret = (char*)block + OFFSET_SIZE;
  88. p->free_cnt -= real_size;
  89. return ret;
  90. }
  91. }
  92. if (p->next) {
  93. return msg_pool_alloc(p->next, size);
  94. }
  95. else {
  96. return msg_pool_alloc(msg_pool_expand(p), size);
  97. }
  98. }
  99. static void
  100. msg_pool_delete_one(struct msg_pool* p) {
  101. free(p->data_ptr);
  102. free(p);
  103. }
  104. void
  105. msg_pool_free(struct msg_pool* p, void* ptr, char* freed) {
  106. if (ptr == NULL || p == NULL) return;
  107. *freed = 0;
  108. ptr = (char*)ptr - OFFSET_SIZE;
  109. struct msg_pool *pre_p = p;
  110. while (p != NULL) {
  111. void *max_ptr = (char*)p->data_ptr + p->max_size;
  112. if (ptr >= p->data_ptr && ptr < max_ptr) {
  113. break;
  114. }
  115. pre_p = p;
  116. p = p->next;
  117. }
  118. if (p == NULL) {
  119. fprintf(stderr, "error: call msg_pool free a block not alloc from pool");
  120. return;
  121. }
  122. TEST size = *(TEST*)ptr;
  123. memset((char*)ptr + OFFSET_SIZE, 0, MIN_ALLOC_CNT - OFFSET_SIZE);
  124. assert(size >= MIN_ALLOC_CNT && size <= p->max_size);
  125. void *block = p->free_list;
  126. void *pre_block = NULL;
  127. while (block && block > ptr) {
  128. void* next = *(void **)((char*)block + OFFSET_SIZE);
  129. pre_block = block;
  130. block = next;
  131. }
  132. if (block) {
  133. *(void **)((char *)block + OFFSET_PRE_PTR_BEGIN) = ptr;
  134. }
  135. if (pre_block) {
  136. void **pre_next = (void **)((char*)pre_block + OFFSET_SIZE);
  137. *pre_next = ptr;
  138. }
  139. *(void **)((char *)ptr + OFFSET_PRE_PTR_BEGIN) = pre_block;
  140. *(void **)((char*)ptr + OFFSET_SIZE) = block;
  141. //merge
  142. int merge_cnt = block ? 2 : 1;
  143. void* merge_ptr = block ? block : ptr;
  144. while (merge_cnt > 0 && merge_ptr) {
  145. TEST size = *(TEST*)merge_ptr;
  146. void* pre_ptr = *(void **)((char *)merge_ptr + OFFSET_PRE_PTR_BEGIN);
  147. if ((char *)merge_ptr + size == pre_ptr) {
  148. TEST pre_size = *(TEST*)pre_ptr;
  149. *(TEST*)merge_ptr = size + pre_size;
  150. void* pre_pre_ptr = *(void **)((char *)pre_ptr + OFFSET_PRE_PTR_BEGIN);
  151. *(void **)((char *)merge_ptr + OFFSET_PRE_PTR_BEGIN) = pre_pre_ptr;
  152. if (pre_pre_ptr) {
  153. *(void **)((char*)pre_pre_ptr + OFFSET_SIZE) = merge_ptr;
  154. }
  155. if (pre_ptr == p->free_list) {
  156. p->free_list = merge_ptr;
  157. }
  158. --p->list_cnt;
  159. }
  160. else {
  161. if (p->free_list < merge_ptr) {
  162. p->free_list = merge_ptr;
  163. }
  164. merge_ptr = pre_ptr;
  165. }
  166. --merge_cnt;
  167. }
  168. p->free_cnt += size;
  169. p->list_cnt += 1;
  170. if (p->free_cnt == p->max_size && pre_p != p) {
  171. pre_p->next = p->next;
  172. //delete p
  173. msg_pool_delete_one(p);
  174. *freed = 1;
  175. }
  176. }
  177. void
  178. msg_pool_delete(struct msg_pool *p) {
  179. if (p == NULL) return;
  180. do {
  181. struct msg_pool* next = p->next;
  182. msg_pool_delete_one(p);
  183. p = next;
  184. } while (p != NULL);
  185. }
  186. /*
  187. int
  188. main(int argc, char *argv[]) {
  189. msg_pool *p = create_pool(1024);
  190. void *b1 = msg_pool_alloc(p, 300);
  191. void *b2 = msg_pool_alloc(p , 600);
  192. bool b;
  193. msg_pool_free(p, b1, b);
  194. void *b3 = msg_pool_alloc(p, 96);
  195. void *b4 = msg_pool_alloc(p, 100);
  196. void *b5 = msg_pool_alloc(p, 90);
  197. msg_pool_free(p, b2, b);
  198. msg_pool_free(p, b3, b);
  199. msg_pool_free(p, b4, b);
  200. msg_pool_free(p, b5, b);
  201. int alloced = 0;
  202. int max_alloced = 1024;
  203. void **vec_alloc = (void **)malloc(sizeof(void *) * 1024);
  204. int tmp1 = 0, tmp2 = 0;
  205. while (1) {
  206. srand(time(0));
  207. int r = rand() % 2;
  208. if (r) {
  209. if (alloced >= max_alloced) {
  210. void **tmp = (void **)malloc(sizeof(void *) * max_alloced * 2);
  211. memcpy(tmp, vec_alloc, max_alloced * sizeof(void *));
  212. max_alloced *= 2;
  213. free(vec_alloc);
  214. vec_alloc = tmp;
  215. }
  216. int size = rand() % 1020 + 1;
  217. void* b = msg_pool_alloc(p, size);
  218. vec_alloc[alloced++] = b;
  219. memset(b, 0, size);
  220. tmp1++;
  221. }
  222. else {
  223. if (alloced > 0) {
  224. void *b = vec_alloc[0];
  225. char freed = 0;
  226. msg_pool_free(p, b, &freed);
  227. alloced -= 1;
  228. memmove(vec_alloc, (char*)vec_alloc + sizeof(void *), alloced * sizeof(void *));
  229. if (!freed) {
  230. void *ptr = (char*)b - OFFSET_SIZE;
  231. TEST size = *(TEST*)ptr;
  232. memset((char *)ptr + MIN_ALLOC_CNT, 0, size - MIN_ALLOC_CNT);
  233. }
  234. tmp2++;
  235. }
  236. }
  237. usleep(1);
  238. }
  239. msg_pool_delete(p);
  240. return 0;
  241. }
  242. */