heap.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. uint32_t length;
  21. uint32_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 = (uint32_t)(__builtin_wasm_memory_size(0) * 0x10000 - (size_t)first - sizeof(struct chunk));
  31. }
  32. #else
  33. #define HEAP_START ((struct chunk *)0x300000000)
  34. void __init_heap()
  35. {
  36. struct chunk *first = HEAP_START;
  37. first->next = first->prev = NULL;
  38. first->allocated = false;
  39. first->length = (32 * 1024) - sizeof(struct chunk);
  40. }
  41. #endif
  42. void __attribute__((noinline)) __free(void *m)
  43. {
  44. struct chunk *cur = m;
  45. cur--;
  46. if (m)
  47. {
  48. cur->allocated = false;
  49. struct chunk *next = cur->next;
  50. if (next && !next->allocated)
  51. {
  52. // merge with next
  53. if ((cur->next = next->next) != NULL)
  54. cur->next->prev = cur;
  55. cur->length += next->length + sizeof(struct chunk);
  56. next = cur->next;
  57. }
  58. struct chunk *prev = cur->prev;
  59. if (prev && !prev->allocated)
  60. {
  61. // merge with previous
  62. prev->next = next;
  63. next->prev = prev;
  64. prev->length += cur->length + sizeof(struct chunk);
  65. }
  66. }
  67. }
  68. static void shrink_chunk(struct chunk *cur, uint32_t size)
  69. {
  70. // round up to nearest 8 bytes
  71. size = (size + 7) & ~7;
  72. if (cur->length - size >= (8 + sizeof(struct chunk)))
  73. {
  74. // split and return
  75. void *data = (cur + 1);
  76. struct chunk *new = data + size;
  77. if ((new->next = cur->next) != NULL)
  78. new->next->prev = new;
  79. cur->next = new;
  80. new->prev = cur;
  81. new->allocated = false;
  82. new->length = cur->length - size - sizeof(struct chunk);
  83. cur->length = size;
  84. }
  85. }
  86. void *__attribute__((noinline)) __malloc(uint32_t size)
  87. {
  88. struct chunk *cur = HEAP_START;
  89. while (cur && (cur->allocated || size > cur->length))
  90. cur = cur->next;
  91. if (cur)
  92. {
  93. shrink_chunk(cur, size);
  94. cur->allocated = true;
  95. return ++cur;
  96. }
  97. else
  98. {
  99. // go bang
  100. #ifdef __wasm__
  101. __builtin_unreachable();
  102. #else
  103. sol_log("out of heap memory");
  104. sol_panic();
  105. #endif
  106. return NULL;
  107. }
  108. }
  109. void *__realloc(void *m, uint32_t size)
  110. {
  111. struct chunk *cur = m;
  112. cur--;
  113. struct chunk *next = cur->next;
  114. if (next && !next->allocated && size <= (cur->length + next->length + sizeof(struct chunk)))
  115. {
  116. // merge with next
  117. cur->next = next->next;
  118. if (cur->next)
  119. cur->next->prev = cur;
  120. cur->length += next->length + sizeof(struct chunk);
  121. // resplit ..
  122. shrink_chunk(cur, size);
  123. return m;
  124. }
  125. else
  126. {
  127. // allocate new area and copy old data
  128. uint32_t len = cur->length;
  129. // if new size is smaller than the old data, only copy remaining data
  130. if (size < len)
  131. len = size;
  132. void *n = __malloc(size);
  133. // __memcpy8() copies 8 bytes at once; round up to the nearest 8 bytes
  134. // this is permitted because allocations are always aligned on 8 byte
  135. // boundaries anyway.
  136. __memcpy8(n, m, (len + 7) / 8);
  137. __free(m);
  138. return n;
  139. }
  140. }