Teensy 4.1 core updated for C++20
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.

86 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. #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. /* undefined behavior handler is called on typical malloc UB situations */
  29. typedef void (*smalloc_ub_handler)(struct smalloc_pool *, const void *);
  30. void sm_set_ub_handler(smalloc_ub_handler);
  31. int sm_align_pool(struct smalloc_pool *);
  32. int sm_set_pool(struct smalloc_pool *, void *, size_t, int, smalloc_oom_handler);
  33. int sm_set_default_pool(void *, size_t, int, smalloc_oom_handler);
  34. int sm_release_pool(struct smalloc_pool *);
  35. int sm_release_default_pool(void);
  36. /* Use these with multiple pools which you control */
  37. void *sm_malloc_pool(struct smalloc_pool *, size_t);
  38. void *sm_zalloc_pool(struct smalloc_pool *, size_t);
  39. void sm_free_pool(struct smalloc_pool *, void *);
  40. void *sm_realloc_pool(struct smalloc_pool *, void *, size_t);
  41. void *sm_realloc_move_pool(struct smalloc_pool *, void *, size_t);
  42. void *sm_calloc_pool(struct smalloc_pool *, size_t, size_t);
  43. int sm_alloc_valid_pool(struct smalloc_pool *spool, const void *p);
  44. size_t sm_szalloc_pool(struct smalloc_pool *, const void *);
  45. int sm_malloc_stats_pool(struct smalloc_pool *, size_t *, size_t *, size_t *, int *);
  46. /* Use these when you use just default smalloc_curr_pool pool */
  47. void *sm_malloc(size_t);
  48. void *sm_zalloc(size_t); /* guarantee zero memory allocation */
  49. void sm_free(void *);
  50. void *sm_realloc(void *, size_t);
  51. void *sm_realloc_move(void *, size_t);
  52. void *sm_calloc(size_t, size_t); /* calls zalloc internally */
  53. int sm_alloc_valid(const void *p); /* verify pointer without intentional crash */
  54. size_t sm_szalloc(const void *); /* get size of allocation */
  55. /*
  56. * get stats: total used, user used, total free, nr. of allocated blocks.
  57. * any of pointers maybe set to NULL, but at least one must be non NULL.
  58. */
  59. int sm_malloc_stats(size_t *, size_t *, size_t *, int *);
  60. #ifdef __cplusplus
  61. } // extern "C"
  62. #endif
  63. #endif