block_pool.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. Copyright (c) 2009 Joseph A. Adams
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. 1. Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. 2. Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. 3. The name of the author may not be used to endorse or promote products
  13. derived from this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  15. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "block_pool.h"
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. //must be a power of 2
  30. #define BLOCK_SIZE 4096
  31. struct block {
  32. size_t remaining;
  33. size_t size;
  34. char *data;
  35. };
  36. struct block_pool {
  37. size_t count;
  38. size_t alloc; //2^n - 1, where n is an integer > 1
  39. struct block *block;
  40. //blocks are arranged in a max-heap by the .remaining field
  41. // (except the root block does not percolate down until it is filled)
  42. };
  43. static int destructor(struct block_pool *bp) {
  44. struct block *block = bp->block;
  45. size_t d = bp->count;
  46. for (;d--;block++)
  47. free(block->data);
  48. free(bp->block);
  49. return 0;
  50. }
  51. struct block_pool *block_pool_new(void *ctx) {
  52. struct block_pool *bp = talloc(ctx, struct block_pool);
  53. talloc_set_destructor(bp, destructor);
  54. bp->count = 0;
  55. bp->alloc = 7;
  56. bp->block = malloc(bp->alloc * sizeof(struct block));
  57. return bp;
  58. }
  59. static void *new_block(struct block *b, size_t needed) {
  60. b->size = (needed+(BLOCK_SIZE-1)) & ~(BLOCK_SIZE-1);
  61. b->remaining = b->size - needed;
  62. b->data = malloc(b->size);
  63. return b->data;
  64. }
  65. //for the first block, keep the memory usage low in case it's the only block.
  66. static void *new_block_tiny(struct block *b, size_t needed) {
  67. if (needed < 256)
  68. b->size = 256;
  69. else
  70. b->size = (needed+(BLOCK_SIZE-1)) & ~(BLOCK_SIZE-1);
  71. b->remaining = b->size - needed;
  72. b->data = malloc(b->size);
  73. return b->data;
  74. }
  75. static void *try_block(struct block *b, size_t size, size_t align) {
  76. size_t offset = b->size - b->remaining;
  77. offset = (offset+align) & ~align;
  78. if (b->size-offset >= size) {
  79. //good, we can use this block
  80. void *ret = b->data + offset;
  81. b->remaining = b->size-offset-size;
  82. return ret;
  83. }
  84. return NULL;
  85. }
  86. #define L(node) (node+node+1)
  87. #define R(node) (node+node+2)
  88. #define P(node) ((node-1)>>1)
  89. #define V(node) (bp->block[node].remaining)
  90. static void percolate_down(struct block_pool *bp, size_t node) {
  91. size_t child = L(node);
  92. struct block tmp;
  93. //get the maximum child
  94. if (child >= bp->count)
  95. return;
  96. if (child+1 < bp->count && V(child+1) > V(child))
  97. child++;
  98. if (V(child) <= V(node))
  99. return;
  100. tmp = bp->block[node];
  101. bp->block[node] = bp->block[child];
  102. bp->block[child] = tmp;
  103. percolate_down(bp, child);
  104. }
  105. //note: percolates up to either 1 or 2 as a root
  106. static void percolate_up(struct block_pool *bp, size_t node) {
  107. size_t parent = P(node);
  108. struct block tmp;
  109. if (node<3 || V(parent) >= V(node))
  110. return;
  111. tmp = bp->block[node];
  112. bp->block[node] = bp->block[parent];
  113. bp->block[parent] = tmp;
  114. percolate_up(bp, parent);
  115. }
  116. void *block_pool_alloc_align(struct block_pool *bp, size_t size, size_t align) {
  117. void *ret;
  118. if (align)
  119. align--;
  120. //if there aren't any blocks, make a new one
  121. if (!bp->count) {
  122. bp->count = 1;
  123. return new_block_tiny(bp->block, size);
  124. }
  125. //try the root block
  126. ret = try_block(bp->block, size, align);
  127. if (ret)
  128. return ret;
  129. //root block is filled, percolate down and try the biggest one
  130. percolate_down(bp, 0);
  131. ret = try_block(bp->block, size, align);
  132. if (ret)
  133. return ret;
  134. //the biggest wasn't big enough; we need a new block
  135. if (bp->count >= bp->alloc) {
  136. //make room for another block
  137. bp->alloc += bp->alloc;
  138. bp->alloc++;
  139. bp->block = realloc(bp->block, bp->alloc * sizeof(struct block));
  140. }
  141. ret = new_block(bp->block+(bp->count++), size);
  142. //fix the heap after adding the new block
  143. percolate_up(bp, bp->count-1);
  144. return ret;
  145. }
  146. #undef L
  147. #undef R
  148. #undef P
  149. #undef V
  150. char *block_pool_strdup(struct block_pool *bp, const char *str) {
  151. size_t size = strlen(str)+1;
  152. char *ret = block_pool_alloc_align(bp, size, 1);
  153. memcpy(ret, str, size);
  154. return ret;
  155. }