Allocator.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "memory/Allocator.h"
  16. #include "rust/cjdns_sys/Rffi.h"
  17. #include <stdio.h>
  18. void Allocator__free(struct Allocator* alloc, const char* file, int line)
  19. {
  20. Rffi_allocator_free(alloc, file, (uintptr_t) line);
  21. }
  22. int Allocator_isFreeing(Allocator_t* alloc)
  23. {
  24. return Rffi_allocator_isFreeing(alloc);
  25. }
  26. void* Allocator__malloc(struct Allocator* allocator,
  27. unsigned long length,
  28. const char* fileName,
  29. int lineNum)
  30. {
  31. return (void*) Rffi_allocator_malloc(allocator, length);
  32. }
  33. void* Allocator__calloc(struct Allocator* alloc,
  34. unsigned long length,
  35. unsigned long count,
  36. const char* fileName,
  37. int lineNum)
  38. {
  39. void* pointer = Allocator__malloc(alloc, length * count, fileName, lineNum);
  40. Bits_memset(pointer, 0, length * count);
  41. return pointer;
  42. }
  43. void* Allocator__realloc(struct Allocator* allocator,
  44. const void* original,
  45. unsigned long size,
  46. const char* fileName,
  47. int lineNum)
  48. {
  49. return (void*) Rffi_allocator_realloc(allocator, (uint8_t*) original, size);
  50. }
  51. void* Allocator__clone(struct Allocator* allocator,
  52. const void* toClone,
  53. unsigned long length,
  54. const char* fileName,
  55. int lineNum)
  56. {
  57. void* pointer = Allocator__malloc(allocator, length, fileName, lineNum);
  58. Bits_memcpy(pointer, toClone, length);
  59. return pointer;
  60. }
  61. struct Allocator* Allocator__child(struct Allocator* allocator, const char* file, int line)
  62. {
  63. return Rffi_allocator_child(allocator, file, line);
  64. }
  65. int Allocator_cancelOnFree(struct Allocator_OnFreeJob* toRemove)
  66. {
  67. /// XXX
  68. return 1;
  69. }
  70. void Allocator__adopt(struct Allocator* adoptedParent,
  71. struct Allocator* childToAdopt,
  72. const char* file,
  73. int line)
  74. {
  75. Rffi_allocator_adopt(adoptedParent, childToAdopt);
  76. }
  77. void Allocator_onFreeComplete(struct Allocator_OnFreeJob* onFreeJob)
  78. {
  79. Identity_check(onFreeJob);
  80. Rffi_allocator_onFreeComplete((OnFreeCtx*) onFreeJob->rContext);
  81. }
  82. static void onFree(void* voj, OnFreeCtx *complete)
  83. {
  84. struct Allocator_OnFreeJob* oj = Identity_check((struct Allocator_OnFreeJob*) voj);
  85. oj->rContext = (void*) complete;
  86. if (oj->callback(oj) != Allocator_ONFREE_ASYNC) {
  87. Rffi_allocator_onFreeComplete(complete);
  88. }
  89. }
  90. struct Allocator_OnFreeJob* Allocator__onFree(struct Allocator* alloc,
  91. Allocator_OnFreeCallback callback,
  92. void* callbackContext,
  93. const char* file,
  94. int line)
  95. {
  96. struct Allocator_OnFreeJob* oj = Allocator__malloc(alloc, sizeof(struct Allocator_OnFreeJob), file, line);
  97. Identity_set(oj);
  98. oj->callback = callback;
  99. oj->userData = callbackContext;
  100. oj->rContext = NULL;
  101. Rffi_allocator_onFree(alloc, onFree, oj, file, line);
  102. return oj;
  103. }
  104. struct Allocator* Allocator__new(unsigned long sizeLimit,
  105. const char* fileName,
  106. int lineNum)
  107. {
  108. return Rffi_allocator_newRoot(fileName, lineNum);
  109. }
  110. unsigned long Allocator_bytesAllocated(struct Allocator* allocator)
  111. {
  112. return 0;
  113. }
  114. void Allocator_snapshot(struct Allocator* alloc, int includeAllocations)
  115. {
  116. }
  117. void Allocator_setCanary(struct Allocator* alloc, uintptr_t value)
  118. {
  119. }