| 1 | #include "state.h"
|
|---|
| 2 |
|
|---|
| 3 | //general purpouse includes
|
|---|
| 4 | #include "tools.h"
|
|---|
| 5 |
|
|---|
| 6 | //platform abstraction includes
|
|---|
| 7 | #include "allocate-pool.h"
|
|---|
| 8 |
|
|---|
| 9 | //gc internal includes
|
|---|
| 10 | // #include "globals.h"
|
|---|
| 11 | // #include "memory_pool.h"
|
|---|
| 12 | // #include "memory_pool_iterator.h"
|
|---|
| 13 | // #include "object_header.h"
|
|---|
| 14 | //
|
|---|
| 15 | // void swap(gc_state* state);
|
|---|
| 16 | // void sweep_roots(worklist_t worklist);
|
|---|
| 17 | // void clear(gc_state* state);
|
|---|
| 18 | // void calc_usage(gc_state* state);
|
|---|
| 19 | //
|
|---|
| 20 | // #if DEBUG
|
|---|
| 21 | // bool roots_match(gc_state* state);
|
|---|
| 22 | // bool no_from_space_ref(gc_state* state);
|
|---|
| 23 | // #endif
|
|---|
| 24 | //
|
|---|
| 25 | // void gc_state_ctor(gc_state* state)
|
|---|
| 26 | // {
|
|---|
| 27 | // state->from_code(0)
|
|---|
| 28 | // state->to_space(nullptr)
|
|---|
| 29 | // state->from_space(nullptr)
|
|---|
| 30 | // state->total_space(0)
|
|---|
| 31 | // state->used_space(0)
|
|---|
| 32 | // state->pools_table()
|
|---|
| 33 | //
|
|---|
| 34 | // gc_allocate_pool(gc_state* state);
|
|---|
| 35 | // }
|
|---|
| 36 | //
|
|---|
| 37 | // bool state::is_in_heap(void* address) const
|
|---|
| 38 | // {
|
|---|
| 39 | // memory_pool* target_pool = pool_of(address);
|
|---|
| 40 | //
|
|---|
| 41 | // auto first = pools_table.cbegin();
|
|---|
| 42 | // auto last = pools_table.cend();
|
|---|
| 43 | // auto result = std::find(first, last, target_pool);
|
|---|
| 44 | // return result != last && (*result)->is_from_space();
|
|---|
| 45 | // }
|
|---|
| 46 | //
|
|---|
| 47 | // bool state::is_in_to_space(void* address) const
|
|---|
| 48 | // {
|
|---|
| 49 | // const memory_pool* target_pool = pool_of(address);
|
|---|
| 50 | //
|
|---|
| 51 | // auto first = pools_table.cbegin();
|
|---|
| 52 | // auto last = pools_table.cend();
|
|---|
| 53 | // auto result = std::find(first, last, target_pool);
|
|---|
| 54 | // return result != last && !(*result)->is_from_space();
|
|---|
| 55 | // }
|
|---|
| 56 | //
|
|---|
| 57 | // object_header* state::get_object_for_ref(void* member)
|
|---|
| 58 | // {
|
|---|
| 59 | // intptr_t target = intptr_t(member);
|
|---|
| 60 | // if(!is_in_heap(member)) return nullptr;
|
|---|
| 61 | //
|
|---|
| 62 | // memory_pool* pool = pool_of(member);
|
|---|
| 63 | // auto it = pool->iterator_for(member);
|
|---|
| 64 | // auto end = pool->end();
|
|---|
| 65 | //
|
|---|
| 66 | // while(it != end)
|
|---|
| 67 | // {
|
|---|
| 68 | // object_header* object = *it;
|
|---|
| 69 | // {
|
|---|
| 70 | // intptr_t start = intptr_t(object);
|
|---|
| 71 | // intptr_t end = intptr_t(start + object->size);
|
|---|
| 72 | // if(start < target && end > target)
|
|---|
| 73 | // {
|
|---|
| 74 | // return object;
|
|---|
| 75 | // }
|
|---|
| 76 | // }
|
|---|
| 77 | // ++it;
|
|---|
| 78 | // }
|
|---|
| 79 | //
|
|---|
| 80 | // checkf(false, "is_in_heap() and iterator_for() return inconsistent data");
|
|---|
| 81 | // abort();
|
|---|
| 82 | // return nullptr;
|
|---|
| 83 | // }
|
|---|
| 84 | //
|
|---|
| 85 | // void* state::try_allocate(size_t size)
|
|---|
| 86 | // {
|
|---|
| 87 | // memory_pool* pool = from_space;
|
|---|
| 88 | // while(pool)
|
|---|
| 89 | // {
|
|---|
| 90 | // if(pool->size_left() > size)
|
|---|
| 91 | // {
|
|---|
| 92 | // return pool->allocate(size, true);
|
|---|
| 93 | // }
|
|---|
| 94 | // pool = pool->next();
|
|---|
| 95 | // }
|
|---|
| 96 | //
|
|---|
| 97 | // return nullptr;
|
|---|
| 98 | // }
|
|---|
| 99 | //
|
|---|
| 100 | // void state::allocate_pool()
|
|---|
| 101 | // {
|
|---|
| 102 | // memory_pool* old_from_space = from_space;
|
|---|
| 103 | // memory_pool* old_to_space = to_space;
|
|---|
| 104 | //
|
|---|
| 105 | // from_space = reinterpret_cast<memory_pool*>(pal_allocPool(POOL_SIZE_BYTES, 1));
|
|---|
| 106 | // to_space = reinterpret_cast<memory_pool*>(pal_allocPool(POOL_SIZE_BYTES, 1));
|
|---|
| 107 | //
|
|---|
| 108 | // new (from_space) memory_pool(POOL_SIZE_BYTES, old_from_space, to_space, from_code);
|
|---|
| 109 | // new (to_space) memory_pool(POOL_SIZE_BYTES, old_to_space, from_space, (~from_code) & 0x01);
|
|---|
| 110 | //
|
|---|
| 111 | // total_space += from_space->size();
|
|---|
| 112 | //
|
|---|
| 113 | // pools_table.push_back(from_space);
|
|---|
| 114 | // pools_table.push_back(to_space);
|
|---|
| 115 | // }
|
|---|
| 116 | //
|
|---|
| 117 | // void state::collect()
|
|---|
| 118 | // {
|
|---|
| 119 | // DEBUG("collecting");
|
|---|
| 120 | // DEBUG("previous usage " << used_space << " / " << total_space);
|
|---|
| 121 | //
|
|---|
| 122 | // std::vector<void**> worklist;
|
|---|
| 123 | // sweep_roots(worklist);
|
|---|
| 124 | //
|
|---|
| 125 | // while(!worklist.empty())
|
|---|
| 126 | // {
|
|---|
| 127 | // void** ref = worklist.back();
|
|---|
| 128 | // worklist.pop_back();
|
|---|
| 129 | // process_reference(ref, worklist);
|
|---|
| 130 | // }
|
|---|
| 131 | //
|
|---|
| 132 | // check(roots_match());
|
|---|
| 133 | // check(no_from_space_ref());
|
|---|
| 134 | //
|
|---|
| 135 | // swap();
|
|---|
| 136 | //
|
|---|
| 137 | // calc_usage();
|
|---|
| 138 | //
|
|---|
| 139 | // if(needs_collect()) allocate_pool();
|
|---|
| 140 | //
|
|---|
| 141 | // DEBUG("done");
|
|---|
| 142 | // }
|
|---|
| 143 | //
|
|---|
| 144 | // void state::swap()
|
|---|
| 145 | // {
|
|---|
| 146 | // std::swap(from_space, to_space);
|
|---|
| 147 | //
|
|---|
| 148 | // memory_pool* pool = to_space;
|
|---|
| 149 | // while(pool)
|
|---|
| 150 | // {
|
|---|
| 151 | // pool->reset();
|
|---|
| 152 | // pool = pool->next();
|
|---|
| 153 | // }
|
|---|
| 154 | //
|
|---|
| 155 | // from_code = (~from_code) & 0x01;
|
|---|
| 156 | //
|
|---|
| 157 | // #if _DEBUG
|
|---|
| 158 | // {
|
|---|
| 159 | // memory_pool* pool = from_space;
|
|---|
| 160 | // while(pool)
|
|---|
| 161 | // {
|
|---|
| 162 | // check(pool->is_from_space());
|
|---|
| 163 | // pool = pool->next();
|
|---|
| 164 | // }
|
|---|
| 165 | //
|
|---|
| 166 | // pool = to_space;
|
|---|
| 167 | // while(pool)
|
|---|
| 168 | // {
|
|---|
| 169 | // check(!pool->is_from_space());
|
|---|
| 170 | // pool = pool->next();
|
|---|
| 171 | // }
|
|---|
| 172 | // }
|
|---|
| 173 | // #endif
|
|---|
| 174 | // }
|
|---|
| 175 | //
|
|---|
| 176 | // void state::sweep_roots(std::vector<void**>& worklist)
|
|---|
| 177 | // {
|
|---|
| 178 | // memory_pool* pool = from_space;
|
|---|
| 179 | // while(pool)
|
|---|
| 180 | // {
|
|---|
| 181 | // for(object_header* object : *pool)
|
|---|
| 182 | // {
|
|---|
| 183 | // if(!object->root_chain) continue;
|
|---|
| 184 | //
|
|---|
| 185 | // copy_object(object);
|
|---|
| 186 | //
|
|---|
| 187 | // scan_object(object->forward, worklist);
|
|---|
| 188 | // }
|
|---|
| 189 | //
|
|---|
| 190 | // pool = pool->next();
|
|---|
| 191 | // }
|
|---|
| 192 | // }
|
|---|
| 193 | //
|
|---|
| 194 | // void state::clear()
|
|---|
| 195 | // {
|
|---|
| 196 | // memory_pool* pool = from_space;
|
|---|
| 197 | // while(pool)
|
|---|
| 198 | // {
|
|---|
| 199 | // pool->reset();
|
|---|
| 200 | // pool = pool->next();
|
|---|
| 201 | // }
|
|---|
| 202 | //
|
|---|
| 203 | // pool = to_space;
|
|---|
| 204 | // while(pool)
|
|---|
| 205 | // {
|
|---|
| 206 | // pool->reset();
|
|---|
| 207 | // pool = pool->next();
|
|---|
| 208 | // }
|
|---|
| 209 | // }
|
|---|
| 210 | //
|
|---|
| 211 | // void state::calc_usage()
|
|---|
| 212 | // {
|
|---|
| 213 | // total_space = 0;
|
|---|
| 214 | // used_space = 0;
|
|---|
| 215 | //
|
|---|
| 216 | // memory_pool* pool = from_space;
|
|---|
| 217 | // while(pool)
|
|---|
| 218 | // {
|
|---|
| 219 | // size_t size = pool->size();
|
|---|
| 220 | // size_t used = size - pool->size_left();
|
|---|
| 221 | // check(used <= size);
|
|---|
| 222 | // total_space += size;
|
|---|
| 223 | // used_space += used;
|
|---|
| 224 | //
|
|---|
| 225 | // pool = pool->next();
|
|---|
| 226 | // }
|
|---|
| 227 | // }
|
|---|
| 228 | //
|
|---|
| 229 | // #if _DEBUG
|
|---|
| 230 | // bool state::roots_match()
|
|---|
| 231 | // {
|
|---|
| 232 | // memory_pool* pool = to_space;
|
|---|
| 233 | // while(pool)
|
|---|
| 234 | // {
|
|---|
| 235 | // size_t size = 0;
|
|---|
| 236 | // for(object_header* object : *pool)
|
|---|
| 237 | // {
|
|---|
| 238 | // size += object->size;
|
|---|
| 239 | //
|
|---|
| 240 | // gcpointer_base* ptr = object->root_chain;
|
|---|
| 241 | // while(ptr)
|
|---|
| 242 | // {
|
|---|
| 243 | // check(get_object_ptr(ptr->m_ptr) == object);
|
|---|
| 244 | // ptr = ptr->m_next;
|
|---|
| 245 | // }
|
|---|
| 246 | // }
|
|---|
| 247 | //
|
|---|
| 248 | // check(size + pool->size_left() == pool->size());
|
|---|
| 249 | //
|
|---|
| 250 | // pool = pool->next();
|
|---|
| 251 | // }
|
|---|
| 252 | //
|
|---|
| 253 | // return true;
|
|---|
| 254 | // }
|
|---|
| 255 | //
|
|---|
| 256 | // bool state::no_from_space_ref()
|
|---|
| 257 | // {
|
|---|
| 258 | // memory_pool* pool = to_space;
|
|---|
| 259 | // while(pool)
|
|---|
| 260 | // {
|
|---|
| 261 | // void** potential_ref = (void**)pool->m_start;
|
|---|
| 262 | // while(potential_ref < (void**)pool->m_free)
|
|---|
| 263 | // {
|
|---|
| 264 | // check(!is_in_heap(*potential_ref));
|
|---|
| 265 | // potential_ref++;
|
|---|
| 266 | // }
|
|---|
| 267 | //
|
|---|
| 268 | // pool = pool->next();
|
|---|
| 269 | // }
|
|---|
| 270 | //
|
|---|
| 271 | // return true;
|
|---|
| 272 | // }
|
|---|
| 273 | // #endif
|
|---|