Ignore:
Timestamp:
Jan 20, 2016, 1:53:17 PM (7 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
aaron-thesis, arm-eh, 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, resolv-new, with_gc
Children:
00ede9e
Parents:
08a40fd
Message:

pool alloc functional

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

Legend:

Unmodified
Added
Removed
  • src/examples/gc_no_raii/src/allocate-pool.c

    r08a40fd rd67a9a1  
    11#define _BSD_SOURCE /* for MAP_ANON */
    22#define _DARWIN_C_SOURCE /* for MAP_ANON on OS X */
     3
    34
    45/* for standards info */
     
    2526#endif
    2627
    27 #ifdef __cplusplus
    28 extern "C" {
     28#ifdef CFA
     29extern "C"{
    2930#endif
    3031
     
    3233#if defined(GGGGC_USE_MALLOC)
    3334#define GGGGC_ALLOCATOR_MALLOC 1
    34 #include "allocate-malloc.cpp"
     35#include "../pool-alloc/allocate-malloc.c"
    3536
    3637#elif _POSIX_ADVISORY_INFO >= 200112L
    3738#define GGGGC_ALLOCATOR_POSIX_MEMALIGN 1
    38 #include "allocate-malign.cpp"
     39#include "../pool-alloc/allocate-malign.c"
    3940
    4041#elif defined(MAP_ANON)
    4142#define GGGGC_ALLOCATOR_MMAP 1
    42 #include "allocate-mmap.cpp"
     43#include "../pool-alloc/allocate-mmap.c"
    4344
    4445#elif defined(_WIN32)
    4546#define GGGGC_ALLOCATOR_VIRTUALALLOC 1
    46 #include "allocate-win-valloc.cpp"
     47#include "../pool-alloc/allocate-win-valloc.c"
    4748
    4849#else
    4950#warning GGGGC: No allocator available other than malloc!
    5051#define GGGGC_ALLOCATOR_MALLOC 1
    51 #include "allocate-malloc.cpp"
     52#include "../pool-alloc/allocate-malloc.c"
    5253
    5354#endif
     
    5859}
    5960
    60 #ifdef __cplusplus
     61#ifdef CFA
    6162}
    6263#endif
  • src/examples/gc_no_raii/src/internal/collector.c

    r08a40fd rd67a9a1  
    11#include "collector.h"
    22
     3#include <stdbool.h>
    34#include <stdint.h>
    45#include <stdlib.h>
     
    78#include "fstream.h"
    89
    9 #include "gc_tools.h"
    10 #include "state.h"
    11 // #include "memory_pool.h"
     10#include "memory_pool.h"
    1211
    1312void* gc_finish_alloc_block(void* block, size_t actual_size, size_t target_size);
     
    6968        gc_object_header* obj = gc_object_header_placement_ctor(header, actual_size);
    7069
    71         (void)obj; //remove unsused warning since this is for debug
     70        // (void)obj; //remove unsused warning since this is for debug
    7271        check(obj == get_object_ptr(data));
    7372
     
    7776}
    7877
    79 //      void process_reference(void** ref, std::vector<void**>& worklist)
    80 //      {
    81 //              check(!gc::gc_get_state().is_in_heap(ref));
    82 //
    83 //              if(gc_object_header* ptr = get_object_ptr(*ref))
    84 //              {
    85 //                      if(!ptr->is_forwarded)
    86 //                      {
    87 //                              copy_object(ptr);
    88 //
    89 //                              scan_object(ptr->forward, worklist);
    90 //
    91 //                              assign_reference(ref, ptr->forward);
    92 //                      }
    93 //                      else
    94 //                      {
    95 //                              //duplication to help debug
    96 //                              assign_reference(ref, ptr->forward);
    97 //                      }
    98 //              }
    99 //      }
    100 //
    101 //      void assign_reference(void** ref, gc_object_header* ptr)
    102 //      {
    103 //              void* address = (void*)(((intptr_t)ptr) + sizeof(gc_object_header));
    104 //
    105 //              write_aligned_ptr(ref, address);
    106 //      }
    107 //
    108 //      gc_object_header* copy_object(gc_object_header* ptr)
    109 //      {
    110 //              check(!ptr->forward);
    111 //              check(!ptr->is_forwarded);
    112 //              check(pool_of(ptr)->is_from_space());
    113 //
    114 //              memory_pool* pool = pool_of(ptr)->mirror();
    115 //
    116 //              void* new_block = pool->allocate(ptr->size, false);
    117 //
    118 //              std::memcpy(new_block, ptr, ptr->size);
    119 //
    120 //              gc_object_header* fwd_ptr = new (new_block) gc_object_header(ptr);
    121 //
    122 //              ptr->forward = fwd_ptr;
    123 //              ptr->is_forwarded = true;
    124 //
    125 //              return fwd_ptr;
    126 //      }
    127 //
    128 //      void scan_object(gc_object_header* object, std::vector<void**>& worklist)
    129 //      {
    130 //              gcpointer_base* type = object->type_chain;
    131 //              while(type)
    132 //              {
    133 //                      check(((intptr_t)type) > ((intptr_t)object));
    134 //                      check(((intptr_t)type) < ((intptr_t)((intptr_t)object) + object->size));
    135 //
    136 //                      check(gc::gc_get_state().is_in_to_space(&type->m_ptr));
    137 //
    138 //                      worklist.push_back(&type->m_ptr);
    139 //
    140 //                      type = type->m_next;
    141 //              }
    142 //      }
    143 // };
     78void 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
     101void 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
     108gc_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
     128void 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}
  • src/examples/gc_no_raii/src/internal/collector.h

    r08a40fd rd67a9a1  
    1212#include "internal/object_header.h"
    1313#include "internal/state.h"
    14 
    15 typedef int worklist_t;
     14#include "tools/worklist.h"
    1615
    1716inline bool gc_is_managed(void* address)
     
    2625}
    2726
    28 inline gc_memory_pool* pool_of(void* address)
     27inline gc_memory_pool* gc_pool_of(void* address)
    2928{
    3029        return (struct gc_memory_pool*)(((intptr_t)address) & POOL_PTR_MASK);
  • src/examples/gc_no_raii/src/internal/object_header.h

    r08a40fd rd67a9a1  
    3131
    3232gc_object_header* gc_object_header_placement_ctor(void* address, size_t size);
     33gc_object_header* gc_object_header_placement_copy_ctor(void* address, gc_object_header* other);
  • src/examples/gc_no_raii/src/internal/state.c

    r08a40fd rd67a9a1  
    11#include "state.h"
     2
     3#include <stdlib.h>
    24
    35//general purpouse includes
     
    911//gc internal includes
    1012// #include "globals.h"
    11 // #include "memory_pool.h"
     13#include "memory_pool.h"
    1214// #include "memory_pool_iterator.h"
    1315// #include "object_header.h"
     
    2224//      bool no_from_space_ref(gc_state* state);
    2325// #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 //
     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
    3741// bool state::is_in_heap(void* address) const
    3842// {
    39 //      memory_pool* target_pool = pool_of(address);
     43//      memory_pool* target_pool = gc_pool_of(address);
    4044//
    4145//      auto first = pools_table.cbegin();
     
    4448//      return result != last && (*result)->is_from_space();
    4549// }
    46 //
     50
    4751// bool state::is_in_to_space(void* address) const
    4852// {
     
    5559// }
    5660//
    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 // }
     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}
    8488//
    8589// void* state::try_allocate(size_t size)
  • src/examples/gc_no_raii/src/internal/state.h

    r08a40fd rd67a9a1  
    1616        size_t used_space;
    1717
    18         const struct memory_pool*       pools_table;
    19         size_t                          pools_table_count;
     18        // const struct memory_pool*    pools_table;
     19        // size_t                               pools_table_count;
    2020};
    2121
Note: See TracChangeset for help on using the changeset viewer.