1
0

dblockpool_free.c 975 B

12345678910111213141516171819202122232425262728293031323334
  1. #include <stdlib.h>
  2. #include "private.h"
  3. /*--------------------------------------------------------- */
  4. void lub_dblockpool_free(lub_dblockpool_t * this, void *block)
  5. {
  6. lub_dblockpool_chunk_t **chunk_ptr;
  7. /* find the chunk from which this derived. */
  8. for (chunk_ptr = &this->first_chunk;
  9. *chunk_ptr; chunk_ptr = &(*chunk_ptr)->next) {
  10. const char *pool_start = (char *)&(*chunk_ptr)[1];
  11. const char *pool_end =
  12. pool_start + (this->block_size * this->chunk_size);
  13. const char *ptr = block;
  14. if ((ptr >= pool_start) && (ptr < pool_end)) {
  15. /* found the right pool */
  16. lub_blockpool_free(&(*chunk_ptr)->pool, block);
  17. (*chunk_ptr)->count--;
  18. /* track the number of allocations */
  19. if (0 == (*chunk_ptr)->count) {
  20. lub_dblockpool_chunk_t *tmp = *chunk_ptr;
  21. /* removing last block from this chunk */
  22. *chunk_ptr = (*chunk_ptr)->next;
  23. free(tmp);
  24. }
  25. break;
  26. }
  27. }
  28. }
  29. /*--------------------------------------------------------- */