Changeset 385c130 for src/examples/gc_no_raii
- Timestamp:
- Apr 19, 2016, 1:19:25 PM (9 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- 9026b4b
- Parents:
- 356bb95
- Location:
- src/examples/gc_no_raii
- Files:
-
- 3 added
- 1 deleted
- 12 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified src/examples/gc_no_raii/bug-repro/void_pointer.c ¶
r356bb95 r385c130 2 2 #include <stdint.h> 3 3 4 //inline intptr_t test(void*address)5 //{6 // return (intptr_t)address;7 //}4 inline void* test(intptr_t address) 5 { 6 return (void*)address; 7 } 8 8 9 9 //inline void* test2(void* address) … … 12 12 //} 13 13 14 inline int test()15 {16 void* d = 0;17 return (int)d;18 }14 // inline int test() 15 // { 16 // void* d = 0; 17 // return (int)d; 18 // } -
TabularUnified src/examples/gc_no_raii/regen_makefile.sh ¶
r356bb95 r385c130 3 3 premake4 clean 4 4 premake4 gmake 5 sed '/# GNU Make project makefile autogenerated by Premake/a CC = ..\/cfa\nCXX = ..\/cfa\n' build/Makefile > out5 sed '/# GNU Make project makefile autogenerated by Premake/a CC = cfa\nCXX = cfa\n' build/Makefile > out 6 6 mv out build/Makefile -
TabularUnified src/examples/gc_no_raii/src/gcpointers.c ¶
r356bb95 r385c130 6 6 #include "internal/state.h" 7 7 8 // void register_ptr(gcpointer_t* this) 9 // { 10 // if(gcpointer_null(this)) return; 11 // 12 // _Bool managed = gc_is_managed(this); 13 // 14 // 15 // gc_state* state = gc_get_state(); 16 // gc_object_header* obj = 0; 17 // if(managed) 18 // { 19 // obj = gc_get_object_for_ref(state, (void*)this); 20 // check(obj); 21 // check(gc_obj_is_valide(obj)); 22 // check(gc_is_managed(this) == gc_is_managed(obj->type_chain) || obj->type_chain == NULL); 23 // this->next = obj->type_chain; 24 // obj->type_chain = this; 25 // check(obj->is_valide()); 26 // } 27 // else 28 // { 29 // obj = gc_get_object_ptr(state, (void*)this->ptr); 30 // check(obj); 31 // check(gc_obj_is_valide(obj)); 32 // check(gc_is_managed(this) == gc_is_managed(obj->root_chain) || obj->root_chain == NULL); 33 // this->next = obj->root_chain; 34 // obj->root_chain = this; 35 // check(gc_obj_is_valide(obj)); 36 // } 37 // } 38 // 39 // void unregister_ptr(gcpointer_t* this) 40 // { 41 // if(gcpointer_null(this)) return; 42 // 43 // gcpointer_t** prev_next_ptr = gc_find_previous_ref(this); 44 // check((*prev_next_ptr) == this); 45 // 46 // (*prev_next_ptr) = this->next; 47 // } 48 // 49 // void gcpointer_ctor(gcpointer_t* this) 50 // { 51 // this->ptr = (intptr_t)NULL; 52 // this->next = NULL; 53 // } 54 // 55 // void gcpointer_ctor(gcpointer_t* this, void* address) 56 // { 57 // this->ptr = (intptr_t)address; 58 // this->next = NULL; 59 // 60 // register_ptr(this); 61 // } 62 // 63 // void gcpointer_ctor(gcpointer_t* this, gcpointer_t* other) 64 // { 65 // this->ptr = other->ptr; 66 // this->next = NULL; 67 // 68 // register_ptr(this); 69 // } 70 // 71 // void gcpointer_dtor(gcpointer_t* this) 72 // { 73 // unregister_ptr(this); 74 // } 75 // 76 // gcpointer_t* gcpointer_assign(gcpointer_t* this, gcpointer_t* rhs) 77 // { 78 // if(this != rhs) 79 // { 80 // unregister_ptr(this); 81 // 82 // this->ptr = rhs->ptr; 83 // 84 // register_ptr(this); 85 // } 86 // 87 // return this; 88 // } 89 // 90 // //Logical operators 91 // int gcpointer_equal(gcpointer_t* this, gcpointer_t* rhs) 92 // { 93 // return this->ptr == rhs->ptr; 94 // } 95 // 96 // int gcpointer_not_equal(gcpointer_t* this, gcpointer_t* rhs) 97 // { 98 // return this->ptr != rhs->ptr; 99 // } 100 // 101 // int gcpointer_null(gcpointer_t* this) 102 // { 103 // return this->ptr == (intptr_t)NULL; 104 // } 8 void register_ptr(gcpointer_t* this) 9 { 10 if(gcpointer_null(this)) return; 11 12 if(gc_is_managed(this)) 13 { 14 gc_object_header* obj = gc_get_object_for_ref(gc_get_state(), (void*)this); 15 check(obj); 16 check(gc_obj_is_valide(obj)); 17 check(gc_is_managed(this) == gc_is_managed(obj->type_chain) || obj->type_chain == NULL); 18 this->next = obj->type_chain; 19 obj->type_chain = this; 20 check(obj->is_valide()); 21 } 22 else 23 { 24 gc_object_header* obj = gc_get_object_ptr((void*)this->ptr); 25 check(obj); 26 check(gc_obj_is_valide(obj)); 27 check(gc_is_managed(this) == gc_is_managed(obj->root_chain) || obj->root_chain == NULL); 28 this->next = obj->root_chain; 29 obj->root_chain = this; 30 check(gc_obj_is_valide(obj)); 31 } 32 } 33 34 void unregister_ptr(gcpointer_t* this) 35 { 36 if(gcpointer_null(this)) return; 37 38 gcpointer_t** prev_next_ptr = gc_find_previous_ref(this); 39 check((*prev_next_ptr) == this); 40 41 (*prev_next_ptr) = this->next; 42 } 43 44 void gcpointer_ctor(gcpointer_t* this) 45 { 46 this->ptr = (intptr_t)NULL; 47 this->next = NULL; 48 } 49 50 void gcpointer_ctor(gcpointer_t* this, void* address) 51 { 52 this->ptr = (intptr_t)address; 53 this->next = NULL; 54 55 register_ptr(this); 56 } 57 58 void gcpointer_ctor(gcpointer_t* this, gcpointer_t* other) 59 { 60 this->ptr = other->ptr; 61 this->next = NULL; 62 63 register_ptr(this); 64 } 65 66 void gcpointer_dtor(gcpointer_t* this) 67 { 68 unregister_ptr(this); 69 } 70 71 gcpointer_t* gcpointer_assign(gcpointer_t* this, gcpointer_t* rhs) 72 { 73 if(this != rhs) 74 { 75 unregister_ptr(this); 76 77 this->ptr = rhs->ptr; 78 79 register_ptr(this); 80 } 81 82 return this; 83 } 84 85 //Logical operators 86 int gcpointer_equal(gcpointer_t* this, gcpointer_t* rhs) 87 { 88 return this->ptr == rhs->ptr; 89 } 90 91 int gcpointer_not_equal(gcpointer_t* this, gcpointer_t* rhs) 92 { 93 return this->ptr != rhs->ptr; 94 } 95 96 int gcpointer_null(gcpointer_t* this) 97 { 98 return this->ptr == (intptr_t)NULL; 99 } -
TabularUnified src/examples/gc_no_raii/src/gcpointers.h ¶
r356bb95 r385c130 10 10 11 11 void gcpointer_ctor(gcpointer_t* this); 12 // void gcpointer_ctor(gcpointer_t* ptr, (int)0);12 void gcpointer_ctor(gcpointer_t* ptr, int null); 13 13 void gcpointer_ctor(gcpointer_t* this, void* address); 14 14 void gcpointer_ctor(gcpointer_t* this, gcpointer_t* other); -
TabularUnified src/examples/gc_no_raii/src/internal/collector.c ¶
r356bb95 r385c130 8 8 #include <fstream> 9 9 10 //#include "state.h"11 //#include "gcpointers.h"12 //#include "memory_pool.h"10 #include "state.h" 11 #include "gcpointers.h" 12 #include "memory_pool.h" 13 13 14 //void* gc_finish_alloc_block(void* block, size_t actual_size, size_t target_size);15 //void gc_assign_reference(void** ref, gc_object_header* ptr);14 void* gc_finish_alloc_block(void* block, size_t actual_size, size_t target_size); 15 void gc_assign_reference(void** ref, gc_object_header* ptr); 16 16 17 // gcpointer_t** gc_find_previous_ref(gcpointer_t* target) 18 // { 19 // if(!(target)) return NULL; 20 // 21 // bool managed = gc_is_managed(target); 22 // gc_object_header* obj = gc_get_object_ptr((void*)target->ptr); 23 // 24 // check(gc_is_valide(obj)); 25 // 26 // gcpointer_t** prev_next_ptr = managed ? &obj->type_chain : &obj->root_chain; 27 // while((*prev_next_ptr) && (*prev_next_ptr) != target) 28 // { 29 // prev_next_ptr = &(*prev_next_ptr)->next; 30 // } 31 // 32 // return prev_next_ptr; 33 // } 17 gcpointer_t** gc_find_previous_ref(gcpointer_t* target) 18 { 19 if(!(target)) return NULL; 34 20 35 // void* gc_allocate(size_t target_size) 36 // { 37 // size_t size = gc_compute_size(target_size + sizeof(gc_object_header)); 38 // 39 // check(size < POOL_SIZE_BYTES); 40 // 41 // void* block = NULL; 42 // gc_state* gc = gc_get_state(); 43 // 44 // if((intptr_t)(block = gc_try_allocate(gc, size))) return gc_finish_alloc_block(block, size, target_size); 45 // 46 // gc_collect(gc); 47 // 48 // if((intptr_t)(block = gc_try_allocate(gc, size))) return gc_finish_alloc_block(block, size, target_size); 49 // 50 // gc_allocate_pool(gc); 51 // 52 // if((intptr_t)(block = gc_try_allocate(gc, size))) return gc_finish_alloc_block(block, size, target_size); 53 // 54 // ofstream_stderr() | "ERROR: allocation in new pool failed" | endl; 55 // abort(); 56 // 57 // return NULL; 58 // } 59 // 60 // void* gc_finish_alloc_block(void* block, size_t actual_size, size_t target_size) 61 // { 62 // void* data = (void*)(((intptr_t)block) + sizeof(gc_object_header)); 63 // void* header = block; 64 // 65 // check(((intptr_t)data) > ((intptr_t)block)); 66 // check(((intptr_t)data) >= ((intptr_t)header)); 67 // check(is_aligned(data)); 68 // check(((intptr_t)data) + target_size <= ((intptr_t)block) + actual_size); 69 // 70 // gc_object_header* obj = gc_object_header_placement_ctor(header, actual_size); 71 // 72 // // (void)obj; //remove unsused warning since this is for debug 73 // check(obj == get_object_ptr(data)); 74 // 75 // gc_register_allocation(gc_get_state(), actual_size); 76 // 77 // return data; 78 // } 79 // 80 // void gc_process_reference(void** ref, worklist_t* worklist) 81 // { 82 // check(!gc_is_in_heap(gc_get_state(), ref)); 83 // 84 // gc_object_header* ptr = gc_get_object_ptr(*ref); 85 // if(ptr) 86 // { 87 // if(!ptr->is_forwarded) 88 // { 89 // gc_copy_object(ptr); 90 // 91 // gc_scan_object(ptr->forward, worklist); 92 // 93 // gc_assign_reference(ref, ptr->forward); 94 // } 95 // else 96 // { 97 // //duplication to help debug 98 // gc_assign_reference(ref, ptr->forward); 99 // } 100 // } 101 // } 102 // 103 // void gc_assign_reference(void** ref, gc_object_header* ptr) 104 // { 105 // void* address = (void*)(((intptr_t)ptr) + sizeof(gc_object_header)); 106 // 107 // gc_write_aligned_ptr(ref, address); 108 // } 109 // 110 // gc_object_header* gc_copy_object(gc_object_header* ptr) 111 // { 112 // check(!ptr->forward); 113 // check(!ptr->is_forwarded); 114 // check(gc_is_from_space(gc_pool_of(ptr))); 115 // 116 // gc_memory_pool* pool = gc_pool_of(ptr)->mirror; 117 // 118 // void* new_block = gc_pool_allocate(pool, ptr->size, true); 119 // 120 // memcpy(new_block, ptr, ptr->size); 121 // 122 // gc_object_header* fwd_ptr = gc_object_header_placement_copy_ctor(new_block, ptr); 123 // 124 // ptr->forward = fwd_ptr; 125 // ptr->is_forwarded = true; 126 // 127 // return fwd_ptr; 128 // } 129 // 130 // void gc_scan_object(gc_object_header* object, worklist_t* worklist) 131 // { 132 // gcpointer_t* field = object->type_chain; 133 // while(field) 134 // { 135 // check(((intptr_t)field) > ((intptr_t)object)); 136 // check(((intptr_t)field) < ((intptr_t)((intptr_t)object) + object->size)); 137 // 138 // check(gc_is_in_to_space(gc_get_state(), &type->ptr)); 139 // 140 // push_back(worklist, &field->ptr); 141 // 142 // field = field->next; 143 // } 144 // } 21 bool managed = gc_is_managed(target); 22 gc_object_header* obj = gc_get_object_ptr((void*)target->ptr); 23 24 check(gc_is_valide(obj)); 25 26 gcpointer_t** prev_next_ptr = managed ? &obj->type_chain : &obj->root_chain; 27 while((*prev_next_ptr) && (*prev_next_ptr) != target) 28 { 29 prev_next_ptr = &(*prev_next_ptr)->next; 30 } 31 32 return prev_next_ptr; 33 } 34 35 void* gc_allocate(size_t target_size) 36 { 37 size_t size = gc_compute_size(target_size + sizeof(gc_object_header)); 38 39 check(size < POOL_SIZE_BYTES); 40 41 void* block = NULL; 42 gc_state* gc = gc_get_state(); 43 44 if((intptr_t)(block = gc_try_allocate(gc, size))) return gc_finish_alloc_block(block, size, target_size); 45 46 gc_collect(gc); 47 48 if((intptr_t)(block = gc_try_allocate(gc, size))) return gc_finish_alloc_block(block, size, target_size); 49 50 gc_allocate_pool(gc); 51 52 if((intptr_t)(block = gc_try_allocate(gc, size))) return gc_finish_alloc_block(block, size, target_size); 53 54 checkf(false, "ERROR: allocation in new pool failed"); 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 gc_process_reference(void** ref, worklist_t* worklist) 80 { 81 check(!gc_is_in_heap(gc_get_state(), ref)); 82 83 gc_object_header* ptr = gc_get_object_ptr(*ref); 84 if(ptr) 85 { 86 if(!ptr->is_forwarded) 87 { 88 gc_copy_object(ptr); 89 90 gc_scan_object(ptr->forward, worklist); 91 92 gc_assign_reference(ref, ptr->forward); 93 } 94 else 95 { 96 //duplication to help debug 97 gc_assign_reference(ref, ptr->forward); 98 } 99 } 100 } 101 102 void gc_assign_reference(void** ref, gc_object_header* ptr) 103 { 104 void* address = (void*)(((intptr_t)ptr) + sizeof(gc_object_header)); 105 106 gc_write_aligned_ptr(ref, address); 107 } 108 109 gc_object_header* gc_copy_object(gc_object_header* ptr) 110 { 111 check(!ptr->forward); 112 check(!ptr->is_forwarded); 113 check(gc_is_from_space(gc_pool_of(ptr))); 114 115 gc_memory_pool* pool = gc_pool_of(ptr)->mirror; 116 117 void* new_block = gc_pool_allocate(pool, ptr->size, true); 118 119 memcpy(new_block, ptr, ptr->size); 120 121 gc_object_header* fwd_ptr = gc_object_header_placement_copy_ctor(new_block, ptr); 122 123 ptr->forward = fwd_ptr; 124 ptr->is_forwarded = true; 125 126 return fwd_ptr; 127 } 128 129 void gc_scan_object(gc_object_header* object, worklist_t* worklist) 130 { 131 gcpointer_t* field = object->type_chain; 132 while(field) 133 { 134 check(((intptr_t)field) > ((intptr_t)object)); 135 check(((intptr_t)field) < ((intptr_t)((intptr_t)object) + object->size)); 136 137 check(gc_is_in_to_space(gc_get_state(), &type->ptr)); 138 139 intptr_t* ref = &field->ptr; 140 // push_back(worklist, ref); 141 142 field = field->next; 143 } 144 } -
TabularUnified src/examples/gc_no_raii/src/internal/collector.h ¶
r356bb95 r385c130 4 4 #include <stdint.h> 5 5 6 //#include "tools.h"6 #include "tools.h" 7 7 // 8 //#include "gcpointers.h"8 #include "gcpointers.h" 9 9 #include "state.h" 10 10 #include "internal/gc_tools.h" 11 //#include "internal/globals.h"12 //#include "internal/object_header.h"13 //#include "internal/state.h"11 #include "internal/globals.h" 12 #include "internal/object_header.h" 13 #include "internal/state.h" 14 14 #include "tools/worklist.h" 15 15 … … 19 19 } 20 20 21 //inline gc_object_header* gc_get_object_ptr(void* ptr)22 //{23 //void* clean = gc_get_aligned_ptr(ptr);24 //return ((gc_object_header*)clean) - 1;25 //}21 inline gc_object_header* gc_get_object_ptr(void* ptr) 22 { 23 void* clean = gc_get_aligned_ptr(ptr); 24 return ((gc_object_header*)clean) - 1; 25 } 26 26 27 // inlinegc_memory_pool* gc_pool_of(void* address)28 //{29 //return (struct gc_memory_pool*)(((intptr_t)address) & POOL_PTR_MASK);30 //}31 // 32 //inline void gc_conditional_collect()33 //{34 //if(gc_needs_collect(gc_get_state()))35 //{36 //gc_collect(gc_get_state());37 //}38 //}39 // 40 //gcpointer_t** gc_find_previous_ref(gcpointer_t* target);41 // 42 //void* gc_allocate(size_t size);43 // 44 //void gc_process_reference(void** ref, worklist_t* worklist);45 // 46 //struct gc_object_header* gc_copy_object(struct gc_object_header* ptr);47 // 48 //void gc_scan_object(struct gc_object_header* object, worklist_t* worklist);27 inline struct gc_memory_pool* gc_pool_of(void* address) 28 { 29 return (struct gc_memory_pool*)(((intptr_t)address) & POOL_PTR_MASK); 30 } 31 32 inline void gc_conditional_collect() 33 { 34 if(gc_needs_collect(gc_get_state())) 35 { 36 gc_collect(gc_get_state()); 37 } 38 } 39 40 gcpointer_t** gc_find_previous_ref(gcpointer_t* target); 41 42 void* gc_allocate(size_t size); 43 44 void gc_process_reference(void** ref, worklist_t* worklist); 45 46 struct gc_object_header* gc_copy_object(struct gc_object_header* ptr); 47 48 void gc_scan_object(struct gc_object_header* object, worklist_t* worklist); -
TabularUnified src/examples/gc_no_raii/src/internal/gc_tools.h ¶
r356bb95 r385c130 12 12 } 13 13 14 //inline void* gc_get_aligned_ptr(void* address)15 //{16 //return (void*)(((intptr_t)address) & (OBJECT_PTR_MASK));17 //}18 // 19 //inline void* gc_write_aligned_ptr(void** reference, void* address)20 //{21 //size_t ref_last_bits = ((intptr_t)*reference) & (~OBJECT_PTR_MASK);22 // 23 //size_t new_val = ((intptr_t)address) & OBJECT_PTR_MASK;24 // 25 //(*reference) = (void*)(new_val | ref_last_bits);26 // 27 //return *reference;28 //}29 // 30 //inline size_t gc_compute_size(size_t size)31 //{32 //size_t word_size = ((size - 1) / OBJECT_ALLIGNMENT) + 1;33 //size_t ret = word_size * OBJECT_ALLIGNMENT;34 // 35 //check(ret >= size);36 //check((ret % OBJECT_ALLIGNMENT) == 0);37 //check( ((size % OBJECT_ALLIGNMENT) != 0) || (ret == size) );38 // 39 //return ret;40 //}14 inline void* gc_get_aligned_ptr(void* address) 15 { 16 return (void*)(((intptr_t)address) & (OBJECT_PTR_MASK)); 17 } 18 19 inline void* gc_write_aligned_ptr(void** reference, void* address) 20 { 21 size_t ref_last_bits = ((intptr_t)*reference) & (~OBJECT_PTR_MASK); 22 23 size_t new_val = ((intptr_t)address) & OBJECT_PTR_MASK; 24 25 (*reference) = (void*)(new_val | ref_last_bits); 26 27 return *reference; 28 } 29 30 inline size_t gc_compute_size(size_t size) 31 { 32 size_t word_size = ((size - 1) / OBJECT_ALLIGNMENT) + 1; 33 size_t ret = word_size * OBJECT_ALLIGNMENT; 34 35 check(ret >= size); 36 check((ret % OBJECT_ALLIGNMENT) == 0); 37 check( ((size % OBJECT_ALLIGNMENT) != 0) || (ret == size) ); 38 39 return ret; 40 } -
TabularUnified src/examples/gc_no_raii/src/internal/memory_pool.h ¶
r356bb95 r385c130 24 24 uint8_t start_p[1]; 25 25 }; 26 27 void gc_memory_pool_ctor(gc_memory_pool *const this, 28 size_t size, 29 gc_memory_pool* next, 30 gc_memory_pool* mirror, 31 uint8_t type 32 ); 26 33 27 34 struct gc_pool_object_iterator … … 58 65 void gc_reset_pool(gc_memory_pool* pool); 59 66 67 inline size_t gc_pool_size_used(const gc_memory_pool* pool) 68 { 69 return pool->free_p - pool->start_p; 70 } 71 60 72 inline size_t gc_pool_size_total(const gc_memory_pool* pool) 61 73 { -
TabularUnified src/examples/gc_no_raii/src/internal/state.c ¶
r356bb95 r385c130 1 1 #include "state.h" 2 2 3 //#include <stdlib.h>4 // 5 // //general purpouse includes6 //#include "tools.h"7 // 8 // //platform abstraction includes9 //#include "allocate-pool.h"10 // 11 // //gc internal includes12 // //#include "globals.h"13 //#include "memory_pool.h"3 #include <stdlib.h> 4 5 //general purpouse includes 6 #include "tools.h" 7 8 //platform abstraction includes 9 #include "allocate-pool.h" 10 11 //gc internal includes 12 #include "globals.h" 13 #include "memory_pool.h" 14 14 // #include "memory_pool_iterator.h" 15 // #include "object_header.h" 16 // 17 // void swap(gc_state* state); 18 // void sweep_roots(worklist_t worklist); 19 // void clear(gc_state* state); 20 // void calc_usage(gc_state* state); 21 // 22 // #if DEBUG 23 // bool roots_match(gc_state* state); 24 // bool no_from_space_ref(gc_state* state); 25 // #endif 26 27 // void gc_state_ctor(gc_state* state) 28 // { 29 // state->from_code = 0; 30 // state->to_space = NULL; 31 // state->from_space = NULL; 32 // state->total_space = 0; 33 // state->used_space = 0; 34 // // state->pools_table(); 35 // 36 // gc_allocate_pool(state); 37 // 38 // state->is_initialized = true; 39 // } 15 #include "object_header.h" 16 17 void gc_state_swap(gc_state *const this); 18 void gc_state_sweep_roots(gc_state *const this, worklist_t* worklist); 19 void gc_state_clear(gc_state *const this); 20 void gc_state_calc_usage(gc_state *const this); 21 22 #if DEBUG 23 bool gc_state_roots_match(gc_state *const this); 24 bool gc_state_no_from_space_ref(gc_state *const this); 25 #endif 26 27 gc_state* gc_get_state() 28 { 29 static gc_state s; 30 if(!s.is_initialized) gc_state_ctor(&s); 31 return &s; 32 } 33 34 void gc_state_ctor(gc_state *const this) 35 { 36 this->from_code = 0; 37 this->to_space = NULL; 38 this->from_space = NULL; 39 this->total_space = 0; 40 this->used_space = 0; 41 // state->pools_table(); 42 43 gc_allocate_pool(this); 44 45 this->is_initialized = true; 46 } 40 47 41 48 // bool state::is_in_heap(void* address) const … … 86 93 // return NULL; 87 94 // } 88 // 89 // void* state::try_allocate(size_t size) 90 // { 91 // memory_pool* pool = from_space; 92 // while(pool) 93 // { 94 // if(pool->size_left() > size) 95 // { 96 // return pool->allocate(size, true); 97 // } 98 // pool = pool->next(); 99 // } 100 // 101 // return nullptr; 102 // } 103 // 104 // void state::allocate_pool() 105 // { 106 // memory_pool* old_from_space = from_space; 107 // memory_pool* old_to_space = to_space; 108 // 109 // from_space = reinterpret_cast<memory_pool*>(pal_allocPool(POOL_SIZE_BYTES, 1)); 110 // to_space = reinterpret_cast<memory_pool*>(pal_allocPool(POOL_SIZE_BYTES, 1)); 111 // 112 // new (from_space) memory_pool(POOL_SIZE_BYTES, old_from_space, to_space, from_code); 113 // new (to_space) memory_pool(POOL_SIZE_BYTES, old_to_space, from_space, (~from_code) & 0x01); 114 // 115 // total_space += from_space->size(); 116 // 117 // pools_table.push_back(from_space); 118 // pools_table.push_back(to_space); 119 // } 120 // 121 // void state::collect() 122 // { 123 // DEBUG("collecting"); 124 // DEBUG("previous usage " << used_space << " / " << total_space); 125 // 126 // std::vector<void**> worklist; 127 // sweep_roots(worklist); 128 // 129 // while(!worklist.empty()) 130 // { 131 // void** ref = worklist.back(); 132 // worklist.pop_back(); 133 // process_reference(ref, worklist); 134 // } 135 // 136 // check(roots_match()); 137 // check(no_from_space_ref()); 138 // 139 // swap(); 140 // 141 // calc_usage(); 142 // 143 // if(needs_collect()) allocate_pool(); 144 // 145 // DEBUG("done"); 146 // } 95 96 void* gc_state_try_allocate(gc_state *const this, size_t size) 97 { 98 gc_memory_pool* pool = this->from_space; 99 while(pool != (gc_memory_pool*)0) 100 { 101 if(gc_pool_size_left(pool) > size) 102 { 103 return gc_pool_allocate(pool, size, true); 104 } 105 pool = pool->next; 106 } 107 108 return (void*)0; 109 } 110 111 void gc_state_allocate_pool(gc_state *const this) 112 { 113 gc_memory_pool* old_from_space = this->from_space; 114 gc_memory_pool* old_to_space = this->to_space; 115 116 this->from_space = (gc_memory_pool*)(pal_allocPool(POOL_SIZE_BYTES, 1)); 117 this->to_space = (gc_memory_pool*)(pal_allocPool(POOL_SIZE_BYTES, 1)); 118 119 gc_memory_pool_ctor(this->from_space, POOL_SIZE_BYTES, old_from_space, this->to_space, this->from_code); 120 gc_memory_pool_ctor(this->to_space, POOL_SIZE_BYTES, old_to_space, this->from_space, (~this->from_code) & 0x01); 121 122 this->total_space += gc_pool_size_used(this->from_space); 123 124 // pools_table.push_back(from_space); 125 // pools_table.push_back(to_space); 126 } 127 128 void gc_state_collect(gc_state *const this) 129 { 130 // DEBUG("collecting"); 131 // DEBUG("previous usage " << this->used_space << " / " << this->total_space); 132 133 worklist_t worklist; 134 // vector_ctor(&worklist); 135 gc_state_sweep_roots(this, &worklist); 136 137 while(!empty(&worklist)) 138 { 139 void** ref = back(&worklist); 140 pop_back(&worklist); 141 gc_state_process_reference(this, ref, &worklist); 142 } 143 // 144 // check(gc_state_roots_match(this)); 145 // check(gc_state_no_from_space_ref(this)); 146 // 147 // gc_state_swap(this); 148 // 149 // gc_state_calc_usage(this); 150 // 151 // if(gc_state_needs_collect(this)) gc_state_allocate_pool(this); 152 153 // DEBUG("done"); 154 // dtor(&worklist); 155 } 147 156 // 148 157 // void state::swap() -
TabularUnified src/examples/gc_no_raii/src/internal/state.h ¶
r356bb95 r385c130 22 22 void gc_state_ctor(struct gc_state* state); 23 23 24 static inline gc_state* gc_get_state() 25 { 26 static gc_state s; 27 if(!s.is_initialized) gc_state_ctor(&s); 28 return &s; 29 } 24 gc_state* gc_get_state(); 30 25 31 26 inline bool gc_needs_collect(gc_state* state) -
TabularUnified src/examples/gc_no_raii/src/tools/worklist.h ¶
r356bb95 r385c130 5 5 6 6 #include "vector.h" 7 /*8 typedef intptr_t* worklist_element_t;9 10 struct worklist_t11 {12 size_t count;13 worklist_element_t* data;14 };15 16 void push_back(worklist_t* worklist, worklist_element_t element);17 */18 7 19 8 typedef vector(intptr_t*, heap_allocator(intptr_t*)) worklist_t; -
TabularUnified src/examples/gc_no_raii/src/vector.h ¶
r356bb95 r385c130 8 8 //------------------------------------------------------------------------------ 9 9 //Declaration 10 context allocator_c(type T, type allocator_t) { 10 trait allocator_c(otype T, otype allocator_t) { 11 void ctor(allocator_t*); 12 void dtor(allocator_t*); 11 13 void realloc(allocator_t*, size_t); 12 14 T* data(allocator_t*); 13 15 }; 14 16 15 forall( type T,type allocator_t | allocator_c(T, allocator_t))17 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 16 18 struct vector 17 19 { … … 21 23 22 24 //------------------------------------------------------------------------------ 25 //Initialization 26 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 27 void vector_ctor(vector(T, allocator_t) *const this); 28 29 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 30 void dtor(vector(T, allocator_t) *const this); 31 32 //------------------------------------------------------------------------------ 23 33 //Capacity 24 forall( type T,type allocator_t | allocator_c(T, allocator_t))25 bool empty(vector(T, allocator_t)*this)34 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 35 inline bool empty(vector(T, allocator_t) *const this) 26 36 { 27 37 return this->size == 0; 28 38 } 29 39 30 forall( type T,type allocator_t | allocator_c(T, allocator_t))31 bool size(vector(T, allocator_t)*this)40 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 41 inline bool size(vector(T, allocator_t) *const this) 32 42 { 33 43 return this->size; 34 44 } 35 45 36 forall( type T,type allocator_t | allocator_c(T, allocator_t))37 void reserve(vector(T, allocator_t)*this, size_t size)46 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 47 inline void reserve(vector(T, allocator_t) *const this, size_t size) 38 48 { 39 49 realloc(&this->storage, this->size+1); … … 42 52 //------------------------------------------------------------------------------ 43 53 //Element access 44 forall( type T,type allocator_t | allocator_c(T, allocator_t))45 T at(vector(T, allocator_t)*this, size_t index)54 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 55 inline T at(vector(T, allocator_t) *const this, size_t index) 46 56 { 47 // assert(index < this->size);57 // assert(index < this->size); 48 58 return data(&this->storage)[index]; 49 59 } 50 60 51 forall( type T,type allocator_t | allocator_c(T, allocator_t))52 T ?[?](vector(T, allocator_t)*this, size_t index)61 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 62 inline T ?[?](vector(T, allocator_t) *const this, size_t index) 53 63 { 54 64 return data(&this->storage)[index]; 55 65 } 56 66 57 forall( type T,type allocator_t | allocator_c(T, allocator_t))58 T front(vector(T, allocator_t)*this)67 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 68 inline T front(vector(T, allocator_t) *const this) 59 69 { 60 70 return data(&this->storage)[0]; 61 71 } 62 72 63 forall( type T,type allocator_t | allocator_c(T, allocator_t))64 T back(vector(T, allocator_t)*this, size_t index)73 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 74 inline T back(vector(T, allocator_t) *const this, size_t index) 65 75 { 66 76 return data(&this->storage)[this->size - 1]; … … 69 79 //------------------------------------------------------------------------------ 70 80 //Modifiers 71 forall(type T, type allocator_t | allocator_c(T, allocator_t)) 72 void push_back(vector(T, allocator_t)* this, T value) 73 { 74 realloc(&this->storage, this->size+1); 75 data(&this->storage)[this->size] = value; 76 this->size++; 77 } 81 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 82 void push_back(vector(T, allocator_t) *const this, T value); 78 83 79 forall(type T, type allocator_t | allocator_c(T, allocator_t)) 80 void pop_back(vector(T, allocator_t)* this) 81 { 82 this->size--; 83 DESTROY(data(&this->storage)[this->size]); 84 } 84 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 85 void pop_back(vector(T, allocator_t) *const this); 85 86 86 forall(type T, type allocator_t | allocator_c(T, allocator_t)) 87 void clear(vector(T, allocator_t)* this) 88 { 89 for(size_t i = 0; i < this->size; i++) 90 { 91 DESTROY(data(&this->storage)[this->size]); 92 } 93 this->size = 0; 94 } 87 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 88 void clear(vector(T, allocator_t) *const this); 95 89 96 90 //------------------------------------------------------------------------------ 97 91 //Allocator 98 forall( type T)92 forall(otype T) 99 93 struct heap_allocator 100 94 { … … 103 97 }; 104 98 105 forall(type T) 106 void realloc(heap_allocator(T)* this, size_t size) 107 { 108 static const size_t GROWTH_RATE = 2; 109 if(size > this->capacity) 110 { 111 this->capacity = GROWTH_RATE * size; 112 this->storage = (T*)realloc((void*)this->storage, this->capacity * sizeof(T)); 113 } 114 } 99 forall(otype T) 100 void ctor(heap_allocator(T) *const this); 115 101 116 forall(type T) 117 T* data(heap_allocator(T)* this) 102 forall(otype T) 103 void dtor(heap_allocator(T) *const this); 104 105 forall(otype T) 106 void realloc(heap_allocator(T) *const this, size_t size); 107 108 forall(otype T) 109 inline T* data(heap_allocator(T) *const this) 118 110 { 119 111 return this->storage;
Note: See TracChangeset
for help on using the changeset viewer.