source: src/examples/gc_no_raii/src/internal/collector.c @ 6be0cf9

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 6be0cf9 was 6be0cf9, checked in by Thierry Delisle <tdelisle@…>, 9 years ago

gcpointers.c compiles

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