mempool.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #pragma once
  2. #ifndef ELL_MEMPOOL_H
  3. #define ELL_MEMPOOL_H
  4. #include <stdlib.h>
  5. struct ell__mempool_chunk;
  6. struct ell_mempool {
  7. size_t element_size;
  8. size_t chunk_size;
  9. size_t num_chunks;
  10. size_t capacity; /*!< number of elements allocated */
  11. size_t size; /*!< number of elements used */
  12. struct ell__mempool_chunk* chunks;
  13. void* next_free;
  14. };
  15. void ell_mempool_init(struct ell_mempool* pool, size_t element_size);
  16. void ell_mempool_init2(struct ell_mempool* pool, size_t element_size, size_t reserve);
  17. void ell_mempool_init3(struct ell_mempool* pool,
  18. size_t element_size,
  19. size_t reserve,
  20. size_t chunk_size);
  21. void ell_mempool_free(struct ell_mempool* pool);
  22. void ell_mempool_reserve(struct ell_mempool* pool, size_t reserve);
  23. /* Memory management */
  24. void* ell_mempool_get(struct ell_mempool* pool);
  25. void ell_mempool_ret(struct ell_mempool* pool, void* ptr);
  26. /* Queries */
  27. size_t ell_mempool_size(const struct ell_mempool* pool);
  28. size_t ell_mempool_capacity(const struct ell_mempool* pool);
  29. /* Typedefs */
  30. #ifdef ELL_CONVENIENCE_TYPEDEFS
  31. #ifndef ELL_CONVENIENCE_TYPEDEF_SUFFIX
  32. #define ELL_CONVENIENCE_TYPEDEF_SUFFIX _t
  33. #endif
  34. #define ELL__CONVENIENCE_TYPEDEF_PASTER(x, y) x##y
  35. #define ELL__CONVENIENCE_TYPEDEF_EVAL(x, y) ELL__CONVENIENCE_TYPEDEF_PASTER(x, y)
  36. #define ELL_CONVENIENCE_TYPEDEF(type) \
  37. ELL__CONVENIENCE_TYPEDEF_EVAL(type, ELL_CONVENIENCE_TYPEDEF_SUFFIX)
  38. typedef struct ell_mempool ELL_CONVENIENCE_TYPEDEF(ell_mempool);
  39. #endif
  40. #endif