Changeset 385c130 for src/examples/gc_no_raii/src/internal/state.c
- Timestamp:
- Apr 19, 2016, 1:19:25 PM (8 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
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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()
Note: See TracChangeset
for help on using the changeset viewer.