heap.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #ifndef HEAP_H_
  2. #define HEAP_H_
  3. #include <assert.h>
  4. #include <stdint.h>
  5. #include <stdlib.h>
  6. #define UNIMPLEMENTED \
  7. do { \
  8. fprintf(stderr, "%s:%d: %s is not implemented yet\n", \
  9. __FILE__, __LINE__, __func__); \
  10. abort(); \
  11. } while(0)
  12. #define HEAP_CAP_BYTES 640000
  13. static_assert(HEAP_CAP_BYTES % sizeof(uintptr_t) == 0,
  14. "The heap capacity is not divisible by "
  15. "the size of the pointer. Of the platform.");
  16. #define HEAP_CAP_WORDS (HEAP_CAP_BYTES / sizeof(uintptr_t))
  17. extern uintptr_t heap[HEAP_CAP_WORDS];
  18. extern const uintptr_t *stack_base;
  19. void *heap_alloc(size_t size_bytes);
  20. void heap_free(void *ptr);
  21. void heap_collect();
  22. #define CHUNK_LIST_CAP 1024
  23. typedef struct {
  24. uintptr_t *start;
  25. size_t size;
  26. } Chunk;
  27. typedef struct {
  28. size_t count;
  29. Chunk chunks[CHUNK_LIST_CAP];
  30. } Chunk_List;
  31. extern Chunk_List alloced_chunks;
  32. extern Chunk_List freed_chunks;
  33. extern Chunk_List tmp_chunks;
  34. void chunk_list_insert(Chunk_List *list, void *start, size_t size);
  35. void chunk_list_merge(Chunk_List *dst, const Chunk_List *src);
  36. void chunk_list_dump(const Chunk_List *list, const char *name);
  37. int chunk_list_find(const Chunk_List *list, uintptr_t *ptr);
  38. void chunk_list_remove(Chunk_List *list, size_t index);
  39. #endif // HEAP_H_