source: examples/gc_no_raii/src/internal/collector.c @ 746ae82

ADTarm-ehast-experimentalcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 746ae82 was 200fcb3, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

add auto newline to sout, change endl to nl

  • Property mode set to 100644
File size: 3.5 KB
RevLine 
[6be0cf9]1#include "collector.h"
2
[16cfd8c]3#ifdef __cforall
4extern "C" {
5#endif
[6be0cf9]6#include <string.h>
[16cfd8c]7#ifdef __cforall
8}
9#endif
[6be0cf9]10
[73abe95]11#include <fstream.hfa>
[6643e72]12
[385c130]13#include "state.h"
14#include "gcpointers.h"
15#include "memory_pool.h"
16
17void* gc_finish_alloc_block(void* block, size_t actual_size, size_t target_size);
18void gc_assign_reference(void** ref, gc_object_header* ptr);
19
20gcpointer_t** gc_find_previous_ref(gcpointer_t* target)
21{
22        if(!(target)) return NULL;
23
24        bool managed = gc_is_managed(target);
25        gc_object_header* obj = gc_get_object_ptr((void*)target->ptr);
26
[46f1d20]27        check(is_valid(obj));
[385c130]28
29        gcpointer_t** prev_next_ptr = managed ? &obj->type_chain : &obj->root_chain;
30        while((*prev_next_ptr) && (*prev_next_ptr) != target)
31        {
32                prev_next_ptr = &(*prev_next_ptr)->next;
33        }
34
35        return prev_next_ptr;
36}
37
38void* gc_allocate(size_t target_size)
39{
[200fcb3]40        // sout | "Allocating " | target_size | " bytes";
[6643e72]41
[385c130]42        size_t size = gc_compute_size(target_size + sizeof(gc_object_header));
43
[200fcb3]44        // sout | "Object header size: " | sizeof(gc_object_header) | " bytes";
45        // sout | "Actual allocation size: " | size | " bytes";
[6643e72]46
[385c130]47        check(size < POOL_SIZE_BYTES);
48
49        void* block = NULL;
50        gc_state* gc = gc_get_state();
51
52        if((intptr_t)(block = gc_try_allocate(gc, size))) return gc_finish_alloc_block(block, size, target_size);
53
54        gc_collect(gc);
55
56        if((intptr_t)(block = gc_try_allocate(gc, size))) return gc_finish_alloc_block(block, size, target_size);
57
58        gc_allocate_pool(gc);
59
60        if((intptr_t)(block = gc_try_allocate(gc, size))) return gc_finish_alloc_block(block, size, target_size);
61
[46f1d20]62        checkf( (int) 0, "ERROR: allocation in new pool failed");
[385c130]63
64        return NULL;
65}
66
67void* gc_finish_alloc_block(void* block, size_t actual_size, size_t target_size)
68{
[46f1d20]69        intptr_t data = ((intptr_t)block) + sizeof(gc_object_header);
[385c130]70        void* header = block;
71
[46f1d20]72        check( data > ((intptr_t)block));
73        check( data >= ((intptr_t)header));
74        check( gc_is_aligned( (void*)data ) );
75        check( data + target_size <= ((intptr_t)block) + actual_size );
[385c130]76
[df4aea7]77        gc_object_header* obj = placement_ctor(header, actual_size);
[385c130]78
79        (void)obj; //remove unsused warning since this is for debug
[46f1d20]80        check(obj == gc_get_object_ptr( (void*)data ));
[385c130]81
82        gc_register_allocation(gc_get_state(), actual_size);
83
[46f1d20]84        return (void*)data;
[385c130]85}
86
87void gc_process_reference(void** ref, worklist_t* worklist)
88{
89        check(!gc_is_in_heap(gc_get_state(), ref));
90
91        gc_object_header* ptr = gc_get_object_ptr(*ref);
92        if(ptr)
93        {
94                if(!ptr->is_forwarded)
95                {
96                        gc_copy_object(ptr);
97
98                        gc_scan_object(ptr->forward, worklist);
99
100                        gc_assign_reference(ref, ptr->forward);
101                }
102                else
103                {
104                        //duplication to help debug
105                        gc_assign_reference(ref, ptr->forward);
106                }
107        }
108}
109
110void gc_assign_reference(void** ref, gc_object_header* ptr)
111{
112        void* address = (void*)(((intptr_t)ptr) + sizeof(gc_object_header));
113
114        gc_write_aligned_ptr(ref, address);
115}
116
117gc_object_header* gc_copy_object(gc_object_header* ptr)
118{
119        check(!ptr->forward);
120        check(!ptr->is_forwarded);
[46f1d20]121        check(gc_pool_is_from_space(gc_pool_of(ptr)));
[385c130]122
123        gc_memory_pool* pool = gc_pool_of(ptr)->mirror;
124
125        void* new_block = gc_pool_allocate(pool, ptr->size, true);
126
127        memcpy(new_block, ptr, ptr->size);
128
[df4aea7]129        gc_object_header* fwd_ptr = placement_copy_ctor(new_block, ptr);
[385c130]130
131        ptr->forward = fwd_ptr;
132        ptr->is_forwarded = true;
133
134        return fwd_ptr;
135}
136
137void gc_scan_object(gc_object_header* object, worklist_t* worklist)
138{
139        gcpointer_t* field = object->type_chain;
140        while(field)
141        {
142                check(((intptr_t)field) > ((intptr_t)object));
143                check(((intptr_t)field) < ((intptr_t)((intptr_t)object) + object->size));
144
[46f1d20]145                check(gc_is_in_to_space(gc_get_state(), &field->ptr));
[385c130]146
147                intptr_t* ref = &field->ptr;
[16cfd8c]148                push_back(worklist, ref);
[385c130]149
150                field = field->next;
151        }
152}
Note: See TracBrowser for help on using the repository browser.