Changeset 29ad0ac


Ignore:
Timestamp:
Apr 15, 2016, 3:04:25 PM (6 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
aaron-thesis, arm-eh, cleanup-dtors, ctor, deferred_resn, demangler, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, resolv-new, with_gc
Children:
356bb95
Parents:
0f9e4403
Message:

some regression testing

Location:
src/examples/gc_no_raii
Files:
2 added
6 edited

Legend:

Unmodified
Added
Removed
  • src/examples/gc_no_raii/src/gcpointers.c

    r0f9e4403 r29ad0ac  
    44#include "internal/collector.h"
    55#include "internal/object_header.h"
    6 // #include "internal/state.h"
     6#include "internal/state.h"
    77
    88// void register_ptr(gcpointer_t* this)
     
    1212//      _Bool managed = gc_is_managed(this);
    1313//
     14//
     15//      gc_state* state = gc_get_state();
     16//      gc_object_header* obj = 0;
    1417//      if(managed)
    1518//      {
    16 //              gc_object_header* obj = gc_get_object_for_ref(gc_get_state(), (void*)this);
     19//              obj = gc_get_object_for_ref(state, (void*)this);
    1720//              check(obj);
    1821//              check(gc_obj_is_valide(obj));
     
    2427//      else
    2528//      {
    26 //              gc_object_header* obj = gc_get_object_ptr((void*)this->ptr);
     29//              obj = gc_get_object_ptr(state, (void*)this->ptr);
    2730//              check(obj);
    2831//              check(gc_obj_is_valide(obj));
  • src/examples/gc_no_raii/src/gctest.c

    r0f9e4403 r29ad0ac  
    44
    55int main() {
    6         ofstream *sout = ofstream_stdout();
    7         ifstream *sin = ifstream_stdin();
    86        sout | "Bonjour au monde!\n";
    97}
  • src/examples/gc_no_raii/src/internal/collector.c

    r0f9e4403 r29ad0ac  
    88#include <fstream>
    99
    10 #include "memory_pool.h"
     10// #include "state.h"
     11// #include "gcpointers.h"
     12// #include "memory_pool.h"
    1113
    12 void* gc_finish_alloc_block(void* block, size_t actual_size, size_t target_size);
    13 void gc_assign_reference(void** ref, gc_object_header* ptr);
     14// void* gc_finish_alloc_block(void* block, size_t actual_size, size_t target_size);
     15// void gc_assign_reference(void** ref, gc_object_header* ptr);
    1416
    15 gcpointer_t** gc_find_previous_ref(gcpointer_t* target)
    16 {
    17         if(!(target)) return NULL;
     17// gcpointer_t** gc_find_previous_ref(gcpointer_t* target)
     18// {
     19//      if(!(target)) return NULL;
     20//
     21//      bool managed = gc_is_managed(target);
     22//      gc_object_header* obj = gc_get_object_ptr((void*)target->ptr);
     23//
     24//      check(gc_is_valide(obj));
     25//
     26//      gcpointer_t** prev_next_ptr = managed ? &obj->type_chain : &obj->root_chain;
     27//      while((*prev_next_ptr) && (*prev_next_ptr) != target)
     28//      {
     29//              prev_next_ptr = &(*prev_next_ptr)->next;
     30//      }
     31//
     32//      return prev_next_ptr;
     33// }
    1834
    19         bool managed = gc_is_managed(target);
    20         gc_object_header* obj = gc_get_object_ptr((void*)target->ptr);
    21 
    22         check(gc_is_valide(obj));
    23 
    24         gcpointer_t** prev_next_ptr = managed ? &obj->type_chain : &obj->root_chain;
    25         while((*prev_next_ptr) && (*prev_next_ptr) != target)
    26         {
    27                 prev_next_ptr = &(*prev_next_ptr)->next;
    28         }
    29 
    30         return prev_next_ptr;
    31 }
    32 
    33 void* gc_allocate(size_t target_size)
    34 {
    35         size_t size = gc_compute_size(target_size + sizeof(gc_object_header));
    36 
    37         check(size < POOL_SIZE_BYTES);
    38 
    39         void* block = NULL;
    40         gc_state* gc = gc_get_state();
    41 
    42         if((intptr_t)(block = gc_try_allocate(gc, size))) return gc_finish_alloc_block(block, size, target_size);
    43 
    44         gc_collect(gc);
    45 
    46         if((intptr_t)(block = gc_try_allocate(gc, size))) return gc_finish_alloc_block(block, size, target_size);
    47 
    48         gc_allocate_pool(gc);
    49 
    50         if((intptr_t)(block = gc_try_allocate(gc, size))) return gc_finish_alloc_block(block, size, target_size);
    51 
    52         ofstream_stderr() | "ERROR: allocation in new pool failed" | endl;
    53         abort();
    54 
    55         return NULL;
    56 }
    57 
    58 void* gc_finish_alloc_block(void* block, size_t actual_size, size_t target_size)
    59 {
    60         void* data = (void*)(((intptr_t)block) + sizeof(gc_object_header));
    61         void* header = block;
    62 
    63         check(((intptr_t)data) > ((intptr_t)block));
    64         check(((intptr_t)data) >= ((intptr_t)header));
    65         check(is_aligned(data));
    66         check(((intptr_t)data) + target_size <= ((intptr_t)block) + actual_size);
    67 
    68         gc_object_header* obj = gc_object_header_placement_ctor(header, actual_size);
    69 
    70         // (void)obj; //remove unsused warning since this is for debug
    71         check(obj == get_object_ptr(data));
    72 
    73         gc_register_allocation(gc_get_state(), actual_size);
    74 
    75         return data;
    76 }
    77 
    78 void gc_process_reference(void** ref, worklist_t* worklist)
    79 {
    80         check(!gc_is_in_heap(gc_get_state(), ref));
    81 
    82         gc_object_header* ptr = gc_get_object_ptr(*ref);
    83         if(ptr)
    84         {
    85                 if(!ptr->is_forwarded)
    86                 {
    87                         gc_copy_object(ptr);
    88 
    89                         gc_scan_object(ptr->forward, worklist);
    90 
    91                         gc_assign_reference(ref, ptr->forward);
    92                 }
    93                 else
    94                 {
    95                         //duplication to help debug
    96                         gc_assign_reference(ref, ptr->forward);
    97                 }
    98         }
    99 }
    100 
    101 void gc_assign_reference(void** ref, gc_object_header* ptr)
    102 {
    103         void* address = (void*)(((intptr_t)ptr) + sizeof(gc_object_header));
    104 
    105         gc_write_aligned_ptr(ref, address);
    106 }
    107 
    108 gc_object_header* gc_copy_object(gc_object_header* ptr)
    109 {
    110         check(!ptr->forward);
    111         check(!ptr->is_forwarded);
    112         check(gc_is_from_space(gc_pool_of(ptr)));
    113 
    114         gc_memory_pool* pool = gc_pool_of(ptr)->mirror;
    115 
    116         void* new_block = gc_pool_allocate(pool, ptr->size, true);
    117 
    118         memcpy(new_block, ptr, ptr->size);
    119 
    120         gc_object_header* fwd_ptr = gc_object_header_placement_copy_ctor(new_block, ptr);
    121 
    122         ptr->forward = fwd_ptr;
    123         ptr->is_forwarded = true;
    124 
    125         return fwd_ptr;
    126 }
    127 
    128 void gc_scan_object(gc_object_header* object, worklist_t* worklist)
    129 {
    130         gcpointer_t* field = object->type_chain;
    131         while(field)
    132         {
    133                 check(((intptr_t)field) > ((intptr_t)object));
    134                 check(((intptr_t)field) < ((intptr_t)((intptr_t)object) + object->size));
    135 
    136                 check(gc_is_in_to_space(gc_get_state(), &type->ptr));
    137 
    138                 push_back(worklist, &field->ptr);
    139 
    140                 field = field->next;
    141         }
    142 }
     35// void* gc_allocate(size_t target_size)
     36// {
     37//      size_t size = gc_compute_size(target_size + sizeof(gc_object_header));
     38//
     39//      check(size < POOL_SIZE_BYTES);
     40//
     41//      void* block = NULL;
     42//      gc_state* gc = gc_get_state();
     43//
     44//      if((intptr_t)(block = gc_try_allocate(gc, size))) return gc_finish_alloc_block(block, size, target_size);
     45//
     46//      gc_collect(gc);
     47//
     48//      if((intptr_t)(block = gc_try_allocate(gc, size))) return gc_finish_alloc_block(block, size, target_size);
     49//
     50//      gc_allocate_pool(gc);
     51//
     52//      if((intptr_t)(block = gc_try_allocate(gc, size))) return gc_finish_alloc_block(block, size, target_size);
     53//
     54//      ofstream_stderr() | "ERROR: allocation in new pool failed" | endl;
     55//      abort();
     56//
     57//      return NULL;
     58// }
     59//
     60// void* gc_finish_alloc_block(void* block, size_t actual_size, size_t target_size)
     61// {
     62//      void* data = (void*)(((intptr_t)block) + sizeof(gc_object_header));
     63//      void* header = block;
     64//
     65//      check(((intptr_t)data) > ((intptr_t)block));
     66//      check(((intptr_t)data) >= ((intptr_t)header));
     67//      check(is_aligned(data));
     68//      check(((intptr_t)data) + target_size <= ((intptr_t)block) + actual_size);
     69//
     70//      gc_object_header* obj = gc_object_header_placement_ctor(header, actual_size);
     71//
     72//      // (void)obj; //remove unsused warning since this is for debug
     73//      check(obj == get_object_ptr(data));
     74//
     75//      gc_register_allocation(gc_get_state(), actual_size);
     76//
     77//      return data;
     78// }
     79//
     80// void gc_process_reference(void** ref, worklist_t* worklist)
     81// {
     82//      check(!gc_is_in_heap(gc_get_state(), ref));
     83//
     84//      gc_object_header* ptr = gc_get_object_ptr(*ref);
     85//      if(ptr)
     86//      {
     87//              if(!ptr->is_forwarded)
     88//              {
     89//                      gc_copy_object(ptr);
     90//
     91//                      gc_scan_object(ptr->forward, worklist);
     92//
     93//                      gc_assign_reference(ref, ptr->forward);
     94//              }
     95//              else
     96//              {
     97//                      //duplication to help debug
     98//                      gc_assign_reference(ref, ptr->forward);
     99//              }
     100//      }
     101// }
     102//
     103// void gc_assign_reference(void** ref, gc_object_header* ptr)
     104// {
     105//      void* address = (void*)(((intptr_t)ptr) + sizeof(gc_object_header));
     106//
     107//      gc_write_aligned_ptr(ref, address);
     108// }
     109//
     110// gc_object_header* gc_copy_object(gc_object_header* ptr)
     111// {
     112//      check(!ptr->forward);
     113//      check(!ptr->is_forwarded);
     114//      check(gc_is_from_space(gc_pool_of(ptr)));
     115//
     116//      gc_memory_pool* pool = gc_pool_of(ptr)->mirror;
     117//
     118//      void* new_block = gc_pool_allocate(pool, ptr->size, true);
     119//
     120//      memcpy(new_block, ptr, ptr->size);
     121//
     122//      gc_object_header* fwd_ptr = gc_object_header_placement_copy_ctor(new_block, ptr);
     123//
     124//      ptr->forward = fwd_ptr;
     125//      ptr->is_forwarded = true;
     126//
     127//      return fwd_ptr;
     128// }
     129//
     130// void gc_scan_object(gc_object_header* object, worklist_t* worklist)
     131// {
     132//      gcpointer_t* field = object->type_chain;
     133//      while(field)
     134//      {
     135//              check(((intptr_t)field) > ((intptr_t)object));
     136//              check(((intptr_t)field) < ((intptr_t)((intptr_t)object) + object->size));
     137//
     138//              check(gc_is_in_to_space(gc_get_state(), &type->ptr));
     139//
     140//              push_back(worklist, &field->ptr);
     141//
     142//              field = field->next;
     143//      }
     144// }
  • src/examples/gc_no_raii/src/internal/collector.h

    r0f9e4403 r29ad0ac  
    77//
    88// #include "gcpointers.h"
    9 // #include "internal/gc_tools.h"
     9#include "state.h"
     10#include "internal/gc_tools.h"
    1011// #include "internal/globals.h"
    1112// #include "internal/object_header.h"
     
    1314#include "tools/worklist.h"
    1415
    15 // inline bool gc_is_managed(void* address)
    16 // {
    17 //      return gc_is_in_heap(gc_get_state(), address);
    18 // }
    19 //
     16inline bool gc_is_managed(void* address)
     17{
     18        return gc_is_in_heap(gc_get_state(), address);
     19}
     20
    2021// inline gc_object_header* gc_get_object_ptr(void* ptr)
    2122// {
     
    2324//      return ((gc_object_header*)clean) - 1;
    2425// }
    25 //
     26
    2627// inline gc_memory_pool* gc_pool_of(void* address)
    2728// {
  • src/examples/gc_no_raii/src/internal/gc_tools.h

    r0f9e4403 r29ad0ac  
    1212}
    1313
    14 inline void* gc_get_aligned_ptr(void* address)
    15 {
    16         return (void*)(((intptr_t)address) & (OBJECT_PTR_MASK));
    17 }
    18 
    19 inline void* gc_write_aligned_ptr(void** reference, void* address)
    20 {
    21         size_t ref_last_bits = ((intptr_t)*reference) & (~OBJECT_PTR_MASK);
    22 
    23       size_t new_val = ((intptr_t)address) & OBJECT_PTR_MASK;
    24 
    25       (*reference) = (void*)(new_val | ref_last_bits);
    26 
    27         return *reference;
    28 }
    29 
    30 inline size_t gc_compute_size(size_t size)
    31 {
    32         size_t word_size = ((size - 1) / OBJECT_ALLIGNMENT) + 1;
    33         size_t ret = word_size * OBJECT_ALLIGNMENT;
    34 
    35         check(ret >= size);
    36         check((ret % OBJECT_ALLIGNMENT) == 0);
    37         check( ((size % OBJECT_ALLIGNMENT) != 0) || (ret == size) );
    38 
    39         return ret;
    40 }
     14// inline void* gc_get_aligned_ptr(void* address)
     15// {
     16//      return (void*)(((intptr_t)address) & (OBJECT_PTR_MASK));
     17// }
     18//
     19// inline void* gc_write_aligned_ptr(void** reference, void* address)
     20// {
     21//      size_t ref_last_bits = ((intptr_t)*reference) & (~OBJECT_PTR_MASK);
     22//
     23//       size_t new_val = ((intptr_t)address) & OBJECT_PTR_MASK;
     24//
     25//       (*reference) = (void*)(new_val | ref_last_bits);
     26//
     27//      return *reference;
     28// }
     29//
     30// inline size_t gc_compute_size(size_t size)
     31// {
     32//      size_t word_size = ((size - 1) / OBJECT_ALLIGNMENT) + 1;
     33//      size_t ret = word_size * OBJECT_ALLIGNMENT;
     34//
     35//      check(ret >= size);
     36//      check((ret % OBJECT_ALLIGNMENT) == 0);
     37//      check( ((size % OBJECT_ALLIGNMENT) != 0) || (ret == size) );
     38//
     39//      return ret;
     40// }
  • src/examples/gc_no_raii/src/internal/object_header.h

    r0f9e4403 r29ad0ac  
    77
    88#if DEBUG
    9         static const void* CANARY_VALUE = (void*)0xCAFEBABACAFEBABA;
     9        static const long unsigned int CANARY_VALUE = 0xCAFEBABACAFEBABA;
    1010#endif
    1111
Note: See TracChangeset for help on using the changeset viewer.