Changeset df4aea7 for src


Ignore:
Timestamp:
Apr 21, 2016, 10:27:55 AM (8 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:
8c8b614
Parents:
3365b37
Message:

all implemented files compile

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  
    22#include <stdint.h>
    33
    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"
    405
    416typedef vector(intptr_t*, heap_allocator(intptr_t*)) worklist_t;
    427
    43 inline void test()
     8void test()
    449{
    4510        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        }
    4816}
  • src/examples/gc_no_raii/src/internal/collector.c

    r3365b37 rdf4aea7  
    6767        check(((intptr_t)data) + target_size <= ((intptr_t)block) + actual_size);
    6868
    69         gc_object_header* obj = gc_object_header_placement_ctor(header, actual_size);
     69        gc_object_header* obj = placement_ctor(header, actual_size);
    7070
    7171        (void)obj; //remove unsused warning since this is for debug
     
    119119        memcpy(new_block, ptr, ptr->size);
    120120
    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);
    122122
    123123        ptr->forward = fwd_ptr;
  • src/examples/gc_no_raii/src/internal/memory_pool.h

    r3365b37 rdf4aea7  
    5353bool ?!=?(const gc_pool_object_iterator lhs, const gc_pool_object_iterator rhs);
    5454
     55gc_pool_object_iterator begin(gc_memory_pool* const);
     56gc_pool_object_iterator end(gc_memory_pool* const);
     57
    5558gc_pool_object_iterator* ++?(gc_pool_object_iterator* it);
    5659
  • src/examples/gc_no_raii/src/internal/object_header.h

    r3365b37 rdf4aea7  
    3030};
    3131
    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);
     32gc_object_header* placement_ctor(void* address, size_t size);
     33gc_object_header* placement_copy_ctor(void* address, const gc_object_header* const other);
  • src/examples/gc_no_raii/src/internal/state.c

    r3365b37 rdf4aea7  
    11#include "state.h"
    22
    3 #include <stdlib.h>
     3#include <stdlib>
    44
    55//general purpouse includes
     
    2828{
    2929        static gc_state s;
    30         if(!s.is_initialized) gc_state_ctor(&s);
     30        if(!s.is_initialized) ctor(&s);
    3131        return &s;
    3232}
    3333
    34 void gc_state_ctor(gc_state *const this)
     34void ctor(gc_state *const this)
    3535{
    3636        this->from_code = 0;
     
    3939        this->total_space = 0;
    4040        this->used_space = 0;
    41         // state->pools_table();
     41        ctor(&this->pools_table);
    4242
    4343        gc_allocate_pool(this);
     
    4646}
    4747
    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)
     48bool 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
     58bool 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
     68gc_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
     96void* gc_try_allocate(gc_state *const this, size_t size)
    9797{
    9898        gc_memory_pool* pool = this->from_space;
     
    122122        this->total_space += gc_pool_size_used(this->from_space);
    123123
    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
     128void gc_state_collect(gc_state* const this)
    129129{
    130130        // DEBUG("collecting");
     
    132132
    133133        worklist_t worklist;
    134         // vector_ctor(&worklist);
     134        ctor(&worklist);
    135135        gc_state_sweep_roots(this, &worklist);
    136136
    137137        while(!empty(&worklist))
    138138        {
    139                 void** ref = back(&worklist);
     139                intptr_t* ref = back(&worklist);
    140140                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);
    152152
    153153        // 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
     157void 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
     189void 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
     210void 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
     227void 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  
    55
    66#include "tools.h"
     7#include "vector.h"
     8
     9typedef vector(struct gc_memory_pool*, heap_allocator(struct gc_memory_pool*)) pools_table_t;
    710
    811struct gc_state
     
    1619        size_t used_space;
    1720
    18         // const struct memory_pool*    pools_table;
    19         // size_t                               pools_table_count;
     21        pools_table_t   pools_table;
     22        size_t          pools_table_count;
    2023};
    2124
    22 void gc_state_ctor(struct gc_state* state);
     25void ctor(struct gc_state* state);
    2326
    2427gc_state* gc_get_state();
     
    3942bool gc_is_in_to_space(const gc_state* state, void* address);
    4043
    41 inline uint8_t gc_from_space_code(const gc_state* state)
     44inline uint8_t gc_from_space_code(const gc_state *const this)
    4245{
    43         return state->from_code;
     46        return this->from_code;
    4447}
    4548
  • src/examples/gc_no_raii/src/tools.h

    r3365b37 rdf4aea7  
    33#include "tools/checks.h"
    44#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
     14trait has_equal(otype T)
     15{
     16        signed int ?==?(T a, T b);
     17};
     18
     19trait 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
     28forall(otype T | has_equal(T), otype InputIterator | InputIterator_t(T, InputIterator))
     29inline 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  
    33#if _DEBUG
    44
    5 #include "fstream.h"
    6 
    7 extern ofstream *sout;
     5#include <fstream>
    86
    97#define DEBUG_OUT(x) sout | x | endl;
  • src/examples/gc_no_raii/src/vector.h

    r3365b37 rdf4aea7  
     1#pragma once
     2
    13#include <assert.h>
    24#include <stdbool.h>
     
    810//------------------------------------------------------------------------------
    911//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*);
     12trait 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);
    1518};
    1619
     
    2528//Initialization
    2629forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
    27 void vector_ctor(vector(T, allocator_t) *const this);
     30void ctor(vector(T, allocator_t) *const this);
    2831
    2932forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
     
    7275
    7376forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
    74 inline T back(vector(T, allocator_t) *const this, size_t index)
     77inline T back(vector(T, allocator_t) *const this)
    7578{
    7679        return data(&this->storage)[this->size - 1];
     
    8790forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
    8891void clear(vector(T, allocator_t) *const this);
     92
     93//------------------------------------------------------------------------------
     94//Iterators
     95forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
     96inline T* begin(vector(T, allocator_t) *const this)
     97{
     98        return data(&this->storage);
     99}
     100
     101forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
     102inline const T* cbegin(const vector(T, allocator_t) *const this)
     103{
     104        return data(&this->storage);
     105}
     106
     107forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
     108inline T* end(vector(T, allocator_t) *const this)
     109{
     110        return data(&this->storage) + this->size;
     111}
     112
     113forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
     114inline const T* cend(const vector(T, allocator_t) *const this)
     115{
     116        return data(&this->storage) + this->size;
     117}
    89118
    90119//------------------------------------------------------------------------------
Note: See TracChangeset for help on using the changeset viewer.