source: libcfa/src/memory.cfa @ 8be729f

arm-ehenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 8be729f was 8be729f, checked in by Andrew Beach <ajbeach@…>, 22 months ago

That should fix the memory module. Simply removed all the special features.

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