source: libcfa/src/memory.cfa @ f8a7fed

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since f8a7fed was 85871478, checked in by Andrew Beach <ajbeach@…>, 4 years ago

Forgot about a memory fix, don't decrement a new pointer before overwriting it.

  • 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 : Mon Feb  1 16:10:00 2021
13// Update Count     : 1
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))
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_copy(this, that);
63}
64
65forall(T & | sized(T), Args... | { void ?{}(T&, Args); })
66void ?{}(counter_ptr(T) & this, Args args) {
67        this.data = malloc();
68        this.data->counter = 1;
69        (this.data->object){args};
70}
71
72forall(T & | sized(T) | { void ^?{}(T &); })
73void ^?{}(counter_ptr(T) & this) {
74        internal_decrement(this);
75}
76
77forall(T & | sized(T))
78T & *?(counter_ptr(T) & this) {
79        return *((this.data) ? &this.data->object : 0p);
80}
81
82forall(T & | sized(T) | { void ^?{}(T &); })
83void ?=?(counter_ptr(T) & this, counter_ptr(T) that) {
84        if (this.data != that.data) {
85                internal_decrement(this);
86                internal_copy(this, that);
87        }
88}
89
90forall(T & | sized(T) | { void ^?{}(T &); })
91void ?=?(counter_ptr(T) & this, zero_t) {
92        internal_decrement(this);
93        this.data = 0p;
94}
95
96forall(T & | sized(T))
97int ?==?(counter_ptr(T) const & this, counter_ptr(T) const & that) {
98        return this.data == that.data;
99}
100
101forall(T & | sized(T))
102int ?!=?(counter_ptr(T) const & this, counter_ptr(T) const & that) {
103        return !?==?(this, that);
104}
105
106forall(T & | sized(T))
107int ?==?(counter_ptr(T) const & this, zero_t) {
108        return this.data == 0;
109}
110
111forall(T & | sized(T))
112int ?!=?(counter_ptr(T) const & this, zero_t) {
113        return !?==?(this, (zero_t)0);
114}
115
116// This is the only pointer that keeps this alive.
117forall(T &)
118void ?{}(unique_ptr(T) & this) {
119        this.data = 0p;
120}
121
122forall(T &)
123void ?{}(unique_ptr(T) & this, zero_t) {
124        this.data = 0p;
125}
126
127forall(T & | sized(T), Args... | { void ?{}(T &, Args); })
128void ?{}(unique_ptr(T) & this, Args args) {
129        this.data = malloc();
130        (*this.data){args};
131}
132
133forall(T & | { void ^?{}(T &); })
134void ^?{}(unique_ptr(T) & this) {
135        delete(this.data);
136}
137
138forall(T &)
139T & *?(unique_ptr(T) & this) {
140        return *this.data;
141}
142
143forall(T & | { void ^?{}(T &); })
144void ?=?(unique_ptr(T) & this, zero_t) {
145        delete(this.data);
146        this.data = 0p;
147}
148
149forall(T & | { void ^?{}(T &); })
150void move(unique_ptr(T) & this, unique_ptr(T) & that) {
151        delete(this.data);
152        this.data = that.data;
153        that.data = 0p;
154}
155
156forall(T &)
157int ?==?(unique_ptr(T) const & this, unique_ptr(T) const & that) {
158        return this.data == that.data;
159}
160
161forall(T &)
162int ?!=?(unique_ptr(T) const & this, unique_ptr(T) const & that) {
163        return !?==?(this, that);
164}
165
166forall(T &)
167int ?==?(unique_ptr(T) const & this, zero_t) {
168        return this.data == 0;
169}
170
171forall(T &)
172int ?!=?(unique_ptr(T) const & this, zero_t) {
173        return !?==?(this, (zero_t)0);
174}
Note: See TracBrowser for help on using the repository browser.