source: libcfa/src/memory.cfa@ 1f8f5a7

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 1f8f5a7 was fd54fef, checked in by Michael Brooks <mlbrooks@…>, 5 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
RevLine 
[aabb846]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.
[fd54fef]20forall(T & | sized(T), Args... | { void ?{}(T &, Args); })
[aabb846]21void ?{}(counter_data(T) & this, Args args) {
22 (this.counter){1};
23 (this.object){args};
24}
25
[fd54fef]26forall(T & | sized(T) | { void ^?{}(T &); })
[aabb846]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.
[fd54fef]33forall(T & | sized(T))
[aabb846]34void ?{}(counter_ptr(T) & this) {
35 this.data = 0p;
36}
37
[fd54fef]38forall(T & | sized(T))
[aabb846]39void ?{}(counter_ptr(T) & this, zero_t) {
40 this.data = 0p;
41}
42
[fd54fef]43forall(T & | sized(T) | { void ^?{}(T &); })
[aabb846]44static void internal_decrement(counter_ptr(T) & this) {
45 if (this.data && 0 == --this.data->counter) {
46 delete(this.data);
47 }
48}
49
[fd54fef]50forall(T & | sized(T))
[aabb846]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
[fd54fef]58forall(T & | sized(T) | { void ^?{}(T &); })
[aabb846]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
[fd54fef]66forall(T & | sized(T), Args... | { void ?{}(T&, Args); })
[aabb846]67void ?{}(counter_ptr(T) & this, Args args) {
[09da82d]68 this.data = (counter_data(T)*)new(args);
[aabb846]69}
70
[fd54fef]71forall(T & | sized(T) | { void ^?{}(T &); })
[aabb846]72void ^?{}(counter_ptr(T) & this) {
73 internal_decrement(this);
74}
75
[fd54fef]76forall(T & | sized(T))
[aabb846]77T & *?(counter_ptr(T) & this) {
78 return *((this.data) ? &this.data->object : 0p);
79}
80
[fd54fef]81forall(T & | sized(T) | { void ^?{}(T &); })
[aabb846]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
[fd54fef]89forall(T & | sized(T) | { void ^?{}(T &); })
[aabb846]90void ?=?(counter_ptr(T) & this, zero_t) {
91 internal_decrement(this);
92 this.data = 0p;
93}
94
[fd54fef]95forall(T & | sized(T))
[aabb846]96int ?==?(counter_ptr(T) const & this, counter_ptr(T) const & that) {
97 return this.data == that.data;
98}
99
[fd54fef]100forall(T & | sized(T))
[aabb846]101int ?!=?(counter_ptr(T) const & this, counter_ptr(T) const & that) {
102 return !?==?(this, that);
103}
104
[fd54fef]105forall(T & | sized(T))
[aabb846]106int ?==?(counter_ptr(T) const & this, zero_t) {
107 return this.data == 0;
108}
109
[fd54fef]110forall(T & | sized(T))
[aabb846]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.
[fd54fef]116forall(T &)
[aabb846]117void ?{}(unique_ptr(T) & this) {
118 this.data = 0p;
119}
120
[fd54fef]121forall(T &)
[aabb846]122void ?{}(unique_ptr(T) & this, zero_t) {
123 this.data = 0p;
124}
125
[fd54fef]126forall(T & | sized(T), Args... | { void ?{}(T &, Args); })
[aabb846]127void ?{}(unique_ptr(T) & this, Args args) {
[09da82d]128 this.data = (T *)new(args);
[aabb846]129}
130
[fd54fef]131forall(T & | { void ^?{}(T &); })
[aabb846]132void ^?{}(unique_ptr(T) & this) {
133 delete(this.data);
134}
135
[fd54fef]136forall(T &)
[aabb846]137T & *?(unique_ptr(T) & this) {
138 return *this.data;
139}
140
[fd54fef]141forall(T & | { void ^?{}(T &); })
[aabb846]142void ?=?(unique_ptr(T) & this, zero_t) {
143 delete(this.data);
144 this.data = 0p;
145}
146
[fd54fef]147forall(T & | { void ^?{}(T &); })
[aabb846]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
[fd54fef]154forall(T &)
[aabb846]155int ?==?(unique_ptr(T) const & this, unique_ptr(T) const & that) {
156 return this.data == that.data;
157}
158
[fd54fef]159forall(T &)
[aabb846]160int ?!=?(unique_ptr(T) const & this, unique_ptr(T) const & that) {
161 return !?==?(this, that);
162}
163
[fd54fef]164forall(T &)
[aabb846]165int ?==?(unique_ptr(T) const & this, zero_t) {
166 return this.data == 0;
167}
168
[fd54fef]169forall(T &)
[aabb846]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.