- Timestamp:
- Apr 21, 2016, 10:27:55 AM (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:
- 8c8b614
- Parents:
- 3365b37
- Location:
- src/examples/gc_no_raii
- Files:
-
- 4 added
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
src/examples/gc_no_raii/bug-repro/push_back.c
r3365b37 rdf4aea7 2 2 #include <stdint.h> 3 3 4 //------------------------------------------------------------------------------ 5 //Declaration 6 trait allocator_c(otype T, otype allocator_t) { 7 void realloc(allocator_t*, size_t); 8 T* data(allocator_t*); 9 }; 10 11 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 12 struct vector 13 { 14 allocator_t storage; 15 size_t size; 16 }; 17 18 //------------------------------------------------------------------------------ 19 //Allocator 20 forall(otype T) 21 struct heap_allocator 22 { 23 T* storage; 24 size_t capacity; 25 }; 26 27 forall(otype T) 28 void realloc(heap_allocator(T)* this, size_t size); 29 30 forall(otype T) 31 inline T* data(heap_allocator(T)* this) 32 { 33 return this->storage; 34 } 35 36 //------------------------------------------------------------------------------ 37 //Modifiers 38 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 39 void push_back(vector(T, allocator_t)* this, T value); 4 #include "push_back.h" 40 5 41 6 typedef vector(intptr_t*, heap_allocator(intptr_t*)) worklist_t; 42 7 43 inlinevoid test()8 void test() 44 9 { 45 10 worklist_t w; 46 intptr_t zero = 0; 47 push_back(&w, &zero); 11 if(!empty(&w)) 12 { 13 intptr_t zero = 0; 14 push_back(&w, &zero); 15 } 48 16 } -
src/examples/gc_no_raii/src/internal/collector.c
r3365b37 rdf4aea7 67 67 check(((intptr_t)data) + target_size <= ((intptr_t)block) + actual_size); 68 68 69 gc_object_header* obj = gc_object_header_placement_ctor(header, actual_size);69 gc_object_header* obj = placement_ctor(header, actual_size); 70 70 71 71 (void)obj; //remove unsused warning since this is for debug … … 119 119 memcpy(new_block, ptr, ptr->size); 120 120 121 gc_object_header* fwd_ptr = gc_object_header_placement_copy_ctor(new_block, ptr);121 gc_object_header* fwd_ptr = placement_copy_ctor(new_block, ptr); 122 122 123 123 ptr->forward = fwd_ptr; -
src/examples/gc_no_raii/src/internal/memory_pool.h
r3365b37 rdf4aea7 53 53 bool ?!=?(const gc_pool_object_iterator lhs, const gc_pool_object_iterator rhs); 54 54 55 gc_pool_object_iterator begin(gc_memory_pool* const); 56 gc_pool_object_iterator end(gc_memory_pool* const); 57 55 58 gc_pool_object_iterator* ++?(gc_pool_object_iterator* it); 56 59 -
src/examples/gc_no_raii/src/internal/object_header.h
r3365b37 rdf4aea7 30 30 }; 31 31 32 gc_object_header* gc_object_header_placement_ctor(void* address, size_t size);33 gc_object_header* gc_object_header_placement_copy_ctor(void* address, gc_object_header*other);32 gc_object_header* placement_ctor(void* address, size_t size); 33 gc_object_header* placement_copy_ctor(void* address, const gc_object_header* const other); -
src/examples/gc_no_raii/src/internal/state.c
r3365b37 rdf4aea7 1 1 #include "state.h" 2 2 3 #include <stdlib .h>3 #include <stdlib> 4 4 5 5 //general purpouse includes … … 28 28 { 29 29 static gc_state s; 30 if(!s.is_initialized) gc_state_ctor(&s);30 if(!s.is_initialized) ctor(&s); 31 31 return &s; 32 32 } 33 33 34 void gc_state_ctor(gc_state *const this)34 void ctor(gc_state *const this) 35 35 { 36 36 this->from_code = 0; … … 39 39 this->total_space = 0; 40 40 this->used_space = 0; 41 // state->pools_table();41 ctor(&this->pools_table); 42 42 43 43 gc_allocate_pool(this); … … 46 46 } 47 47 48 // bool state::is_in_heap(void* address) const 49 //{50 //memory_pool* target_pool = gc_pool_of(address);51 // 52 // auto first = pools_table.cbegin();53 // auto last = pools_table.cend();54 // auto result = std::find(first,last, target_pool);55 // return result != last && (*result)->is_from_space();56 //}57 58 // bool state::is_in_to_space(void* address) const 59 //{60 // const memory_pool* target_pool =pool_of(address);61 // 62 // auto first = pools_table.cbegin();63 // auto last = pools_table.cend();64 // auto result = std::find(first,last, target_pool);65 // return result != last && !(*result)->is_from_space();66 //}67 // 68 //gc_object_header* gc_get_object_for_ref(gc_state* state, void* member)69 //{70 //intptr_t target = ((intptr_t)member);71 //if(!gc_is_in_heap(state, member)) return NULL;72 // 73 //gc_memory_pool* pool = gc_pool_of(member);74 //gc_pool_object_iterator it = gc_pool_iterator_for(pool, member);75 //gc_pool_object_iterator end = gc_pool_end(pool);76 // 77 //while(it != end)78 //{79 //gc_object_header* object = *it;80 //{81 //intptr_t start = ((intptr_t)object);82 //intptr_t end = ((intptr_t)start + object->size);83 //if(start < target && end > target)84 //{85 //return object;86 //}87 //}88 //++it;89 //}90 // 91 //checkf(false, "is_in_heap() and iterator_for() return inconsistent data");92 //abort();93 //return NULL;94 //}95 96 void* gc_ state_try_allocate(gc_state *const this, size_t size)48 bool gc_state_is_in_heap(const gc_state* const this, void* address) 49 { 50 gc_memory_pool* target_pool = gc_pool_of(address); 51 52 gc_memory_pool** first = cbegin(&this->pools_table); 53 gc_memory_pool** last = cend(&this->pools_table); 54 gc_memory_pool** result = find(first, &last, target_pool); 55 return result != last && gc_pool_is_from_space(*result); 56 } 57 58 bool gc_state_is_in_to_space(const gc_state* const this, void* address) 59 { 60 gc_memory_pool* target_pool = gc_pool_of(address); 61 62 gc_memory_pool** first = cbegin(&this->pools_table); 63 gc_memory_pool** last = cend(&this->pools_table); 64 gc_memory_pool** result = find(first, &last, target_pool); 65 return result != last && !gc_pool_is_from_space(*result); 66 } 67 68 gc_object_header* gc_get_object_for_ref(gc_state* state, void* member) 69 { 70 intptr_t target = ((intptr_t)member); 71 if(!gc_is_in_heap(state, member)) return NULL; 72 73 gc_memory_pool* pool = gc_pool_of(member); 74 gc_pool_object_iterator it = gc_pool_iterator_for(pool, member); 75 gc_pool_object_iterator end = gc_pool_end(pool); 76 77 while(it != end) 78 { 79 gc_object_header* object = *it; 80 { 81 intptr_t start = ((intptr_t)object); 82 intptr_t end = ((intptr_t)start + object->size); 83 if(start < target && end > target) 84 { 85 return object; 86 } 87 } 88 ++it; 89 } 90 91 checkf(false, "is_in_heap() and iterator_for() return inconsistent data"); 92 abort(); 93 return NULL; 94 } 95 96 void* gc_try_allocate(gc_state *const this, size_t size) 97 97 { 98 98 gc_memory_pool* pool = this->from_space; … … 122 122 this->total_space += gc_pool_size_used(this->from_space); 123 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)124 push_back(&this->pools_table, this->from_space); 125 push_back(&this->pools_table, this->to_space); 126 } 127 128 void gc_state_collect(gc_state* const this) 129 129 { 130 130 // DEBUG("collecting"); … … 132 132 133 133 worklist_t worklist; 134 // vector_ctor(&worklist);134 ctor(&worklist); 135 135 gc_state_sweep_roots(this, &worklist); 136 136 137 137 while(!empty(&worklist)) 138 138 { 139 void** ref = back(&worklist);139 intptr_t* ref = back(&worklist); 140 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);141 gc_process_reference((void**)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_needs_collect(this)) gc_state_allocate_pool(this); 152 152 153 153 // DEBUG("done"); 154 // dtor(&worklist); 155 } 156 // 157 // void state::swap() 158 // { 159 // std::swap(from_space, to_space); 160 // 161 // memory_pool* pool = to_space; 162 // while(pool) 163 // { 164 // pool->reset(); 165 // pool = pool->next(); 166 // } 167 // 168 // from_code = (~from_code) & 0x01; 169 // 170 // #if _DEBUG 171 // { 172 // memory_pool* pool = from_space; 173 // while(pool) 174 // { 175 // check(pool->is_from_space()); 176 // pool = pool->next(); 177 // } 178 // 179 // pool = to_space; 180 // while(pool) 181 // { 182 // check(!pool->is_from_space()); 183 // pool = pool->next(); 184 // } 185 // } 186 // #endif 187 // } 188 // 189 // void state::sweep_roots(std::vector<void**>& worklist) 190 // { 191 // memory_pool* pool = from_space; 192 // while(pool) 193 // { 194 // for(object_header* object : *pool) 195 // { 196 // if(!object->root_chain) continue; 197 // 198 // copy_object(object); 199 // 200 // scan_object(object->forward, worklist); 201 // } 202 // 203 // pool = pool->next(); 204 // } 205 // } 206 // 207 // void state::clear() 208 // { 209 // memory_pool* pool = from_space; 210 // while(pool) 211 // { 212 // pool->reset(); 213 // pool = pool->next(); 214 // } 215 // 216 // pool = to_space; 217 // while(pool) 218 // { 219 // pool->reset(); 220 // pool = pool->next(); 221 // } 222 // } 223 // 224 // void state::calc_usage() 225 // { 226 // total_space = 0; 227 // used_space = 0; 228 // 229 // memory_pool* pool = from_space; 230 // while(pool) 231 // { 232 // size_t size = pool->size(); 233 // size_t used = size - pool->size_left(); 234 // check(used <= size); 235 // total_space += size; 236 // used_space += used; 237 // 238 // pool = pool->next(); 239 // } 240 // } 241 // 242 // #if _DEBUG 243 // bool state::roots_match() 244 // { 245 // memory_pool* pool = to_space; 246 // while(pool) 247 // { 248 // size_t size = 0; 249 // for(object_header* object : *pool) 250 // { 251 // size += object->size; 252 // 253 // gcpointer_base* ptr = object->root_chain; 254 // while(ptr) 255 // { 256 // check(get_object_ptr(ptr->m_ptr) == object); 257 // ptr = ptr->m_next; 258 // } 259 // } 260 // 261 // check(size + pool->size_left() == pool->size()); 262 // 263 // pool = pool->next(); 264 // } 265 // 266 // return true; 267 // } 268 // 269 // bool state::no_from_space_ref() 270 // { 271 // memory_pool* pool = to_space; 272 // while(pool) 273 // { 274 // void** potential_ref = (void**)pool->m_start; 275 // while(potential_ref < (void**)pool->m_free) 276 // { 277 // check(!is_in_heap(*potential_ref)); 278 // potential_ref++; 279 // } 280 // 281 // pool = pool->next(); 282 // } 283 // 284 // return true; 285 // } 286 // #endif 154 dtor(&worklist); 155 } 156 157 void gc_state_swap(gc_state* const this) 158 { 159 swap(&this->from_space, &this->to_space); 160 161 gc_memory_pool* pool = this->to_space; 162 while(pool) 163 { 164 gc_reset_pool(pool); 165 pool = pool->next; 166 } 167 168 this->from_code = (~this->from_code) & 0x01; 169 170 #if _DEBUG 171 { 172 gc_memory_pool* pool = this->from_space; 173 while(pool) 174 { 175 check(gc_pool_is_from_space(pool)); 176 pool = pool->next; 177 } 178 179 pool = this->to_space; 180 while(pool) 181 { 182 check(!gc_pool_is_from_space(pool)); 183 pool = pool->next; 184 } 185 } 186 #endif 187 } 188 189 void gc_state_sweep_roots(gc_state* const this, worklist_t* worklist) 190 { 191 gc_memory_pool* pool = this->from_space; 192 while(pool) 193 { 194 gc_pool_object_iterator it = begin(pool); 195 gc_pool_object_iterator end = end(pool); 196 for(;it != end; ++it) 197 { 198 gc_object_header* object = *it; 199 if(!object->root_chain) continue; 200 201 gc_copy_object(object); 202 203 gc_scan_object(object->forward, worklist); 204 } 205 206 pool = pool->next; 207 } 208 } 209 210 void gc_state_clear(gc_state* const this) 211 { 212 gc_memory_pool* pool = this->from_space; 213 while(pool) 214 { 215 gc_reset_pool(pool); 216 pool = pool->next; 217 } 218 219 pool = this->to_space; 220 while(pool) 221 { 222 gc_reset_pool(pool); 223 pool = pool->next; 224 } 225 } 226 227 void gc_state_calc_usage(gc_state* const this) 228 { 229 this->total_space = 0; 230 this->used_space = 0; 231 232 gc_memory_pool* pool = this->from_space; 233 while(pool) 234 { 235 size_t size = gc_pool_size_total(pool); 236 size_t used = gc_pool_size_used(pool); 237 check(used <= size); 238 this->total_space += size; 239 this->used_space += used; 240 241 pool = pool->next; 242 } 243 } 244 245 #if _DEBUG 246 bool gc_state_roots_match(gc_state* const this) 247 { 248 gc_memory_pool* pool = this->to_space; 249 while(pool) 250 { 251 size_t size = 0; 252 gc_pool_object_iterator it = begin(pool); 253 gc_pool_object_iterator end = end(pool); 254 for(;it != end; ++it) 255 { 256 gc_object_header* object = *it; 257 size += object->size; 258 259 gcpointer_base* ptr = object->root_chain; 260 while(ptr) 261 { 262 check(get_object_ptr(ptr->m_ptr) == object); 263 ptr = ptr->m_next; 264 } 265 } 266 267 check(size + gc_pool_size_used(pool) == gc_pool_size_total(pool)); 268 269 pool = pool->next; 270 } 271 272 return true; 273 } 274 275 bool gc_state_no_from_space_ref(gc_state* const this) 276 { 277 gc_memory_pool* pool = this->to_space; 278 while(pool) 279 { 280 void** potential_ref = (void**)pool->m_start; 281 while(potential_ref < (void**)pool->m_free) 282 { 283 check(!gc_is_in_heap(*potential_ref)); 284 potential_ref++; 285 } 286 287 pool = pool->next; 288 } 289 290 return true; 291 } 292 #endif -
src/examples/gc_no_raii/src/internal/state.h
r3365b37 rdf4aea7 5 5 6 6 #include "tools.h" 7 #include "vector.h" 8 9 typedef vector(struct gc_memory_pool*, heap_allocator(struct gc_memory_pool*)) pools_table_t; 7 10 8 11 struct gc_state … … 16 19 size_t used_space; 17 20 18 // const struct memory_pool*pools_table;19 // size_tpools_table_count;21 pools_table_t pools_table; 22 size_t pools_table_count; 20 23 }; 21 24 22 void gc_state_ctor(struct gc_state* state);25 void ctor(struct gc_state* state); 23 26 24 27 gc_state* gc_get_state(); … … 39 42 bool gc_is_in_to_space(const gc_state* state, void* address); 40 43 41 inline uint8_t gc_from_space_code(const gc_state * state)44 inline uint8_t gc_from_space_code(const gc_state *const this) 42 45 { 43 return state->from_code;46 return this->from_code; 44 47 } 45 48 -
src/examples/gc_no_raii/src/tools.h
r3365b37 rdf4aea7 3 3 #include "tools/checks.h" 4 4 #include "tools/print.h" 5 6 // forall(otype T) 7 // inline void swap(T* const a, T* const b) 8 // { 9 // T* temp = a; 10 // *a = *b; 11 // *b = *temp; 12 // } 13 14 trait has_equal(otype T) 15 { 16 signed int ?==?(T a, T b); 17 }; 18 19 trait InputIterator_t(otype T, otype InputIterator) 20 { 21 signed int ?==?(InputIterator a, InputIterator b); 22 signed int ?!=?(InputIterator a, InputIterator b); 23 T *?(InputIterator a); 24 InputIterator ++?(InputIterator* a); 25 InputIterator ?++(InputIterator* a); 26 }; 27 28 forall(otype T | has_equal(T), otype InputIterator | InputIterator_t(T, InputIterator)) 29 inline InputIterator find( InputIterator first, const InputIterator* const last, T val) 30 { 31 while ( first != *last) 32 { 33 if(*first == val) return first; 34 ++first; 35 } 36 return *last; 37 } -
src/examples/gc_no_raii/src/tools/print.h
r3365b37 rdf4aea7 3 3 #if _DEBUG 4 4 5 #include "fstream.h" 6 7 extern ofstream *sout; 5 #include <fstream> 8 6 9 7 #define DEBUG_OUT(x) sout | x | endl; -
src/examples/gc_no_raii/src/vector.h
r3365b37 rdf4aea7 1 #pragma once 2 1 3 #include <assert.h> 2 4 #include <stdbool.h> … … 8 10 //------------------------------------------------------------------------------ 9 11 //Declaration 10 trait allocator_c(otype T, otype allocator_t) { 11 void ctor(allocator_t*); 12 void dtor(allocator_t*); 13 void realloc(allocator_t*, size_t); 14 T* data(allocator_t*); 12 trait allocator_c(otype T, otype allocator_t) 13 { 14 void ctor(allocator_t* const); 15 void dtor(allocator_t* const); 16 void realloc(allocator_t* const, size_t); 17 T* data(allocator_t* const); 15 18 }; 16 19 … … 25 28 //Initialization 26 29 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 27 void vector_ctor(vector(T, allocator_t) *const this);30 void ctor(vector(T, allocator_t) *const this); 28 31 29 32 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) … … 72 75 73 76 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 74 inline T back(vector(T, allocator_t) *const this , size_t index)77 inline T back(vector(T, allocator_t) *const this) 75 78 { 76 79 return data(&this->storage)[this->size - 1]; … … 87 90 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 88 91 void clear(vector(T, allocator_t) *const this); 92 93 //------------------------------------------------------------------------------ 94 //Iterators 95 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 96 inline T* begin(vector(T, allocator_t) *const this) 97 { 98 return data(&this->storage); 99 } 100 101 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 102 inline const T* cbegin(const vector(T, allocator_t) *const this) 103 { 104 return data(&this->storage); 105 } 106 107 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 108 inline T* end(vector(T, allocator_t) *const this) 109 { 110 return data(&this->storage) + this->size; 111 } 112 113 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 114 inline const T* cend(const vector(T, allocator_t) *const this) 115 { 116 return data(&this->storage) + this->size; 117 } 89 118 90 119 //------------------------------------------------------------------------------
Note: See TracChangeset
for help on using the changeset viewer.