A Hugo incarnation of the blog.
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.

runtime.h 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #pragma once
  2. #include <stdlib.h>
  3. struct gmachine;
  4. enum node_tag {
  5. NODE_APP,
  6. NODE_NUM,
  7. NODE_GLOBAL,
  8. NODE_IND,
  9. NODE_DATA
  10. };
  11. struct node_base {
  12. enum node_tag tag;
  13. int8_t gc_reachable;
  14. struct node_base* gc_next;
  15. };
  16. struct node_app {
  17. struct node_base base;
  18. struct node_base* left;
  19. struct node_base* right;
  20. };
  21. struct node_num {
  22. struct node_base base;
  23. int32_t value;
  24. };
  25. struct node_global {
  26. struct node_base base;
  27. int32_t arity;
  28. void (*function)(struct gmachine*);
  29. };
  30. struct node_ind {
  31. struct node_base base;
  32. struct node_base* next;
  33. };
  34. struct node_data {
  35. struct node_base base;
  36. int8_t tag;
  37. struct node_base** array;
  38. };
  39. struct node_base* alloc_node();
  40. struct node_app* alloc_app(struct node_base* l, struct node_base* r);
  41. struct node_num* alloc_num(int32_t n);
  42. struct node_global* alloc_global(void (*f)(struct gmachine*), int32_t a);
  43. struct node_ind* alloc_ind(struct node_base* n);
  44. void free_node_direct(struct node_base*);
  45. void gc_visit_node(struct node_base*);
  46. struct stack {
  47. size_t size;
  48. size_t count;
  49. struct node_base** data;
  50. };
  51. void stack_init(struct stack* s);
  52. void stack_free(struct stack* s);
  53. void stack_push(struct stack* s, struct node_base* n);
  54. struct node_base* stack_pop(struct stack* s);
  55. struct node_base* stack_peek(struct stack* s, size_t o);
  56. void stack_popn(struct stack* s, size_t n);
  57. struct gmachine {
  58. struct stack stack;
  59. struct node_base* gc_nodes;
  60. int64_t gc_node_count;
  61. int64_t gc_node_threshold;
  62. };
  63. void gmachine_init(struct gmachine* g);
  64. void gmachine_free(struct gmachine* g);
  65. void gmachine_slide(struct gmachine* g, size_t n);
  66. void gmachine_update(struct gmachine* g, size_t o);
  67. void gmachine_alloc(struct gmachine* g, size_t o);
  68. void gmachine_pack(struct gmachine* g, size_t n, int8_t t);
  69. void gmachine_split(struct gmachine* g, size_t n);
  70. struct node_base* gmachine_track(struct gmachine* g, struct node_base* b);
  71. void gmachine_gc(struct gmachine* g);