source: libcfa/src/memory.cfa @ ad47ec4

Last change on this file since ad47ec4 was 789f279, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

More standard lib visibility

  • Property mode set to 100644
File size: 4.0 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#pragma GCC visibility push(default)
20
21// Internal data object.
22forall(T & | sized(T), Args... | { void ?{}(T &, Args); })
23void ?{}(counter_data(T) & this, Args args) {
24        (this.counter){1};
25        (this.object){args};
26}
27
28forall(T & | sized(T) | { void ^?{}(T &); })
29void ^?{}(counter_data(T) & this) {
30        assert(0 == this.counter);
31        ^(this.object){};
32}
33
34// This is one of many pointers keeping this alive.
35forall(T & | sized(T))
36void ?{}(counter_ptr(T) & this) {
37        this.data = 0p;
38}
39
40forall(T & | sized(T))
41void ?{}(counter_ptr(T) & this, zero_t) {
42        this.data = 0p;
43}
44
45forall(T & | sized(T) | { void ^?{}(T &); })
46static void internal_decrement(counter_ptr(T) & this) {
47        if (this.data && 0 == --this.data->counter) {
48                delete(this.data);
49        }
50}
51
52forall(T & | sized(T))
53static void internal_copy(counter_ptr(T) & this, counter_ptr(T) & that) {
54        this.data = that.data;
55        if (this.data) {
56                ++this.data->counter;
57        }
58}
59
60forall(T & | sized(T))
61void ?{}(counter_ptr(T) & this, counter_ptr(T) that) {
62        // `that` is a copy but it should have neither a constructor
63        // nor destructor run on it so it shouldn't need adjustment.
64        internal_copy(this, that);
65}
66
67forall(T & | sized(T), Args... | { void ?{}(T&, Args); })
68void ?{}(counter_ptr(T) & this, Args args) {
69        this.data = malloc();
70        this.data->counter = 1;
71        (this.data->object){args};
72}
73
74forall(T & | sized(T) | { void ^?{}(T &); })
75void ^?{}(counter_ptr(T) & this) {
76        internal_decrement(this);
77}
78
79forall(T & | sized(T))
80T & *?(counter_ptr(T) & this) {
81        return *((this.data) ? &this.data->object : 0p);
82}
83
84forall(T & | sized(T) | { void ^?{}(T &); })
85void ?=?(counter_ptr(T) & this, counter_ptr(T) that) {
86        if (this.data != that.data) {
87                internal_decrement(this);
88                internal_copy(this, that);
89        }
90}
91
92forall(T & | sized(T) | { void ^?{}(T &); })
93void ?=?(counter_ptr(T) & this, zero_t) {
94        internal_decrement(this);
95        this.data = 0p;
96}
97
98forall(T & | sized(T))
99int ?==?(counter_ptr(T) const & this, counter_ptr(T) const & that) {
100        return this.data == that.data;
101}
102
103forall(T & | sized(T))
104int ?!=?(counter_ptr(T) const & this, counter_ptr(T) const & that) {
105        return !?==?(this, that);
106}
107
108forall(T & | sized(T))
109int ?==?(counter_ptr(T) const & this, zero_t) {
110        return this.data == 0;
111}
112
113forall(T & | sized(T))
114int ?!=?(counter_ptr(T) const & this, zero_t) {
115        return !?==?(this, (zero_t)0);
116}
117
118// This is the only pointer that keeps this alive.
119forall(T &)
120void ?{}(unique_ptr(T) & this) {
121        this.data = 0p;
122}
123
124forall(T &)
125void ?{}(unique_ptr(T) & this, zero_t) {
126        this.data = 0p;
127}
128
129forall(T & | sized(T), Args... | { void ?{}(T &, Args); })
130void ?{}(unique_ptr(T) & this, Args args) {
131        this.data = malloc();
132        (*this.data){args};
133}
134
135forall(T & | { void ^?{}(T &); })
136void ^?{}(unique_ptr(T) & this) {
137        delete(this.data);
138}
139
140forall(T &)
141T & *?(unique_ptr(T) & this) {
142        return *this.data;
143}
144
145forall(T & | { void ^?{}(T &); })
146void ?=?(unique_ptr(T) & this, zero_t) {
147        delete(this.data);
148        this.data = 0p;
149}
150
151forall(T & | { void ^?{}(T &); })
152void move(unique_ptr(T) & this, unique_ptr(T) & that) {
153        delete(this.data);
154        this.data = that.data;
155        that.data = 0p;
156}
157
158forall(T &)
159T * release(unique_ptr(T) & this) {
160        T * data = this.data;
161        this.data = 0p;
162        return data;
163}
164
165forall(T &)
166int ?==?(unique_ptr(T) const & this, unique_ptr(T) const & that) {
167        return this.data == that.data;
168}
169
170forall(T &)
171int ?!=?(unique_ptr(T) const & this, unique_ptr(T) const & that) {
172        return !?==?(this, that);
173}
174
175forall(T &)
176int ?==?(unique_ptr(T) const & this, zero_t) {
177        return this.data == 0;
178}
179
180forall(T &)
181int ?!=?(unique_ptr(T) const & this, zero_t) {
182        return !?==?(this, (zero_t)0);
183}
Note: See TracBrowser for help on using the repository browser.