Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

103 lines
3.2KB

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