Ignore:
Timestamp:
Jan 19, 2016, 4:07:06 PM (9 years ago)
Author:
Thierry Delisle <tdelisle@…>
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:
08a40fd
Parents:
a2b2761
Message:

gcpointers.c compiles

Location:
src/examples/gc_no_raii/src/internal
Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • src/examples/gc_no_raii/src/internal/collector.h

    ra2b2761 r6be0cf9  
    2020}
    2121
    22 inline struct gc_object_header* gc_get_object_ptr(void* ptr)
     22inline gc_object_header* gc_get_object_ptr(void* ptr)
    2323{
    2424        void* clean = gc_get_aligned_ptr(ptr);
    25         return ((struct gc_object_header*)clean) - 1;
     25        return ((gc_object_header*)clean) - 1;
    2626}
    2727
    28 inline struct gc_memory_pool* pool_of(void* address)
     28inline gc_memory_pool* pool_of(void* address)
    2929{
    3030        return (struct gc_memory_pool*)(((intptr_t)address) & POOL_PTR_MASK);
  • src/examples/gc_no_raii/src/internal/globals.h

    ra2b2761 r6be0cf9  
    11#pragma once
    22
    3 #include <stddef.h>
    4 #include <stdint.h>
     3// #include <stddef.h>
     4// #include <stdint.h>
     5//
     6// static const size_t POOL_SIZE_EXP = 24;
     7// static const size_t POOL_SIZE_BYTES = 0x1 << POOL_SIZE_EXP;
     8// static const size_t POOL_PTR_MASK = ~(POOL_SIZE_BYTES - 1);
     9//
     10// static const size_t CARDS_SIZE_EXP = 12;
     11// static const size_t CARDS_SIZE_BYTES = 0x1 << CARDS_SIZE_EXP;
     12// static const size_t CARDS_OFFSET_MASK = (~(CARDS_SIZE_BYTES - 1)) & (POOL_SIZE_BYTES - 1);
     13// static const size_t CARDS_COUNT = POOL_SIZE_BYTES / CARDS_SIZE_BYTES;
     14//
     15// static const size_t OBJECT_ALLIGNMENT = sizeof(size_t);
     16// static const size_t OBJECT_PTR_MASK = ~(OBJECT_ALLIGNMENT - 1);
    517
    6 static const size_t POOL_SIZE_EXP = 24;
    7 static const size_t POOL_SIZE_BYTES = 0x1 << POOL_SIZE_EXP;
    8 static const size_t POOL_PTR_MASK = ~(POOL_SIZE_BYTES - 1);
    918
    10 static const size_t CARDS_SIZE_EXP = 12;
    11 static const size_t CARDS_SIZE_BYTES = 0x1 << CARDS_SIZE_EXP;
    12 static const size_t CARDS_OFFSET_MASK = (~(CARDS_SIZE_BYTES - 1)) & (POOL_SIZE_BYTES - 1);
    13 static const size_t CARDS_COUNT = POOL_SIZE_BYTES / CARDS_SIZE_BYTES;
     19#define POOL_SIZE_EXP 24
     20#define POOL_SIZE_BYTES 0x1 << POOL_SIZE_EXP
     21#define POOL_PTR_MASK ~(POOL_SIZE_BYTES - 1)
    1422
    15 static const size_t OBJECT_ALLIGNMENT = sizeof(size_t);
    16 static const size_t OBJECT_PTR_MASK = ~(OBJECT_ALLIGNMENT - 1);
     23#define CARDS_SIZE_EXP 12
     24#define CARDS_SIZE_BYTES 0x1 << CARDS_SIZE_EXP
     25#define CARDS_OFFSET_MASK (~(CARDS_SIZE_BYTES - 1)) & (POOL_SIZE_BYTES - 1)
     26#define CARDS_COUNT POOL_SIZE_BYTES / CARDS_SIZE_BYTES
     27
     28#define OBJECT_ALLIGNMENT sizeof(size_t)
     29#define OBJECT_PTR_MASK ~(OBJECT_ALLIGNMENT - 1)
  • src/examples/gc_no_raii/src/internal/object_header.h

    ra2b2761 r6be0cf9  
    66#include "tools.h"
    77
    8 static const void* CANARY_VALUE = (void*)0xCAFEBABACAFEBABA;
     8#if DEBUG
     9        static const void* CANARY_VALUE = (void*)0xCAFEBABACAFEBABA;
     10#endif
    911
    1012struct gcpointer_t;
     13struct gc_object_header;
    1114
    1215struct gc_object_header
    1316{
    14         #if _DEBUG
     17        #if DEBUG
    1518                void* canary_start;
    1619        #endif
    1720
    18         size_t                          size;
    19         gcpointer_t*                    root_chain;
    20         gcpointer_t*                    type_chain;
    21         struct gc_object_header*        forward;
    22         bool                                    is_forwarded;
     21        size_t          size;
     22        gcpointer_t*    root_chain;
     23        gcpointer_t*    type_chain;
     24        gc_object_header*       forward;
     25        bool                    is_forwarded;
    2326
    24         #if _DEBUG
     27        #if DEBUG
    2528                void* canary_end;
    2629        #endif
    2730};
     31
     32gc_object_header* gc_object_header_placement_ctor(void* address, size_t size);
  • src/examples/gc_no_raii/src/internal/state.c

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

    ra2b2761 r6be0cf9  
    2020};
    2121
    22 void gc_state_ctor(gc_state* state);
     22void gc_state_ctor(struct gc_state* state);
    2323
    24 inline gc_state* get_state()
     24static inline gc_state* gc_get_state()
    2525{
    2626        static gc_state s;
    27         if(!s.is_initialized) gc_state_ctor(s);
     27        if(!s.is_initialized) gc_state_ctor(&s);
    2828        return &s;
    2929}
    3030
    31 inline bool needs_collect(gc_state* state)
     31inline bool gc_needs_collect(gc_state* state)
    3232{
    3333        return state->used_space * 2 > state->total_space;
Note: See TracChangeset for help on using the changeset viewer.