Changeset eb38dd5


Ignore:
Timestamp:
Feb 25, 2016, 10:04:11 AM (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:
8bb59af
Parents:
61a4875
Message:

vectors have almost no bugs left

Location:
src/examples/gc_no_raii/src
Files:
4 edited

Legend:

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

    r61a4875 reb38dd5  
    11#include "gcpointers.h"
    22
    3 #include "gc.h"
     3// #include "gc.h"
    44#include "internal/collector.h"
    5 //#include "internal/object_header.h"
     5// #include "internal/object_header.h"
    66// #include "internal/state.h"
    7 /*
    8 void register_ptr(gcpointer_t* this)
    9 {
    10         // if(gcpointer_null(this)) return;
    11         //
    12         // _Bool managed = gc_is_managed(this);
    13         //
    14         // if(managed)
    15         // {
    16         //      gc_object_header* obj = gc_get_object_for_ref(gc_get_state(), (void*)this);
    17         //      check(obj);
    18         //      check(gc_obj_is_valide(obj));
    19         //      check(gc_is_managed(this) == gc_is_managed(obj->type_chain) || obj->type_chain == NULL);
    20         //      this->next = obj->type_chain;
    21         //      obj->type_chain = this;
    22         //      check(obj->is_valide());
    23         // }
    24         // else
    25         // {
    26         //      gc_object_header* obj = gc_get_object_ptr((void*)this->ptr);
    27         //      check(obj);
    28         //      check(gc_obj_is_valide(obj));
    29         //      check(gc_is_managed(this) == gc_is_managed(obj->root_chain) || obj->root_chain == NULL);
    30         //      this->next = obj->root_chain;
    31         //      obj->root_chain = this;
    32         //      check(gc_obj_is_valide(obj));
    33         // }
    34 }
    357
    36 void unregister_ptr(gcpointer_t* this)
    37 {
    38         // if(gcpointer_null(this)) return;
    39         //
    40         // gcpointer_t** prev_next_ptr = gc_find_previous_ref(this);
    41         // check((*prev_next_ptr) == this);
    42         //
    43         // (*prev_next_ptr) = this->next;
    44 }
    45 
    46 void gcpointer_ctor(gcpointer_t* this)
    47 {
    48         this->ptr = (intptr_t)NULL;
    49         this->next = NULL;
    50 }
    51 
    52 void gcpointer_ctor(gcpointer_t* this, void* address)
    53 {
    54         this->ptr = (intptr_t)address;
    55         this->next = NULL;
    56 
    57         register_ptr(this);
    58 }
    59 
    60 void gcpointer_ctor(gcpointer_t* this, gcpointer_t* other)
    61 {
    62         this->ptr = other->ptr;
    63         this->next = NULL;
    64 
    65         register_ptr(this);
    66 }
    67 
    68 void gcpointer_dtor(gcpointer_t* this)
    69 {
    70         unregister_ptr(this);
    71 }
    72 
    73 gcpointer_t* gcpointer_assign(gcpointer_t* this, gcpointer_t* rhs)
    74 {
    75         if(this != rhs)
    76         {
    77                 unregister_ptr(this);
    78 
    79                 this->ptr = rhs->ptr;
    80 
    81                 register_ptr(this);
    82         }
    83 
    84         return this;
    85 }
    86 
    87 //Logical operators
    88 int gcpointer_equal(gcpointer_t* this, gcpointer_t* rhs)
    89 {
    90         return this->ptr == rhs->ptr;
    91 }
    92 
    93 int gcpointer_not_equal(gcpointer_t* this, gcpointer_t* rhs)
    94 {
    95         return this->ptr != rhs->ptr;
    96 }
    97 
    98 int gcpointer_null(gcpointer_t* this)
    99 {
    100         return this->ptr == (intptr_t)NULL;
    101 }*/
     8// void register_ptr(gcpointer_t* this)
     9// {
     10//      if(gcpointer_null(this)) return;
     11//
     12//      _Bool managed = gc_is_managed(this);
     13//
     14//      if(managed)
     15//      {
     16//              gc_object_header* obj = gc_get_object_for_ref(gc_get_state(), (void*)this);
     17//              check(obj);
     18//              check(gc_obj_is_valide(obj));
     19//              check(gc_is_managed(this) == gc_is_managed(obj->type_chain) || obj->type_chain == NULL);
     20//              this->next = obj->type_chain;
     21//              obj->type_chain = this;
     22//              check(obj->is_valide());
     23//      }
     24//      else
     25//      {
     26//              gc_object_header* obj = gc_get_object_ptr((void*)this->ptr);
     27//              check(obj);
     28//              check(gc_obj_is_valide(obj));
     29//              check(gc_is_managed(this) == gc_is_managed(obj->root_chain) || obj->root_chain == NULL);
     30//              this->next = obj->root_chain;
     31//              obj->root_chain = this;
     32//              check(gc_obj_is_valide(obj));
     33//      }
     34// }
     35//
     36// void unregister_ptr(gcpointer_t* this)
     37// {
     38//      if(gcpointer_null(this)) return;
     39//
     40//      gcpointer_t** prev_next_ptr = gc_find_previous_ref(this);
     41//      check((*prev_next_ptr) == this);
     42//
     43//      (*prev_next_ptr) = this->next;
     44// }
     45//
     46// void gcpointer_ctor(gcpointer_t* this)
     47// {
     48//      this->ptr = (intptr_t)NULL;
     49//      this->next = NULL;
     50// }
     51//
     52// void gcpointer_ctor(gcpointer_t* this, void* address)
     53// {
     54//      this->ptr = (intptr_t)address;
     55//      this->next = NULL;
     56//
     57//      register_ptr(this);
     58// }
     59//
     60// void gcpointer_ctor(gcpointer_t* this, gcpointer_t* other)
     61// {
     62//      this->ptr = other->ptr;
     63//      this->next = NULL;
     64//
     65//      register_ptr(this);
     66// }
     67//
     68// void gcpointer_dtor(gcpointer_t* this)
     69// {
     70//      unregister_ptr(this);
     71// }
     72//
     73// gcpointer_t* gcpointer_assign(gcpointer_t* this, gcpointer_t* rhs)
     74// {
     75//      if(this != rhs)
     76//      {
     77//              unregister_ptr(this);
     78//
     79//              this->ptr = rhs->ptr;
     80//
     81//              register_ptr(this);
     82//      }
     83//
     84//      return this;
     85// }
     86//
     87// //Logical operators
     88// int gcpointer_equal(gcpointer_t* this, gcpointer_t* rhs)
     89// {
     90//      return this->ptr == rhs->ptr;
     91// }
     92//
     93// int gcpointer_not_equal(gcpointer_t* this, gcpointer_t* rhs)
     94// {
     95//      return this->ptr != rhs->ptr;
     96// }
     97//
     98// int gcpointer_null(gcpointer_t* this)
     99// {
     100//      return this->ptr == (intptr_t)NULL;
     101// }
  • src/examples/gc_no_raii/src/internal/collector.h

    r61a4875 reb38dd5  
    44#include <stdint.h>
    55
    6 #include "tools.h"
     6// #include "tools.h"
     7//
     8// #include "gcpointers.h"
     9// #include "internal/gc_tools.h"
     10// #include "internal/globals.h"
     11// #include "internal/object_header.h"
     12// #include "internal/state.h"
     13#include "tools/worklist.h"
    714
    8 //#include "gcpointers.h"
    9 //#include "internal/gc_tools.h"
    10 //#include "internal/globals.h"
    11 #include "internal/object_header.h"
    12 //#include "internal/state.h"
    13 #include "tools/worklist.h"
    14 /*
    15 inline bool gc_is_managed(void* address)
    16 {
    17         return gc_is_in_heap(gc_get_state(), address);
    18 }
    19 
    20 inline gc_object_header* gc_get_object_ptr(void* ptr)
    21 {
    22         void* clean = gc_get_aligned_ptr(ptr);
    23         return ((gc_object_header*)clean) - 1;
    24 }
    25 
    26 inline gc_memory_pool* gc_pool_of(void* address)
    27 {
    28         //return (struct gc_memory_pool*)(((intptr_t)address) & POOL_PTR_MASK);
    29 }
    30 
    31 inline void gc_conditional_collect()
    32 {
    33         if(gc_needs_collect(gc_get_state()))
    34         {
    35                 gc_collect(gc_get_state());
    36         }
    37 }
    38 
    39 gcpointer_t** gc_find_previous_ref(gcpointer_t* target);
    40 
    41 void* gc_allocate(size_t size);
    42 
    43 void gc_process_reference(void** ref, worklist_t* worklist);
    44 
    45 struct gc_object_header* gc_copy_object(struct gc_object_header* ptr);
    46 
    47 void gc_scan_object(struct gc_object_header* object, worklist_t* worklist);
    48 */
     15// inline bool gc_is_managed(void* address)
     16// {
     17//      return gc_is_in_heap(gc_get_state(), address);
     18// }
     19//
     20// inline gc_object_header* gc_get_object_ptr(void* ptr)
     21// {
     22//      void* clean = gc_get_aligned_ptr(ptr);
     23//      return ((gc_object_header*)clean) - 1;
     24// }
     25//
     26// inline gc_memory_pool* gc_pool_of(void* address)
     27// {
     28//      return (struct gc_memory_pool*)(((intptr_t)address) & POOL_PTR_MASK);
     29// }
     30//
     31// inline void gc_conditional_collect()
     32// {
     33//      if(gc_needs_collect(gc_get_state()))
     34//      {
     35//              gc_collect(gc_get_state());
     36//      }
     37// }
     38//
     39// gcpointer_t** gc_find_previous_ref(gcpointer_t* target);
     40//
     41// void* gc_allocate(size_t size);
     42//
     43// void gc_process_reference(void** ref, worklist_t* worklist);
     44//
     45// struct gc_object_header* gc_copy_object(struct gc_object_header* ptr);
     46//
     47// void gc_scan_object(struct gc_object_header* object, worklist_t* worklist);
  • src/examples/gc_no_raii/src/tools/worklist.h

    r61a4875 reb38dd5  
    1717*/
    1818
    19 //typedef vector(intptr_t*) worklist_t;
     19typedef vector(intptr_t*, heap_allocator(intptr_t*)) worklist_t;
  • src/examples/gc_no_raii/src/vector.h

    r61a4875 reb38dd5  
     1#include <assert.h>
     2#include <stdbool.h>
     3#include <stddef.h>
     4#include <stdlib.h>
    15
     6#define DESTROY(x)
    27
    3 context allocator(type T, type all) {
    4         T realloc(all*, size_t);
     8//------------------------------------------------------------------------------
     9//Declaration
     10context allocator_c(type T, type allocator_t) {
     11        void reallocate(allocator_t*, size_t);
     12        T* data(allocator_t*);
    513};
    614
    7 forall(type T, type all | allocator(T, all))
     15forall(type T, type allocator_t | allocator_c(T, allocator_t))
    816struct vector
    917{
    10         T *m_data;
     18        allocator_t storage;
     19        size_t size;
    1120};
    1221
    13 // forall(type T, type all)
    14 // void push_back(vector(T, all)* this, T value)
    15 // {
    16 //      (*(this->m_data)) = value;
    17 // }
     22//------------------------------------------------------------------------------
     23//Capacity
     24forall(type T, type allocator_t | allocator_c(T, allocator_t))
     25bool empty(vector(T, allocator_t)* this)
     26{
     27        return this->size == 0;
     28}
     29
     30forall(type T, type allocator_t | allocator_c(T, allocator_t))
     31bool size(vector(T, allocator_t)* this)
     32{
     33        return this->size;
     34}
     35
     36forall(type T, type allocator_t | allocator_c(T, allocator_t))
     37void reserve(vector(T, allocator_t)* this, size_t size)
     38{
     39        reallocate(&this->storage, this->size+1);
     40}
     41
     42//------------------------------------------------------------------------------
     43//Element access
     44forall(type T, type allocator_t | allocator_c(T, allocator_t))
     45T at(vector(T, allocator_t)* this, size_t index)
     46{
     47        //assert(index < this->size);
     48        return data(&this->storage)[index];
     49}
     50
     51forall(type T, type allocator_t | allocator_c(T, allocator_t))
     52T ?[?](vector(T, allocator_t)* this, size_t index)
     53{
     54        return data(&this->storage)[index];
     55}
     56
     57forall(type T, type allocator_t | allocator_c(T, allocator_t))
     58T front(vector(T, allocator_t)* this)
     59{
     60        return data(&this->storage)[0];
     61}
     62
     63forall(type T, type allocator_t | allocator_c(T, allocator_t))
     64T back(vector(T, allocator_t)* this, size_t index)
     65{
     66        return data(&this->storage)[this->size - 1];
     67}
     68
     69//------------------------------------------------------------------------------
     70//Modifiers
     71forall(type T, type allocator_t | allocator_c(T, allocator_t))
     72void push_back(vector(T, allocator_t)* this, T value)
     73{
     74        reallocate(&this->storage, this->size+1);
     75        data(&this->storage)[this->size] = value;
     76        this->size++;
     77}
     78
     79forall(type T, type allocator_t | allocator_c(T, allocator_t))
     80void pop_back(vector(T, allocator_t)* this)
     81{
     82        this->size--;
     83        DESTROY(data(&this->storage)[this->size]);
     84}
     85
     86forall(type T, type allocator_t | allocator_c(T, allocator_t))
     87void clear(vector(T, allocator_t)* this)
     88{
     89        for(size_t i = 0; i < this->size; i++)
     90        {
     91                DESTROY(data(&this->storage)[this->size]);
     92        }
     93        this->size = 0;
     94}
     95
     96//------------------------------------------------------------------------------
     97//Allocator
     98forall(type T)
     99struct heap_allocator
     100{
     101        T* storage;
     102        size_t capacity;
     103};
     104
     105forall(type T)
     106void reallocate(heap_allocator(T)* this, size_t size)
     107{
     108        static const size_t GROWTH_RATE = 2;
     109        if(size > this->capacity)
     110        {
     111                this->capacity = GROWTH_RATE * size;
     112                this->storage = (T*)realloc((void*)this->storage, this->capacity * sizeof(T));
     113        }
     114}
     115
     116forall(type T)
     117T* data(heap_allocator(T)* this)
     118{
     119        return this->storage;
     120}
Note: See TracChangeset for help on using the changeset viewer.