1 | #include "collector.h" |
---|
2 | |
---|
3 | #include <stdint.h> |
---|
4 | #include <stdlib.h> |
---|
5 | #include <string.h> |
---|
6 | |
---|
7 | #include "fstream.h" |
---|
8 | |
---|
9 | #include "gc_tools.h" |
---|
10 | #include "state.h" |
---|
11 | // #include "memory_pool.h" |
---|
12 | |
---|
13 | void* gc_finish_alloc_block(void* block, size_t actual_size, size_t target_size); |
---|
14 | void gc_assign_reference(void** ref, gc_object_header* ptr); |
---|
15 | |
---|
16 | gcpointer_t** gc_find_previous_ref(gcpointer_t* target) |
---|
17 | { |
---|
18 | if(!(target)) return NULL; |
---|
19 | |
---|
20 | bool managed = gc_is_managed(target); |
---|
21 | gc_object_header* obj = gc_get_object_ptr((void*)target->ptr); |
---|
22 | |
---|
23 | check(gc_is_valide(obj)); |
---|
24 | |
---|
25 | gcpointer_t** prev_next_ptr = managed ? &obj->type_chain : &obj->root_chain; |
---|
26 | while((*prev_next_ptr) && (*prev_next_ptr) != target) |
---|
27 | { |
---|
28 | prev_next_ptr = &(*prev_next_ptr)->next; |
---|
29 | } |
---|
30 | |
---|
31 | return prev_next_ptr; |
---|
32 | } |
---|
33 | |
---|
34 | void* gc_allocate(size_t target_size) |
---|
35 | { |
---|
36 | size_t size = gc_compute_size(target_size + sizeof(gc_object_header)); |
---|
37 | |
---|
38 | check(size < POOL_SIZE_BYTES); |
---|
39 | |
---|
40 | void* block = NULL; |
---|
41 | gc_state* gc = gc_get_state(); |
---|
42 | |
---|
43 | if((intptr_t)(block = gc_try_allocate(gc, size))) return gc_finish_alloc_block(block, size, target_size); |
---|
44 | |
---|
45 | gc_collect(gc); |
---|
46 | |
---|
47 | if((intptr_t)(block = gc_try_allocate(gc, size))) return gc_finish_alloc_block(block, size, target_size); |
---|
48 | |
---|
49 | gc_allocate_pool(gc); |
---|
50 | |
---|
51 | if((intptr_t)(block = gc_try_allocate(gc, size))) return gc_finish_alloc_block(block, size, target_size); |
---|
52 | |
---|
53 | ofstream_stderr() | "ERROR: allocation in new pool failed" | endl; |
---|
54 | abort(); |
---|
55 | |
---|
56 | return NULL; |
---|
57 | } |
---|
58 | |
---|
59 | void* gc_finish_alloc_block(void* block, size_t actual_size, size_t target_size) |
---|
60 | { |
---|
61 | void* data = (void*)(((intptr_t)block) + sizeof(gc_object_header)); |
---|
62 | void* header = block; |
---|
63 | |
---|
64 | check(((intptr_t)data) > ((intptr_t)block)); |
---|
65 | check(((intptr_t)data) >= ((intptr_t)header)); |
---|
66 | check(is_aligned(data)); |
---|
67 | check(((intptr_t)data) + target_size <= ((intptr_t)block) + actual_size); |
---|
68 | |
---|
69 | gc_object_header* obj = gc_object_header_placement_ctor(header, actual_size); |
---|
70 | |
---|
71 | (void)obj; //remove unsused warning since this is for debug |
---|
72 | check(obj == get_object_ptr(data)); |
---|
73 | |
---|
74 | gc_register_allocation(gc_get_state(), actual_size); |
---|
75 | |
---|
76 | return data; |
---|
77 | } |
---|
78 | |
---|
79 | // void process_reference(void** ref, std::vector<void**>& worklist) |
---|
80 | // { |
---|
81 | // check(!gc::gc_get_state().is_in_heap(ref)); |
---|
82 | // |
---|
83 | // if(gc_object_header* ptr = get_object_ptr(*ref)) |
---|
84 | // { |
---|
85 | // if(!ptr->is_forwarded) |
---|
86 | // { |
---|
87 | // copy_object(ptr); |
---|
88 | // |
---|
89 | // scan_object(ptr->forward, worklist); |
---|
90 | // |
---|
91 | // assign_reference(ref, ptr->forward); |
---|
92 | // } |
---|
93 | // else |
---|
94 | // { |
---|
95 | // //duplication to help debug |
---|
96 | // assign_reference(ref, ptr->forward); |
---|
97 | // } |
---|
98 | // } |
---|
99 | // } |
---|
100 | // |
---|
101 | // void assign_reference(void** ref, gc_object_header* ptr) |
---|
102 | // { |
---|
103 | // void* address = (void*)(((intptr_t)ptr) + sizeof(gc_object_header)); |
---|
104 | // |
---|
105 | // write_aligned_ptr(ref, address); |
---|
106 | // } |
---|
107 | // |
---|
108 | // gc_object_header* copy_object(gc_object_header* ptr) |
---|
109 | // { |
---|
110 | // check(!ptr->forward); |
---|
111 | // check(!ptr->is_forwarded); |
---|
112 | // check(pool_of(ptr)->is_from_space()); |
---|
113 | // |
---|
114 | // memory_pool* pool = pool_of(ptr)->mirror(); |
---|
115 | // |
---|
116 | // void* new_block = pool->allocate(ptr->size, false); |
---|
117 | // |
---|
118 | // std::memcpy(new_block, ptr, ptr->size); |
---|
119 | // |
---|
120 | // gc_object_header* fwd_ptr = new (new_block) gc_object_header(ptr); |
---|
121 | // |
---|
122 | // ptr->forward = fwd_ptr; |
---|
123 | // ptr->is_forwarded = true; |
---|
124 | // |
---|
125 | // return fwd_ptr; |
---|
126 | // } |
---|
127 | // |
---|
128 | // void scan_object(gc_object_header* object, std::vector<void**>& worklist) |
---|
129 | // { |
---|
130 | // gcpointer_base* type = object->type_chain; |
---|
131 | // while(type) |
---|
132 | // { |
---|
133 | // check(((intptr_t)type) > ((intptr_t)object)); |
---|
134 | // check(((intptr_t)type) < ((intptr_t)((intptr_t)object) + object->size)); |
---|
135 | // |
---|
136 | // check(gc::gc_get_state().is_in_to_space(&type->m_ptr)); |
---|
137 | // |
---|
138 | // worklist.push_back(&type->m_ptr); |
---|
139 | // |
---|
140 | // type = type->m_next; |
---|
141 | // } |
---|
142 | // } |
---|
143 | // }; |
---|