alloc.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #ifndef MINILISP_ALLOC_H
  2. #define MINILISP_ALLOC_H
  3. #include "minilisp.h"
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include "strmap.h"
  7. #define env_t StrMap
  8. #if defined(CPU_ARM) || defined(CPU_X86) || defined(__AMIGA)
  9. #define STACK_FRAME_MARKER 0xf0000001
  10. #endif
  11. #ifdef CPU_X64
  12. // functions store a pointer to their own definition ORed with this marker on the stack
  13. #define STACK_FRAME_MARKER 0xf000000000000001
  14. #endif
  15. enum cell_allocator_t {
  16. CA_STACK,
  17. CA_HEAP
  18. };
  19. typedef struct MemStats {
  20. unsigned long byte_heap_used;
  21. unsigned long byte_heap_max;
  22. unsigned long cells_used;
  23. unsigned long cells_max;
  24. } MemStats;
  25. void init_allocator();
  26. Cell* get_cell_heap();
  27. void* cell_malloc(int num_bytes);
  28. void* cell_realloc(void* old_addr, unsigned int old_size, unsigned int num_bytes);
  29. Cell* collect_garbage(env_t* global_env, void* stack_end, void* stack_pointer);
  30. Cell* list_symbols(env_t* global_env);
  31. Cell* alloc_cons(Cell* ar, Cell* dr);
  32. Cell* alloc_list(Cell** items, int num);
  33. Cell* alloc_sym(char* str);
  34. Cell* alloc_bytes();
  35. Cell* alloc_num_bytes(unsigned int num_bytes);
  36. Cell* alloc_string();
  37. Cell* alloc_num_string(unsigned int num_bytes);
  38. Cell* alloc_string_copy(char* str);
  39. Cell* alloc_string_from_bytes(Cell* bytes);
  40. Cell* alloc_concat(Cell* str1, Cell* str2);
  41. Cell* alloc_substr(Cell* str, unsigned int from, unsigned int len);
  42. Cell* alloc_int(int i);
  43. Cell* alloc_nil();
  44. Cell* alloc_error(unsigned int code);
  45. Cell* alloc_lambda(Cell* args);
  46. Cell* alloc_builtin(unsigned int b, Cell* signature);
  47. Cell* alloc_clone(Cell* orig);
  48. Cell* alloc_struct_def(int size);
  49. Cell* alloc_struct(Cell* struct_def);
  50. Cell* alloc_vector(int size);
  51. MemStats* alloc_stats();
  52. void free_tree(Cell* root);
  53. #endif