source: src/examples/gc_no_raii/src/internal/state.c @ d67a9a1

aaron-thesisarm-ehcleanup-dtorsctordeferred_resndemanglergc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerresolv-newwith_gc
Last change on this file since d67a9a1 was d67a9a1, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

pool alloc functional

  • Property mode set to 100644
File size: 5.6 KB
Line 
1#include "state.h"
2
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// #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
27void 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}
40
41// bool state::is_in_heap(void* address) const
42// {
43//      memory_pool* target_pool = gc_pool_of(address);
44//
45//      auto first = pools_table.cbegin();
46//      auto last = pools_table.cend();
47//      auto result = std::find(first, last, target_pool);
48//      return result != last && (*result)->is_from_space();
49// }
50
51// bool state::is_in_to_space(void* address) const
52// {
53//      const memory_pool* target_pool = pool_of(address);
54//
55//      auto first = pools_table.cbegin();
56//      auto last = pools_table.cend();
57//      auto result = std::find(first, last, target_pool);
58//      return result != last && !(*result)->is_from_space();
59// }
60//
61gc_object_header* gc_get_object_for_ref(gc_state* state, void* member)
62{
63        intptr_t target = ((intptr_t)member);
64        if(!gc_is_in_heap(state, member)) return NULL;
65
66        gc_memory_pool* pool = gc_pool_of(member);
67        gc_pool_object_iterator it = gc_pool_iterator_for(pool, member);
68        gc_pool_object_iterator end = gc_pool_end(pool);
69
70        while(it != end)
71        {
72                gc_object_header* object = *it;
73                {
74                        intptr_t start = ((intptr_t)object);
75                        intptr_t end = ((intptr_t)start + object->size);
76                        if(start < target && end > target)
77                        {
78                                return object;
79                        }
80                }
81                ++it;
82        }
83
84        checkf(false, "is_in_heap() and iterator_for() return inconsistent data");
85        abort();
86        return NULL;
87}
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// }
147//
148// void state::swap()
149// {
150//      std::swap(from_space, to_space);
151//
152//      memory_pool* pool = to_space;
153//      while(pool)
154//      {
155//              pool->reset();
156//              pool = pool->next();
157//      }
158//
159//      from_code = (~from_code) & 0x01;
160//
161//      #if _DEBUG
162//              {
163//                      memory_pool* pool = from_space;
164//                      while(pool)
165//                      {
166//                              check(pool->is_from_space());
167//                              pool = pool->next();
168//                      }
169//
170//                      pool = to_space;
171//                      while(pool)
172//                      {
173//                              check(!pool->is_from_space());
174//                              pool = pool->next();
175//                      }
176//              }
177//      #endif
178// }
179//
180// void state::sweep_roots(std::vector<void**>& worklist)
181// {
182//      memory_pool* pool = from_space;
183//      while(pool)
184//      {
185//              for(object_header* object : *pool)
186//              {
187//                      if(!object->root_chain) continue;
188//
189//                      copy_object(object);
190//
191//                      scan_object(object->forward, worklist);
192//              }
193//
194//              pool = pool->next();
195//      }
196// }
197//
198// void state::clear()
199// {
200//      memory_pool* pool = from_space;
201//      while(pool)
202//      {
203//              pool->reset();
204//              pool = pool->next();
205//      }
206//
207//      pool = to_space;
208//      while(pool)
209//      {
210//              pool->reset();
211//              pool = pool->next();
212//      }
213// }
214//
215// void state::calc_usage()
216// {
217//      total_space = 0;
218//      used_space = 0;
219//
220//      memory_pool* pool = from_space;
221//      while(pool)
222//      {
223//              size_t size = pool->size();
224//              size_t used = size - pool->size_left();
225//              check(used <= size);
226//              total_space += size;
227//              used_space += used;
228//
229//              pool = pool->next();
230//      }
231// }
232//
233// #if _DEBUG
234//      bool state::roots_match()
235//      {
236//              memory_pool* pool = to_space;
237//              while(pool)
238//              {
239//                      size_t size = 0;
240//                      for(object_header* object : *pool)
241//                      {
242//                              size += object->size;
243//
244//                              gcpointer_base* ptr = object->root_chain;
245//                              while(ptr)
246//                              {
247//                                      check(get_object_ptr(ptr->m_ptr) == object);
248//                                      ptr = ptr->m_next;
249//                              }
250//                      }
251//
252//                      check(size + pool->size_left() == pool->size());
253//
254//                      pool = pool->next();
255//              }
256//
257//              return true;
258//      }
259//
260//      bool state::no_from_space_ref()
261//      {
262//              memory_pool* pool = to_space;
263//              while(pool)
264//              {
265//                      void** potential_ref = (void**)pool->m_start;
266//                      while(potential_ref < (void**)pool->m_free)
267//                      {
268//                              check(!is_in_heap(*potential_ref));
269//                              potential_ref++;
270//                      }
271//
272//                      pool = pool->next();
273//              }
274//
275//              return true;
276//      }
277// #endif
Note: See TracBrowser for help on using the repository browser.