mem-pool.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #ifndef MEM_POOL_H
  2. #define MEM_POOL_H
  3. struct mp_block {
  4. struct mp_block *next_block;
  5. char *next_free;
  6. char *end;
  7. uintmax_t space[FLEX_ARRAY]; /* more */
  8. };
  9. struct mem_pool {
  10. struct mp_block *mp_block;
  11. /*
  12. * The amount of available memory to grow the pool by.
  13. * This size does not include the overhead for the mp_block.
  14. */
  15. size_t block_alloc;
  16. /* The total amount of memory allocated by the pool. */
  17. size_t pool_alloc;
  18. };
  19. /*
  20. * Initialize mem_pool with specified initial size.
  21. */
  22. void mem_pool_init(struct mem_pool *pool, size_t initial_size);
  23. /*
  24. * Discard all the memory the memory pool is responsible for.
  25. */
  26. void mem_pool_discard(struct mem_pool *pool, int invalidate_memory);
  27. /*
  28. * Alloc memory from the mem_pool.
  29. */
  30. void *mem_pool_alloc(struct mem_pool *pool, size_t len);
  31. /*
  32. * Allocate and zero memory from the memory pool.
  33. */
  34. void *mem_pool_calloc(struct mem_pool *pool, size_t count, size_t size);
  35. /*
  36. * Allocate memory from the memory pool and copy str into it.
  37. */
  38. char *mem_pool_strdup(struct mem_pool *pool, const char *str);
  39. char *mem_pool_strndup(struct mem_pool *pool, const char *str, size_t len);
  40. /*
  41. * Move the memory associated with the 'src' pool to the 'dst' pool. The 'src'
  42. * pool will be empty and not contain any memory. It still needs to be free'd
  43. * with a call to `mem_pool_discard`.
  44. */
  45. void mem_pool_combine(struct mem_pool *dst, struct mem_pool *src);
  46. /*
  47. * Check if a memory pointed at by 'mem' is part of the range of
  48. * memory managed by the specified mem_pool.
  49. */
  50. int mem_pool_contains(struct mem_pool *pool, void *mem);
  51. #endif