arena.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright 2022 Alexey Kutepov <[email protected]>
  2. // Permission is hereby granted, free of charge, to any person obtaining
  3. // a copy of this software and associated documentation files (the
  4. // "Software"), to deal in the Software without restriction, including
  5. // without limitation the rights to use, copy, modify, merge, publish,
  6. // distribute, sublicense, and/or sell copies of the Software, and to
  7. // permit persons to whom the Software is furnished to do so, subject to
  8. // the following conditions:
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  12. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  13. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  14. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  15. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  16. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  17. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. #ifndef ARENA_H_
  19. #define ARENA_H_
  20. #include <stddef.h>
  21. #include <stdint.h>
  22. #ifndef ARENA_ASSERT
  23. #include <assert.h>
  24. #define ARENA_ASSERT assert
  25. #endif
  26. #define ARENA_BACKEND_LIBC_MALLOC 0
  27. #define ARENA_BACKEND_LINUX_MMAP 1
  28. #define ARENA_BACKEND_WIN32_VIRTUALALLOC 2
  29. #define ARENA_BACKEND_WASM_HEAPBASE 3
  30. #ifndef ARENA_BACKEND
  31. #define ARENA_BACKEND ARENA_BACKEND_LIBC_MALLOC
  32. #endif // ARENA_BACKEND
  33. typedef struct Region Region;
  34. struct Region {
  35. Region *next;
  36. size_t count;
  37. size_t capacity;
  38. uintptr_t data[];
  39. };
  40. typedef struct {
  41. Region *begin, *end;
  42. } Arena;
  43. #define REGION_DEFAULT_CAPACITY (8*1024)
  44. Region *new_region(size_t capacity);
  45. void free_region(Region *r);
  46. // TODO: snapshot/rewind capability for the arena
  47. // - Snapshot should be combination of a->end and a->end->count.
  48. // - Rewinding should be restoring a->end and a->end->count from the snapshot and
  49. // setting count-s of all the Region-s after the remembered a->end to 0.
  50. void *arena_alloc(Arena *a, size_t size_bytes);
  51. void arena_reset(Arena *a);
  52. void arena_free(Arena *a);
  53. #endif // ARENA_H_
  54. #ifdef ARENA_IMPLEMENTATION
  55. #if ARENA_BACKEND == ARENA_BACKEND_LIBC_MALLOC
  56. #include <stdlib.h>
  57. // TODO: instead of accepting specific capacity new_region() should accept the size of the object we want to fit into the region
  58. // It should be up to new_region() to decide the actual capacity to allocate
  59. Region *new_region(size_t capacity)
  60. {
  61. size_t size_bytes = sizeof(Region) + sizeof(uintptr_t)*capacity;
  62. // TODO: it would be nice if we could guarantee that the regions are allocated by ARENA_BACKEND_LIBC_MALLOC are page aligned
  63. Region *r = malloc(size_bytes);
  64. ARENA_ASSERT(r);
  65. r->next = NULL;
  66. r->count = 0;
  67. r->capacity = capacity;
  68. return r;
  69. }
  70. void free_region(Region *r)
  71. {
  72. free(r);
  73. }
  74. #elif ARENA_BACKEND == ARENA_BACKEND_LINUX_MMAP
  75. # error "TODO: Linux mmap backend is not implemented yet"
  76. #elif ARENA_BACKEND == ARENA_BACKEND_WIN32_VIRTUALALLOC
  77. # error "TODO: Win32 VirtualAlloc backend is not implemented yet"
  78. #elif ARENA_BACKEND == ARENA_BACKEND_WASM_HEAPBASE
  79. # error "TODO: WASM __heap_base backend is not implemented yet"
  80. #else
  81. # error "Unknown Arena backend"
  82. #endif
  83. // TODO: add debug statistic collection mode for arena
  84. // Should collect things like:
  85. // - How many times new_region was called
  86. // - How many times existing region was skipped
  87. // - How many times allocation exceeded REGION_DEFAULT_CAPACITY
  88. void *arena_alloc(Arena *a, size_t size_bytes)
  89. {
  90. size_t size = (size_bytes + sizeof(uintptr_t) - 1)/sizeof(uintptr_t);
  91. if (a->end == NULL) {
  92. ARENA_ASSERT(a->begin == NULL);
  93. size_t capacity = REGION_DEFAULT_CAPACITY;
  94. if (capacity < size) capacity = size;
  95. a->end = new_region(capacity);
  96. a->begin = a->end;
  97. }
  98. while (a->end->count + size > a->end->capacity && a->end->next != NULL) {
  99. a->end = a->end->next;
  100. }
  101. if (a->end->count + size > a->end->capacity) {
  102. ARENA_ASSERT(a->end->next == NULL);
  103. size_t capacity = REGION_DEFAULT_CAPACITY;
  104. if (capacity < size) capacity = size;
  105. a->end->next = new_region(capacity);
  106. a->end = a->end->next;
  107. }
  108. void *result = &a->end->data[a->end->count];
  109. a->end->count += size;
  110. return result;
  111. }
  112. void arena_reset(Arena *a)
  113. {
  114. for (Region *r = a->begin; r != NULL; r = r->next) {
  115. r->count = 0;
  116. }
  117. a->end = a->begin;
  118. }
  119. void arena_free(Arena *a)
  120. {
  121. Region *r = a->begin;
  122. while (r) {
  123. Region *r0 = r;
  124. r = r->next;
  125. free_region(r0);
  126. }
  127. a->begin = NULL;
  128. a->end = NULL;
  129. }
  130. #endif // ARENA_IMPLEMENTATION