Teensy 4.1 core updated for C++20
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

sm_pool.c 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * This file is a part of SMalloc.
  3. * SMalloc is MIT licensed.
  4. * Copyright (c) 2017 Andrey Rys.
  5. */
  6. #include "smalloc_i.h"
  7. struct smalloc_pool smalloc_curr_pool;
  8. int smalloc_verify_pool(struct smalloc_pool *spool)
  9. {
  10. if (!spool->pool || !spool->pool_size) return 0;
  11. if (spool->pool_size % HEADER_SZ) return 0;
  12. return 1;
  13. }
  14. int sm_align_pool(struct smalloc_pool *spool)
  15. {
  16. size_t x;
  17. if (smalloc_verify_pool(spool)) return 1;
  18. x = spool->pool_size % HEADER_SZ;
  19. if (x) spool->pool_size -= x;
  20. if (spool->pool_size <= MIN_POOL_SZ) {
  21. errno = ENOSPC;
  22. return 0;
  23. }
  24. return 1;
  25. }
  26. int sm_set_pool(struct smalloc_pool *spool, void *new_pool, size_t new_pool_size, int do_zero, smalloc_oom_handler oom_handler)
  27. {
  28. if (!spool) {
  29. errno = EINVAL;
  30. return 0;
  31. }
  32. if (!new_pool || !new_pool_size) {
  33. if (smalloc_verify_pool(spool)) {
  34. if (spool->do_zero) memset(spool->pool, 0, spool->pool_size);
  35. memset(spool, 0, sizeof(struct smalloc_pool));
  36. return 1;
  37. }
  38. errno = EINVAL;
  39. return 0;
  40. }
  41. spool->pool = new_pool;
  42. spool->pool_size = new_pool_size;
  43. spool->oomfn = oom_handler;
  44. if (!sm_align_pool(spool)) return 0;
  45. if (do_zero) {
  46. spool->do_zero = do_zero;
  47. memset(spool->pool, 0, spool->pool_size);
  48. }
  49. return 1;
  50. }
  51. int sm_set_default_pool(void *new_pool, size_t new_pool_size, int do_zero, smalloc_oom_handler oom_handler)
  52. {
  53. return sm_set_pool(&smalloc_curr_pool, new_pool, new_pool_size, do_zero, oom_handler);
  54. }
  55. int sm_release_pool(struct smalloc_pool *spool)
  56. {
  57. return sm_set_pool(spool, NULL, 0, 0, NULL);
  58. }
  59. int sm_release_default_pool(void)
  60. {
  61. return sm_release_pool(&smalloc_curr_pool);
  62. }