heap.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // SPDX-License-Identifier: Apache-2.0
  2. #include <stdint.h>
  3. #include <stddef.h>
  4. #include <stdbool.h>
  5. #include "stdlib.h"
  6. #ifndef __wasm__
  7. #include "solana_sdk.h"
  8. #endif
  9. /*
  10. There are many tradeoffs in heaps. I think for Solidity, we want:
  11. - small code size to reduce code length
  12. - not many malloc objects
  13. - not much memory
  14. So I think we should avoid fragmentation by neighbour merging. The most
  15. costly is walking the doubly linked list looking for free space.
  16. */
  17. struct chunk
  18. {
  19. struct chunk *next, *prev;
  20. size_t length;
  21. size_t allocated;
  22. };
  23. #ifdef __wasm__
  24. #define HEAP_START ((struct chunk *)0x10000)
  25. void __init_heap()
  26. {
  27. struct chunk *first = HEAP_START;
  28. first->next = first->prev = NULL;
  29. first->allocated = false;
  30. first->length = (size_t)(__builtin_wasm_memory_size(0) * 0x10000 -
  31. (size_t)first - sizeof(struct chunk));
  32. }
  33. #else
  34. #define HEAP_START ((struct chunk *)0x300000000)
  35. void __init_heap()
  36. {
  37. struct chunk *first = HEAP_START;
  38. first->next = first->prev = NULL;
  39. first->allocated = false;
  40. first->length = (32 * 1024) - sizeof(struct chunk);
  41. }
  42. #endif
  43. void __attribute__((noinline)) __free(void *m)
  44. {
  45. struct chunk *cur = m;
  46. cur--;
  47. if (m)
  48. {
  49. cur->allocated = false;
  50. struct chunk *next = cur->next;
  51. if (next && !next->allocated)
  52. {
  53. // merge with next
  54. if ((cur->next = next->next) != NULL)
  55. cur->next->prev = cur;
  56. cur->length += next->length + sizeof(struct chunk);
  57. next = cur->next;
  58. }
  59. struct chunk *prev = cur->prev;
  60. if (prev && !prev->allocated)
  61. {
  62. // merge with previous
  63. prev->next = next;
  64. next->prev = prev;
  65. prev->length += cur->length + sizeof(struct chunk);
  66. }
  67. }
  68. }
  69. static void shrink_chunk(struct chunk *cur, size_t size)
  70. {
  71. // round up to nearest 8 bytes
  72. size = (size + 7) & ~7;
  73. if (cur->length - size >= (8 + sizeof(struct chunk)))
  74. {
  75. // split and return
  76. void *data = (cur + 1);
  77. struct chunk *new = data + size;
  78. if ((new->next = cur->next) != NULL)
  79. new->next->prev = new;
  80. cur->next = new;
  81. new->prev = cur;
  82. new->allocated = false;
  83. new->length = cur->length - size - sizeof(struct chunk);
  84. cur->length = size;
  85. }
  86. }
  87. void *__attribute__((noinline)) __malloc(uint32_t size)
  88. {
  89. struct chunk *cur = HEAP_START;
  90. while (cur && (cur->allocated || size > cur->length))
  91. cur = cur->next;
  92. if (cur)
  93. {
  94. shrink_chunk(cur, size);
  95. cur->allocated = true;
  96. return ++cur;
  97. }
  98. else
  99. {
  100. // go bang
  101. #ifdef __wasm__
  102. __builtin_unreachable();
  103. #else
  104. sol_log("out of heap memory");
  105. sol_panic();
  106. #endif
  107. return NULL;
  108. }
  109. }
  110. void *__realloc(void *m, size_t size)
  111. {
  112. struct chunk *cur = m;
  113. cur--;
  114. struct chunk *next = cur->next;
  115. if (next && !next->allocated && size <= (cur->length + next->length + sizeof(struct chunk)))
  116. {
  117. // merge with next
  118. cur->next = next->next;
  119. if (cur->next)
  120. cur->next->prev = cur;
  121. cur->length += next->length + sizeof(struct chunk);
  122. // resplit ..
  123. shrink_chunk(cur, size);
  124. return m;
  125. }
  126. else
  127. {
  128. // allocate new area and copy old data
  129. uint32_t len = cur->length;
  130. // if new size is smaller than the old data, only copy remaining data
  131. if (size < len)
  132. len = size;
  133. void *n = __malloc(size);
  134. // __memcpy8() copies 8 bytes at once; round up to the nearest 8 bytes
  135. // this is permitted because allocations are always aligned on 8 byte
  136. // boundaries anyway.
  137. __memcpy8(n, m, (len + 7) / 8);
  138. __free(m);
  139. return n;
  140. }
  141. }