1
0

Allocator.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. static void onFree(void* voj)
  78. {
  79. struct Allocator_OnFreeJob* oj = Identity_check((struct Allocator_OnFreeJob*) voj);
  80. oj->callback(oj);
  81. }
  82. struct Allocator_OnFreeJob* Allocator__onFree(struct Allocator* alloc,
  83. Allocator_OnFreeCallback callback,
  84. void* callbackContext,
  85. const char* file,
  86. int line)
  87. {
  88. struct Allocator_OnFreeJob* oj = Allocator__malloc(alloc, sizeof(struct Allocator_OnFreeJob), file, line);
  89. Identity_set(oj);
  90. oj->callback = callback;
  91. oj->userData = callbackContext;
  92. Rffi_allocator_onFree(alloc, onFree, oj, file, line);
  93. return oj;
  94. }
  95. struct Allocator* Allocator__new(unsigned long sizeLimit,
  96. const char* fileName,
  97. int lineNum)
  98. {
  99. return Rffi_allocator_newRoot(fileName, lineNum);
  100. }
  101. unsigned long Allocator_bytesAllocated(struct Allocator* allocator)
  102. {
  103. return 0;
  104. }
  105. void Allocator_snapshot(struct Allocator* alloc, int includeAllocations)
  106. {
  107. }
  108. void Allocator_setCanary(struct Allocator* alloc, uintptr_t value)
  109. {
  110. }