Ignore:
Timestamp:
Aug 18, 2016, 10:08:31 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, 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:
7a5d773
Parents:
926af74
Message:

first basic test for garbage collector now compiles and works

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

Legend:

Unmodified
Added
Removed
  • src/examples/gc_no_raii/premake4.lua

    r926af74 r6643e72  
    66        "src/",
    77        "../",
     8        "containers/"
    89}
    910
     
    4849                linkoptions (linkOptionList)
    4950                includedirs (includeDirList)
    50                 files { "src/**.c" }
     51                files { "src/**.c", "containers/**.c" }
    5152
    5253        configuration "debug"
  • src/examples/gc_no_raii/src/gc.h

    r926af74 r6643e72  
    44#include "internal/collector.h"
    55
    6 forall(otype T)
    7 static inline gcpointer(T) gcmalloc()
    8 {
    9     gcpointer(T) ptr = { gc_allocate(sizeof(T)) };
    10     ptr{};
    11     gc_conditional_collect();
    12     return ptr;
    13 }
     6// forall(otype T)
     7// static inline gcpointer(T) gcmalloc()
     8// {
     9//     gcpointer(T) ptr = { gc_allocate(sizeof(T)) };
     10//     ptr{};
     11//     gc_conditional_collect();
     12//     return ptr;
     13// }
    1414
    1515forall(otype T)
    1616static inline void gcmalloc(gcpointer(T)* ptr)
    1717{
    18         ptr{ gc_allocate(sizeof(T)) };
    19       (*ptr){};
     18        ptr { gc_allocate(sizeof(T)) };
     19        get(ptr) {};
    2020      gc_conditional_collect();
    2121}
  • src/examples/gc_no_raii/src/gcpointers.c

    r926af74 r6643e72  
    4242}
    4343
    44 void gcpointer_ctor(gcpointer_t* this)
     44void ?{}(gcpointer_t* this)
    4545{
    4646        this->ptr = (intptr_t)NULL;
     
    4848}
    4949
    50 void gcpointer_ctor(gcpointer_t* this, void* address)
     50void ?{}(gcpointer_t* this, void* address)
    5151{
    5252        this->ptr = (intptr_t)address;
     
    5656}
    5757
    58 void gcpointer_ctor(gcpointer_t* this, gcpointer_t* other)
     58void ?{}(gcpointer_t* this, gcpointer_t other)
    5959{
    60         this->ptr = other->ptr;
     60        this->ptr = other.ptr;
    6161        this->next = NULL;
    6262
     
    6464}
    6565
    66 void gcpointer_dtor(gcpointer_t* this)
     66void ^?{}(gcpointer_t* this)
    6767{
    6868        unregister_ptr(this);
     
    9898        return this->ptr == (intptr_t)NULL;
    9999}
     100
     101forall(otype T) void ?{}(gcpointer(T)* this) {
     102        (&this->internal) {};
     103}
     104
     105forall(otype T) void ?{}(gcpointer(T)* this, void* address) {
     106        (&this->internal) { address };
     107}
     108
     109forall(otype T) void ?{}(gcpointer(T)* this, gcpointer(T)* other) {
     110        (&this->internal) { other->internal };
     111}
     112
     113forall(otype T) void ^?{}(gcpointer(T)* this) {
     114        ^?{}(&this->internal);
     115}
     116
     117// forall(otype T) gcpointer(T) ?=?(gcpointer(T) this, gcpointer(T) rhs);
     118//
     119// forall(otype T) T *?(gcpointer(T) this);
     120
     121forall(otype T) T* get(gcpointer(T)* this) {
     122        return (T*)this->internal.ptr;
     123}
     124//
     125// //Logical operators
     126// forall(otype T) int ?!=?(gcpointer(T) this, gcpointer(T) rhs);
     127// forall(otype T) int ?==?(gcpointer(T) this, gcpointer(T) rhs);
  • src/examples/gc_no_raii/src/gcpointers.h

    r926af74 r6643e72  
    3030forall(otype T) void ?{}(gcpointer(T)* this);
    3131forall(otype T) void ?{}(gcpointer(T)* this, void* address);
    32 forall(otype T) void ctor(gcpointer(T)* this, void* address);
    3332forall(otype T) void ?{}(gcpointer(T)* this, gcpointer(T)* other);
    3433forall(otype T) void ^?{}(gcpointer(T)* this);
     
    3736
    3837forall(otype T) T *?(gcpointer(T) this);
     38forall(otype T) T* get(gcpointer(T)* this);
    3939
    4040//Logical operators
  • src/examples/gc_no_raii/src/internal/collector.c

    r926af74 r6643e72  
    88}
    99#endif
     10
     11#include <fstream>
    1012
    1113#include "state.h"
     
    3638void* gc_allocate(size_t target_size)
    3739{
     40        sout | "Allocating " | target_size | " bytes" | endl;
     41
    3842        size_t size = gc_compute_size(target_size + sizeof(gc_object_header));
     43
     44        sout | "Object header size: " | sizeof(gc_object_header) | " bytes" | endl;
     45        sout | "Actual allocation size: " | size | " bytes" | endl;
    3946
    4047        check(size < POOL_SIZE_BYTES);
  • src/examples/gc_no_raii/src/internal/state.h

    r926af74 r6643e72  
    99}
    1010#endif
     11#include <fstream>
    1112#include <vector>
    1213
     
    3738static inline bool gc_needs_collect(gc_state* state)
    3839{
     40        sout | "Used Space: " | state->used_space | " bytes" | endl;
    3941        return state->used_space * 2 > state->total_space;
    4042}
  • src/examples/gc_no_raii/test/gctest.c

    r926af74 r6643e72  
    22
    33#include "gc.h"
     4#include "internal/collector.h"
    45
    56#warning default test
     
    89        sout | "Bonjour au monde!\n";
    910
    10         for(int i = 0; i < 1000000; i++) {
    11                 gcpointer(int) anInt;
    12                 gcmalloc(&anInt);
     11        gcpointer(int) theInt;
     12        gcmalloc(&theInt);
     13
     14        for(int i = 0; i < 10; i++) {
     15                int a;
     16                {
     17                        gcpointer(int) anInt;
     18                        gcmalloc(&anInt);
     19                }
     20                int p;
    1321        }
     22
     23        gc_collect(gc_get_state());
     24        gc_conditional_collect();
    1425}
Note: See TracChangeset for help on using the changeset viewer.