Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

78 lines
2.6KB

  1. /*
  2. * SMalloc -- a *static* memory allocator.
  3. *
  4. * See README for a complete description.
  5. *
  6. * SMalloc is MIT licensed.
  7. * Copyright (c) 2017 Andrey Rys.
  8. * Written during Aug2017.
  9. */
  10. #ifndef _SMALLOC_H
  11. #define _SMALLOC_H
  12. #include <stddef.h>
  13. #include <stdint.h>
  14. struct smalloc_pool;
  15. typedef size_t (*smalloc_oom_handler)(struct smalloc_pool *, size_t);
  16. /* describes static pool, if you're going to use multiple pools at same time */
  17. struct smalloc_pool {
  18. void *pool; /* pointer to your pool */
  19. size_t pool_size; /* it's size. Must be aligned with sm_align_pool. */
  20. int do_zero; /* zero pool before use and all the new allocations from it. */
  21. smalloc_oom_handler oomfn; /* this will be called, if non-NULL, on OOM condition in pool */
  22. };
  23. /* a default one which is initialised with sm_set_default_pool. */
  24. extern struct smalloc_pool smalloc_curr_pool;
  25. /* undefined behavior handler is called on typical malloc UB situations */
  26. typedef void (*smalloc_ub_handler)(struct smalloc_pool *, const void *);
  27. void sm_set_ub_handler(smalloc_ub_handler);
  28. int sm_align_pool(struct smalloc_pool *);
  29. int sm_set_pool(struct smalloc_pool *, void *, size_t, int, smalloc_oom_handler);
  30. int sm_set_default_pool(void *, size_t, int, smalloc_oom_handler);
  31. int sm_release_pool(struct smalloc_pool *);
  32. int sm_release_default_pool(void);
  33. /* Use these with multiple pools which you control */
  34. void *sm_malloc_pool(struct smalloc_pool *, size_t);
  35. void *sm_zalloc_pool(struct smalloc_pool *, size_t);
  36. void sm_free_pool(struct smalloc_pool *, void *);
  37. void *sm_realloc_pool(struct smalloc_pool *, void *, size_t);
  38. void *sm_realloc_move_pool(struct smalloc_pool *, void *, size_t);
  39. void *sm_calloc_pool(struct smalloc_pool *, size_t, size_t);
  40. int sm_alloc_valid_pool(struct smalloc_pool *spool, const void *p);
  41. size_t sm_szalloc_pool(struct smalloc_pool *, const void *);
  42. int sm_malloc_stats_pool(struct smalloc_pool *, size_t *, size_t *, size_t *, int *);
  43. /* Use these when you use just default smalloc_curr_pool pool */
  44. void *sm_malloc(size_t);
  45. void *sm_zalloc(size_t); /* guarantee zero memory allocation */
  46. void sm_free(void *);
  47. void *sm_realloc(void *, size_t);
  48. void *sm_realloc_move(void *, size_t);
  49. void *sm_calloc(size_t, size_t); /* calls zalloc internally */
  50. int sm_alloc_valid(const void *p); /* verify pointer without intentional crash */
  51. size_t sm_szalloc(const void *); /* get size of allocation */
  52. /*
  53. * get stats: total used, user used, total free, nr. of allocated blocks.
  54. * any of pointers maybe set to NULL, but at least one must be non NULL.
  55. */
  56. int sm_malloc_stats(size_t *, size_t *, size_t *, int *);
  57. #endif