No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

allocator_internal.h 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #ifndef ALLOCATOR_INTERNAL_H
  2. #define ALLOCATOR_INTERNAL_H
  3. #include <stdint.h>
  4. // No enum because these need to be 1 byte
  5. const char RT_FREESPACE = 0;
  6. const char RT_TREE_NODE = 1;
  7. const char RT_WATERMARK = 2;
  8. const char COLOR_RED = 0;
  9. const char COLOR_BLACK = 1;
  10. const uintptr_t MIN_NEW_MEM_SIZE = 4096;
  11. typedef struct Arena {
  12. struct FreeSpace *root_freespace;
  13. struct TreeAlloc *root_treealloc;
  14. void *(*get_new_region)(uintptr_t);
  15. void (*error)(char*);
  16. } Arena;
  17. // All three of these types should be memory-compatible with each other
  18. // Before I forget to write it down: It is assumed that the beginning of every allocation region (or
  19. // free space region) is aligned to the alignment of all three of these types (or WatermarkAlloc since
  20. // it has at least all the fields of the other one). This allows for the allocation record to appear
  21. // at the beginning of the region and still be aligned. As a consequence, all sizes are a multiple of
  22. // the alignment.
  23. typedef struct TreeAlloc {
  24. char type; // Should be RT_TREE_NODE
  25. char color;
  26. struct TreeAlloc *parent;
  27. struct TreeAlloc *left;
  28. struct TreeAlloc *right;
  29. uintptr_t size;
  30. struct TreeAlloc *before;
  31. struct TreeAlloc *after;
  32. } TreeAlloc;
  33. typedef struct FreeSpace {
  34. char type; // Should be RT_FREESPACE
  35. char color;
  36. struct FreeSpace *parent;
  37. struct FreeSpace *left;
  38. struct FreeSpace *right;
  39. uintptr_t size;
  40. } FreeSpace;
  41. typedef struct WatermarkAlloc {
  42. char type; // Should be RT_WATERMARK
  43. char color;
  44. struct TreeAlloc *parent;
  45. struct TreeAlloc *left;
  46. struct TreeAlloc *right;
  47. uintptr_t size;
  48. struct TreeAlloc *before;
  49. struct TreeAlloc *after;
  50. int num_allocs;
  51. void *next_alloc;
  52. } WatermarkAlloc;
  53. void* align_after(void* address, uintptr_t align);
  54. uintptr_t lcm(uintptr_t a, uintptr_t b);
  55. uintptr_t gcd(uintptr_t a, uintptr_t b);
  56. TreeAlloc *search_by_address(TreeAlloc *root, void *address);
  57. TreeAlloc *search_by_size(TreeAlloc *root, uintptr_t padding, uintptr_t align, uintptr_t size);
  58. TreeAlloc *get_sibling(TreeAlloc *ta);
  59. void rotate_left(TreeAlloc **root_ptr, TreeAlloc *ta);
  60. void rotate_right(TreeAlloc **root_ptr, TreeAlloc *ta);
  61. void repair_tree_after_insert(TreeAlloc **root_ptr, TreeAlloc *ta);
  62. void remove_node(TreeAlloc** root_ptr, TreeAlloc* node);
  63. void insert_singleton(TreeAlloc **root_ptr, TreeAlloc *to_insert);
  64. void insert_right(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* after);
  65. void insert_left(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* before);
  66. void unalloc(Arena *arena, void *addr);
  67. void *alloc(Arena *arena, uintptr_t size, uintptr_t align);
  68. #endif