Changeset 13488ca
- Timestamp:
- May 17, 2016, 5:51:20 PM (9 years ago)
- 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
- Location:
- src/examples/wrapper/src
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
src/examples/wrapper/src/main.c
r58440ce r13488ca 3 3 int main(int argc, char const *argv[]) 4 4 { 5 content_t c; 6 7 content_t* cp = (content_t*)malloc(); 5 wrapper_t p; 8 6 return 0; 9 7 } -
src/examples/wrapper/src/pointer.h
r58440ce r13488ca 3 3 #include <fstream> 4 4 #include <stddef.h> 5 #include <stdlib.h> 5 #include <stdlib> 6 7 //============================================================================== 8 // type safe malloc / free 9 10 forall(otype T) 11 T* new() 12 { 13 T* p = malloc(); 14 p{}; 15 return p; 16 } 17 18 forall(otype T) 19 void delete(T* p) 20 { 21 ^p{}; 22 free(p); 23 } 24 25 //============================================================================== 26 // ref counter content 6 27 7 28 struct content_t … … 17 38 } 18 39 40 void ?{}(content_t* this) 41 { 42 sout | "Constructing content" | endl; 43 this->count = 0; 44 } 45 19 46 void ^?{}(content_t* this) 20 47 { … … 22 49 } 23 50 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 54 struct wrapper_t 55 { 56 content_t* ptr; 57 }; 58 59 wrapper_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 69 void ?{}(wrapper_t* this) 70 { 71 this->ptr = new(); 72 this->ptr->count++; 73 sout | "Constructing empty ref pointer" | endl; 74 } 75 76 void ^?{}(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 90 void 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.