source: libcfa/src/memory.cfa @ fd54fef

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since fd54fef was fd54fef, checked in by Michael Brooks <mlbrooks@…>, 3 years ago

Converting the project to use the new syntax for otype, dtype and ttytpe.

Changed prelude (gen), libcfa and test suite to use it. Added a simple deprecation rule of the old syntax to the parser; we might wish to support both syntaxes "officially," like with an extra CLI switch, but this measure should serve as a simple reminder for our team to try the new syntax.

  • Property mode set to 100644
File size: 3.8 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// memory.cfa -- Memory Management Tools for CFA
8//
9// Author           : Andrew Beach
10// Created On       : Tue Jun  2 16:48:00 2020
11// Last Modified By : Andrew Beach
12// Last Modified On : Tue Jun  3 12:30:00 2020
13// Update Count     : 0
14//
15
16#include "memory.hfa"
17#include "stdlib.hfa"
18
19// Internal data object.
20forall(T & | sized(T), Args... | { void ?{}(T &, Args); })
21void ?{}(counter_data(T) & this, Args args) {
22        (this.counter){1};
23        (this.object){args};
24}
25
26forall(T & | sized(T) | { void ^?{}(T &); })
27void ^?{}(counter_data(T) & this) {
28        assert(0 == this.counter);
29        ^(this.object){};
30}
31
32// This is one of many pointers keeping this alive.
33forall(T & | sized(T))
34void ?{}(counter_ptr(T) & this) {
35        this.data = 0p;
36}
37
38forall(T & | sized(T))
39void ?{}(counter_ptr(T) & this, zero_t) {
40        this.data = 0p;
41}
42
43forall(T & | sized(T) | { void ^?{}(T &); })
44static void internal_decrement(counter_ptr(T) & this) {
45        if (this.data && 0 == --this.data->counter) {
46                delete(this.data);
47        }
48}
49
50forall(T & | sized(T))
51static void internal_copy(counter_ptr(T) & this, counter_ptr(T) & that) {
52        this.data = that.data;
53        if (this.data) {
54                ++this.data->counter;
55        }
56}
57
58forall(T & | sized(T) | { void ^?{}(T &); })
59void ?{}(counter_ptr(T) & this, counter_ptr(T) that) {
60        // `that` is a copy but it should have neither a constructor
61        // nor destructor run on it so it shouldn't need adjustment.
62        internal_decrement(this);
63        internal_copy(this, that);
64}
65
66forall(T & | sized(T), Args... | { void ?{}(T&, Args); })
67void ?{}(counter_ptr(T) & this, Args args) {
68        this.data = (counter_data(T)*)new(args);
69}
70
71forall(T & | sized(T) | { void ^?{}(T &); })
72void ^?{}(counter_ptr(T) & this) {
73        internal_decrement(this);
74}
75
76forall(T & | sized(T))
77T & *?(counter_ptr(T) & this) {
78        return *((this.data) ? &this.data->object : 0p);
79}
80
81forall(T & | sized(T) | { void ^?{}(T &); })
82void ?=?(counter_ptr(T) & this, counter_ptr(T) that) {
83        if (this.data != that.data) {
84                internal_decrement(this);
85                internal_copy(this, that);
86        }
87}
88
89forall(T & | sized(T) | { void ^?{}(T &); })
90void ?=?(counter_ptr(T) & this, zero_t) {
91        internal_decrement(this);
92        this.data = 0p;
93}
94
95forall(T & | sized(T))
96int ?==?(counter_ptr(T) const & this, counter_ptr(T) const & that) {
97        return this.data == that.data;
98}
99
100forall(T & | sized(T))
101int ?!=?(counter_ptr(T) const & this, counter_ptr(T) const & that) {
102        return !?==?(this, that);
103}
104
105forall(T & | sized(T))
106int ?==?(counter_ptr(T) const & this, zero_t) {
107        return this.data == 0;
108}
109
110forall(T & | sized(T))
111int ?!=?(counter_ptr(T) const & this, zero_t) {
112        return !?==?(this, (zero_t)0);
113}
114
115// This is the only pointer that keeps this alive.
116forall(T &)
117void ?{}(unique_ptr(T) & this) {
118        this.data = 0p;
119}
120
121forall(T &)
122void ?{}(unique_ptr(T) & this, zero_t) {
123        this.data = 0p;
124}
125
126forall(T & | sized(T), Args... | { void ?{}(T &, Args); })
127void ?{}(unique_ptr(T) & this, Args args) {
128        this.data = (T *)new(args);
129}
130
131forall(T & | { void ^?{}(T &); })
132void ^?{}(unique_ptr(T) & this) {
133        delete(this.data);
134}
135
136forall(T &)
137T & *?(unique_ptr(T) & this) {
138        return *this.data;
139}
140
141forall(T & | { void ^?{}(T &); })
142void ?=?(unique_ptr(T) & this, zero_t) {
143        delete(this.data);
144        this.data = 0p;
145}
146
147forall(T & | { void ^?{}(T &); })
148void move(unique_ptr(T) & this, unique_ptr(T) & that) {
149        delete(this.data);
150        this.data = that.data;
151        that.data = 0p;
152}
153
154forall(T &)
155int ?==?(unique_ptr(T) const & this, unique_ptr(T) const & that) {
156        return this.data == that.data;
157}
158
159forall(T &)
160int ?!=?(unique_ptr(T) const & this, unique_ptr(T) const & that) {
161        return !?==?(this, that);
162}
163
164forall(T &)
165int ?==?(unique_ptr(T) const & this, zero_t) {
166        return this.data == 0;
167}
168
169forall(T &)
170int ?!=?(unique_ptr(T) const & this, zero_t) {
171        return !?==?(this, (zero_t)0);
172}
Note: See TracBrowser for help on using the repository browser.