You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.7KB

  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. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. struct smalloc_pool;
  18. typedef size_t (*smalloc_oom_handler)(struct smalloc_pool *, size_t);
  19. /* describes static pool, if you're going to use multiple pools at same time */
  20. struct smalloc_pool {
  21. void *pool; /* pointer to your pool */
  22. size_t pool_size; /* it's size. Must be aligned with sm_align_pool. */
  23. int do_zero; /* zero pool before use and all the new allocations from it. */
  24. smalloc_oom_handler oomfn; /* this will be called, if non-NULL, on OOM condition in pool */
  25. };
  26. /* a default one which is initialised with sm_set_default_pool. */
  27. extern struct smalloc_pool smalloc_curr_pool;
  28. #ifdef ARDUINO_TEENSY41
  29. extern struct smalloc_pool extmem_smalloc_pool;
  30. #endif
  31. /* undefined behavior handler is called on typical malloc UB situations */
  32. typedef void (*smalloc_ub_handler)(struct smalloc_pool *, const void *);
  33. void sm_set_ub_handler(smalloc_ub_handler);
  34. int sm_align_pool(struct smalloc_pool *);
  35. int sm_set_pool(struct smalloc_pool *, void *, size_t, int, smalloc_oom_handler);
  36. int sm_set_default_pool(void *, size_t, int, smalloc_oom_handler);
  37. int sm_release_pool(struct smalloc_pool *);
  38. int sm_release_default_pool(void);
  39. /* Use these with multiple pools which you control */
  40. void *sm_malloc_pool(struct smalloc_pool *, size_t);
  41. void *sm_zalloc_pool(struct smalloc_pool *, size_t);
  42. void sm_free_pool(struct smalloc_pool *, void *);
  43. void *sm_realloc_pool(struct smalloc_pool *, void *, size_t);
  44. void *sm_realloc_move_pool(struct smalloc_pool *, void *, size_t);
  45. void *sm_calloc_pool(struct smalloc_pool *, size_t, size_t);
  46. int sm_alloc_valid_pool(struct smalloc_pool *spool, const void *p);
  47. size_t sm_szalloc_pool(struct smalloc_pool *, const void *);
  48. int sm_malloc_stats_pool(struct smalloc_pool *, size_t *, size_t *, size_t *, int *);
  49. /* Use these when you use just default smalloc_curr_pool pool */
  50. void *sm_malloc(size_t);
  51. void *sm_zalloc(size_t); /* guarantee zero memory allocation */
  52. void sm_free(void *);
  53. void *sm_realloc(void *, size_t);
  54. void *sm_realloc_move(void *, size_t);
  55. void *sm_calloc(size_t, size_t); /* calls zalloc internally */
  56. int sm_alloc_valid(const void *p); /* verify pointer without intentional crash */
  57. size_t sm_szalloc(const void *); /* get size of allocation */
  58. /*
  59. * get stats: total used, user used, total free, nr. of allocated blocks.
  60. * any of pointers maybe set to NULL, but at least one must be non NULL.
  61. */
  62. int sm_malloc_stats(size_t *, size_t *, size_t *, int *);
  63. #ifdef __cplusplus
  64. } // extern "C"
  65. #endif
  66. #endif