Reduct  v1.0.4-3-gdaf0d70
A functional and immutable language.
Loading...
Searching...
No Matches
gc_impl.h
Go to the documentation of this file.
1#include "item.h"
2#ifndef REDUCT_GC_IMPL_H
3#define REDUCT_GC_IMPL_H 1
4
5#include "core.h"
6#include "eval.h"
7#include "gc.h"
8#include "item.h"
9#include "list.h"
10
11static void reduct_gc_mark(reduct_t* reduct, reduct_item_t* item);
12
14{
15 if (node == REDUCT_NULL)
16 {
17 return;
18 }
19
22 {
23 return;
24 }
26
27 if (shift == 0)
28 {
29 for (int i = 0; i < REDUCT_LIST_WIDTH; i++)
30 {
31 reduct_handle_t h = node->handles[i];
33 {
35 }
36 }
37 }
38 else
39 {
40 for (int i = 0; i < REDUCT_LIST_WIDTH; i++)
41 {
42 reduct_gc_mark_node(reduct, shift - REDUCT_LIST_BITS, node->children[i]);
43 }
44 }
45}
46
47static void reduct_gc_mark_list(reduct_t* reduct, reduct_list_t* list)
48{
49 reduct_gc_mark_node(reduct, list->shift, list->root);
50 reduct_gc_mark_node(reduct, 0, list->tail);
51}
52
53static void reduct_gc_mark(reduct_t* reduct, reduct_item_t* item)
54{
55 REDUCT_ASSERT(reduct != REDUCT_NULL);
56
57 if (item == REDUCT_NULL || (item->flags & REDUCT_ITEM_FLAG_GC_MARK))
58 {
59 return;
60 }
61
63
64 if (item->type == REDUCT_ITEM_TYPE_LIST)
65 {
66 reduct_gc_mark_list(reduct, &item->list);
67 }
68 else if (item->type == REDUCT_ITEM_TYPE_FUNCTION)
69 {
70 for (reduct_uint16_t i = 0; i < item->function.constantCount; i++)
71 {
73 {
74 reduct_gc_mark(reduct, item->function.constants[i].item);
75 }
77 {
79 }
80 }
81 }
82 else if (item->type == REDUCT_ITEM_TYPE_CLOSURE)
83 {
85 for (reduct_uint16_t i = 0; i < item->closure.function->constantCount; i++)
86 {
87 reduct_handle_t handle = item->closure.constants[i];
88 if (REDUCT_HANDLE_IS_ITEM(&handle))
89 {
90 reduct_gc_mark(reduct, REDUCT_HANDLE_TO_ITEM(&handle));
91 }
92 }
93 }
94}
95
97{
98 REDUCT_ASSERT(reduct != REDUCT_NULL);
99
100 if (reduct->blocksAllocated >= reduct->gcThreshold)
101 {
102 reduct_gc(reduct);
103 reduct->blocksAllocated = 0;
105 }
106}
107
109{
110 REDUCT_ASSERT(reduct != REDUCT_NULL);
111
112 reduct_item_block_t* rootBlock = reduct->block;
113 while (rootBlock != REDUCT_NULL)
114 {
115 for (int i = 0; i < REDUCT_ITEM_BLOCK_MAX; i++)
116 {
117 reduct_item_t* item = &rootBlock->items[i];
118 if (item->type != REDUCT_ITEM_TYPE_NONE && item->retainCount > 0)
119 {
120 reduct_gc_mark(reduct, item);
121 }
122 }
123 rootBlock = rootBlock->next;
124 }
125
126 if (reduct->evalState != REDUCT_NULL)
127 {
128 for (reduct_uint32_t i = 0; i < reduct->evalState->regCount; i++)
129 {
130 reduct_handle_t child = reduct->evalState->regs[i];
131 if (REDUCT_HANDLE_IS_ITEM(&child))
132 {
133 reduct_gc_mark(reduct, REDUCT_HANDLE_TO_ITEM(&child));
134 }
135 }
136 for (reduct_uint32_t i = 0; i < reduct->evalState->frameCount; i++)
137 {
138 reduct_closure_t* closure = reduct->evalState->frames[i].closure;
139 reduct_gc_mark(reduct, REDUCT_CONTAINER_OF(closure, reduct_item_t, closure));
140 }
141 }
142
143 reduct->freeList = REDUCT_NULL;
144
145 reduct_item_block_t* block = reduct->block;
146 while (block != REDUCT_NULL)
147 {
148 for (int i = 0; i < REDUCT_ITEM_BLOCK_MAX; i++)
149 {
150 reduct_item_t* item = &block->items[i];
152 {
153 item->flags &= ~REDUCT_ITEM_FLAG_GC_MARK;
154 }
155 else
156 {
157 reduct_item_free(reduct, item);
158 }
159 }
160 block = block->next;
161 }
162}
163
164#endif
Core definitions and structures.
#define REDUCT_CONTAINER_OF(_ptr, _type, _member)
Container of macro.
Definition defs.h:184
reduct_uint64_t reduct_handle_t
Handle type.
Definition defs.h:189
uint32_t reduct_uint32_t
Definition defs.h:95
#define REDUCT_ASSERT(_cond)
Definition defs.h:25
#define REDUCT_API
Definition defs.h:7
uint16_t reduct_uint16_t
Definition defs.h:97
#define REDUCT_NULL
Definition defs.h:23
Virtual machine evaluation.
Garbage collection.
static void reduct_gc_mark(reduct_t *reduct, reduct_item_t *item)
Definition gc_impl.h:53
static void reduct_gc_mark_node(reduct_t *reduct, reduct_uint32_t shift, reduct_list_node_t *node)
Definition gc_impl.h:13
static void reduct_gc_mark_list(reduct_t *reduct, reduct_list_t *list)
Definition gc_impl.h:47
#define REDUCT_GC_THRESHOLD_INITIAL
Initial blocks allocated threshold for garbage collection.
Definition core.h:22
#define REDUCT_CONST_SLOT_CAPTURE(_capture)
Create a constant slot containing a variable name to be captured.
Definition function.h:64
#define REDUCT_CONST_SLOT_ITEM(_item)
Create a constant slot containing an item.
Definition function.h:57
REDUCT_API void reduct_gc_if_needed(reduct_t *reduct)
Optionally run the garbage collector if the number of allocated blocks exceeds the threshold.
Definition gc_impl.h:96
REDUCT_API void reduct_gc(reduct_t *reduct)
Run the garbage collector.
Definition gc_impl.h:108
#define REDUCT_HANDLE_IS_ITEM(_handle)
Check if a handle is an item.
Definition handle.h:171
#define REDUCT_HANDLE_TO_ITEM(_handle)
Get the item pointer of a handle.
Definition handle.h:257
#define REDUCT_ITEM_TYPE_LIST
A list.
Definition item.h:28
#define REDUCT_ITEM_TYPE_FUNCTION
A function.
Definition item.h:29
REDUCT_API void reduct_item_free(struct reduct *reduct, reduct_item_t *item)
Free an Reduct item.
#define REDUCT_ITEM_TYPE_NONE
No type.
Definition item.h:26
#define REDUCT_ITEM_TYPE_CLOSURE
A closure.
Definition item.h:30
#define REDUCT_ITEM_BLOCK_MAX
The maximum number of items in a block.
Definition item.h:80
#define REDUCT_ITEM_FLAG_GC_MARK
Item is marked by GC.
Definition item.h:44
Item management.
List management.
#define REDUCT_LIST_BITS
Number of bits per level in the trie.
Definition list.h:27
#define REDUCT_LIST_WIDTH
The number of children per node.
Definition list.h:28
Closure structure.
Definition closure.h:26
reduct_handle_t * constants
The array of constant slots forming the constant template.
Definition closure.h:28
reduct_function_t * function
Pointer to the prototype function item.
Definition closure.h:27
reduct_const_slot_type_t type
The type of the constant slot.
Definition function.h:44
struct reduct_item * item
The item contained in the constant slot.
Definition function.h:47
struct reduct_atom * capture
The name of the variable to be captured.
Definition function.h:48
reduct_const_slot_t * constants
The array of constant slots forming the constant template.
Definition function.h:83
reduct_uint16_t constantCount
Number of constants.
Definition function.h:84
Item block structure.
Definition item.h:89
struct reduct_item_block * next
Definition item.h:90
reduct_item_t items[REDUCT_ITEM_BLOCK_MAX]
Definition item.h:92
Item structure.
Definition item.h:57
reduct_list_t list
A list.
Definition item.h:67
reduct_closure_t closure
A closure.
Definition item.h:70
reduct_function_t function
A function.
Definition item.h:69
reduct_item_flags_t flags
Flags for the item.
Definition item.h:60
reduct_uint16_t retainCount
The reference count for GC retention.
Definition item.h:62
reduct_item_type_t type
The type of the item.
Definition item.h:61
List node structure.
Definition list.h:36
reduct_handle_t handles[REDUCT_LIST_WIDTH]
Definition list.h:39
struct reduct_list_node * children[REDUCT_LIST_WIDTH]
Definition list.h:38
List structure.
Definition list.h:48
reduct_list_node_t * tail
Pointer to the tail node.
Definition list.h:52
reduct_list_node_t * root
Pointer to the trie root node.
Definition list.h:51
reduct_uint32_t shift
The amount to shift the index to compute access paths.
Definition list.h:50
State structure.
Definition core.h:61
reduct_size_t gcThreshold
Definition core.h:63
reduct_item_block_t * block
Definition core.h:64
struct reduct_eval_state * evalState
Definition core.h:79
struct reduct_item * freeList
Definition core.h:65
reduct_size_t blocksAllocated
Definition core.h:62