source: libcfa/src/memory.cfa @ 0d070ca

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

Added a first draft of the memory management library module.

  • Property mode set to 100644
File size: 3.9 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.
20forall(dtype T | sized(T), ttype Args | { void ?{}(T &, Args); })
21void ?{}(counter_data(T) & this, Args args) {
22        (this.counter){1};
23        (this.object){args};
24}
25
26forall(dtype 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(dtype T | sized(T))
34void ?{}(counter_ptr(T) & this) {
35        this.data = 0p;
36}
37
38forall(dtype T | sized(T))
39void ?{}(counter_ptr(T) & this, zero_t) {
40        this.data = 0p;
41}
42
43forall(dtype 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(dtype 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(dtype 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(dtype T | sized(T), ttype Args | { void ?{}(T&, Args); })
67void ?{}(counter_ptr(T) & this, Args args) {
68        this.data = new(args);
69}
70
71forall(dtype T | sized(T) | { void ^?{}(T &); })
72void ^?{}(counter_ptr(T) & this) {
73        internal_decrement(this);
74}
75
76forall(dtype T | sized(T))
77T & *?(counter_ptr(T) & this) {
78        return *((this.data) ? &this.data->object : 0p);
79}
80
81forall(dtype 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(dtype T | sized(T) | { void ^?{}(T &); })
90void ?=?(counter_ptr(T) & this, zero_t) {
91        internal_decrement(this);
92        this.data = 0p;
93}
94
95forall(dtype T | sized(T))
96int ?==?(counter_ptr(T) const & this, counter_ptr(T) const & that) {
97        return this.data == that.data;
98}
99
100forall(dtype T | sized(T))
101int ?!=?(counter_ptr(T) const & this, counter_ptr(T) const & that) {
102        return !?==?(this, that);
103}
104
105forall(dtype T | sized(T))
106int ?==?(counter_ptr(T) const & this, zero_t) {
107        return this.data == 0;
108}
109
110forall(dtype 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(dtype T)
117void ?{}(unique_ptr(T) & this) {
118        this.data = 0p;
119}
120
121forall(dtype T)
122void ?{}(unique_ptr(T) & this, zero_t) {
123        this.data = 0p;
124}
125
126forall(dtype T | sized(T), ttype Args | { void ?{}(T &, Args); })
127void ?{}(unique_ptr(T) & this, Args args) {
128        this.data = new(args);
129}
130
131forall(dtype T | { void ^?{}(T &); })
132void ^?{}(unique_ptr(T) & this) {
133        delete(this.data);
134}
135
136forall(dtype T)
137T & *?(unique_ptr(T) & this) {
138        return *this.data;
139}
140
141forall(dtype T | { void ^?{}(T &); })
142void ?=?(unique_ptr(T) & this, zero_t) {
143        delete(this.data);
144        this.data = 0p;
145}
146
147forall(dtype 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(dtype T)
155int ?==?(unique_ptr(T) const & this, unique_ptr(T) const & that) {
156        return this.data == that.data;
157}
158
159forall(dtype T)
160int ?!=?(unique_ptr(T) const & this, unique_ptr(T) const & that) {
161        return !?==?(this, that);
162}
163
164forall(dtype T)
165int ?==?(unique_ptr(T) const & this, zero_t) {
166        return this.data == 0;
167}
168
169forall(dtype 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.