Changeset 13488ca


Ignore:
Timestamp:
May 17, 2016, 5:51:20 PM (9 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:
19de7b70
Parents:
58440ce
Message:

more testing done

Location:
src/examples/wrapper/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/examples/wrapper/src/main.c

    r58440ce r13488ca  
    33int main(int argc, char const *argv[])
    44{
    5         content_t c;
    6 
    7         content_t* cp = (content_t*)malloc();
     5        wrapper_t p;
    86        return 0;
    97}
  • src/examples/wrapper/src/pointer.h

    r58440ce r13488ca  
    33#include <fstream>
    44#include <stddef.h>
    5 #include <stdlib.h>
     5#include <stdlib>
     6
     7//==============================================================================
     8// type safe malloc / free
     9
     10forall(otype T)
     11T* new()
     12{
     13        T* p = malloc();
     14        p{};
     15        return p;
     16}
     17
     18forall(otype T)
     19void delete(T* p)
     20{
     21        ^p{};
     22        free(p);
     23}
     24
     25//==============================================================================
     26// ref counter content
    627
    728struct content_t
     
    1738}
    1839
     40void ?{}(content_t* this)
     41{
     42        sout | "Constructing content" | endl;
     43        this->count = 0;
     44}
     45
    1946void ^?{}(content_t* this)
    2047{
     
    2249}
    2350
    24 // struct wrapper_t
    25 // {
    26 //      content_t* ptr;
    27 // };
    28 //
    29 // void ?{}(wrapper_t* this)
    30 // {
    31 //      //content_t** cp = &this->ptr;
    32 //      //*cp = malloc();
    33 //      this->ptr->count++;
    34 //      sout | "Constructing ref pointer" | endl;
    35 //      sout | "Reference is " | this->ptr->count | endl;
    36 // }
    37 //
    38 // void ^?{}(wrapper_t* this)
    39 // {
    40 //      this->ptr->count--;
    41 //      if(!this->ptr->count) free(this->ptr);
    42 //      sout | "Destroying ref pointer" | endl;
    43 //      sout | "Reference is " | this->ptr->count | endl;
    44 // }
     51//==============================================================================
     52// ref counter wrapper
     53
     54struct wrapper_t
     55{
     56        content_t* ptr;
     57};
     58
     59wrapper_t wrap(int val)
     60{
     61        wrapper_t w;
     62        content_t* c = malloc;
     63        c{};
     64        c->value = val;
     65        reset(&w, c);
     66        return w;
     67}
     68
     69void ?{}(wrapper_t* this)
     70{
     71        this->ptr = new();
     72        this->ptr->count++;
     73        sout | "Constructing empty ref pointer" | endl;
     74}
     75
     76void ^?{}(wrapper_t* this)
     77{
     78        {
     79                this->ptr->count--;
     80                if(!this->ptr->count) delete(this->ptr);
     81                sout | "Destroying ref pointer" | endl;
     82                sout | "Reference is " | this->ptr->count | endl;
     83        }
     84        else
     85        {
     86                sout | "Destroying empty ref pointer" | endl;
     87        }
     88}
     89
     90void set(wrapper_t* this, content_t* c)
     91{
     92        this->ptr = c;
     93        sout | "Setting ref pointer" | endl;
     94        sout | "Reference is " | this->ptr->count | endl;
     95}
Note: See TracChangeset for help on using the changeset viewer.