Isty01

Untitled

Oct 23rd, 2016
669
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. #include <stdbool.h>
  2. #include "mem_pool.h"
  3.  
  4.  
  5. typedef struct block Block;
  6.  
  7. typedef struct block
  8. {
  9. Block *next;
  10. void *ptr;
  11. bool free;
  12. } Block;
  13.  
  14. struct mem_pool
  15. {
  16. Block *head;
  17. void *memory;
  18. size_t memory_size;
  19. size_t memb_size;
  20. };
  21.  
  22.  
  23. #define get_ptr_and_increase(pool, size) pool->memory; pool->memory += size
  24.  
  25. #define add_block_size(pool) pool->memory_size += sizeof(Block) + pool->memb_size
  26.  
  27. /**
  28. * Creates an internal representation of a memory block
  29. */
  30. static Block *new_block(MemoryPool *pool, bool is_free)
  31. {
  32. Block *block = get_ptr_and_increase(pool, sizeof(Block));
  33. block->ptr = get_ptr_and_increase(pool, pool->memb_size);
  34. block->next = NULL;
  35. block->free = is_free;
  36.  
  37. return block;
  38. }
  39.  
  40. /**
  41. * Initializes the pool with the given size of members
  42. */
  43. MemoryPool *pool_init(size_t memb_size)
  44. {
  45. MemoryPool *pool = malloc(sizeof(MemoryPool));
  46. pool->memb_size = memb_size;
  47. pool->memory_size = 0;
  48. add_block_size(pool);
  49. pool->memory = malloc(pool->memory_size);
  50. pool->head = new_block(pool, true);
  51.  
  52. return pool;
  53. }
  54.  
  55. /**
  56. * Internally looks for the first free Block
  57. */
  58. static Block *find_free(MemoryPool *pool)
  59. {
  60. Block *current = pool->head;
  61.  
  62. while (NULL != current && false == current->free) {
  63. current = current->next;
  64. }
  65.  
  66. return current;
  67. }
  68.  
  69. /**
  70. * Adds a new Block to the beginning of the list if no other block is free
  71. */
  72. static void add_new_head(MemoryPool *pool)
  73. {
  74. Block *tmp = pool->head;
  75. pool->head = new_block(pool, false);
  76. pool->head->next = tmp;
  77. add_block_size(pool);
  78. pool->memory = realloc(pool->memory, pool->memory_size);
  79. }
  80.  
  81. /**
  82. * Finds or creates a new Block, but only returns the void pointer to the user
  83. */
  84. void *pool_alloc(MemoryPool *pool)
  85. {
  86. Block *free = find_free(pool);
  87.  
  88. if (NULL != free) {
  89. return free->ptr;
  90. }
  91. add_new_head(pool);
  92.  
  93. return pool->head->ptr;
  94. }
  95.  
  96. /**
  97. * Sets the block free, thus alloweing the pool the give it to the user again
  98. */
  99. void pool_free(MemoryPool *pool, void *block)
  100. {
  101. Block *current = pool->head;
  102.  
  103. while (current->ptr != block) {
  104. current = current->next;
  105. }
  106.  
  107. current->free = true;
  108. }
  109.  
  110. void pool_destroy(MemoryPool *pool)
  111. {
  112. free(pool->memory - pool->memory_size);
  113. free(pool);
  114. }
Advertisement
Add Comment
Please, Sign In to add comment