summaryrefslogtreecommitdiffhomepage
path: root/include/stc/carc.h
blob: 9ba2ddd1cc621a24fe2da599240f072bfc89d79b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* MIT License
 *
 * Copyright (c) 2023 Tyge Løvset
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/* carc: atomic reference counted shared_ptr
#define i_implement
#include <stc/cstr.h>

typedef struct { cstr name, last; } Person;

Person Person_make(const char* name, const char* last) {
    return (Person){.name = cstr_from(name), .last = cstr_from(last)};
}
Person Person_clone(Person p) {
    p.name = cstr_clone(p.name);
    p.last = cstr_clone(p.last);
    return p;
}
void Person_drop(Person* p) {
    printf("drop: %s %s\n", cstr_str(&p->name), cstr_str(&p->last));
    cstr_drop(&p->name);
    cstr_drop(&p->last);
}

#define i_type ArcPers
#define i_valclass Person    // clone, drop, cmp, hash
#define i_opt c_no_cmp|c_no_hash  // exclude cmp, hash
#include <stc/carc.h>

int main(void) {
    ArcPers p = ArcPers_from(Person_make("John", "Smiths"));
    ArcPers q = ArcPers_clone(p); // share the pointer

    printf("%s %s. uses: %ld\n", cstr_str(&q.get->name), cstr_str(&q.get->last), *q.use_count);
    c_drop(ArcPers, &p, &q);
}
*/
#include "priv/linkage.h"

#ifndef CARC_H_INCLUDED
#define CARC_H_INCLUDED
#include "ccommon.h"
#include "forward.h"
#include <stdlib.h>

#if defined(__GNUC__) || defined(__clang__)
    typedef long catomic_long;
    #define c_atomic_inc(v) (void)__atomic_add_fetch(v, 1, __ATOMIC_SEQ_CST)
    #define c_atomic_dec_and_test(v) !__atomic_sub_fetch(v, 1, __ATOMIC_SEQ_CST)
#elif defined(_MSC_VER)
    #include <intrin.h>
    typedef long catomic_long;
    #define c_atomic_inc(v) (void)_InterlockedIncrement(v)
    #define c_atomic_dec_and_test(v) !_InterlockedDecrement(v)
#else
    #include <stdatomic.h>
    typedef _Atomic long catomic_long;
    #define c_atomic_inc(v) (void)atomic_fetch_add(v, 1)
    #define c_atomic_dec_and_test(v) (atomic_fetch_sub(v, 1) == 1)
#endif

#define carc_null {0}
#endif // CARC_H_INCLUDED

#define _i_prefix carc_
#define _i_carc
#include "priv/template.h"
typedef i_keyraw _cx_raw;

#if !c_option(c_no_atomic)
  #define _i_atomic_inc(v)          c_atomic_inc(v)
  #define _i_atomic_dec_and_test(v) c_atomic_dec_and_test(v)
#else
  #define _i_atomic_inc(v)          (void)(++*(v))
  #define _i_atomic_dec_and_test(v) !(--*(v))
#endif
#ifndef i_is_forward
_cx_DEFTYPES(_c_carc_types, _cx_Self, i_key);
#endif
struct _cx_MEMB(_rep_) { catomic_long counter; i_key value; };

STC_INLINE _cx_Self _cx_MEMB(_init)(void) 
    { return c_LITERAL(_cx_Self){NULL, NULL}; }

STC_INLINE long _cx_MEMB(_use_count)(const _cx_Self* self)
    { return self->use_count ? *self->use_count : 0; }

STC_INLINE _cx_Self _cx_MEMB(_from_ptr)(_cx_value* p) {
    _cx_Self ptr = {p};
    if (p) 
        *(ptr.use_count = _i_alloc(catomic_long)) = 1;
    return ptr;
}

// c++: std::make_shared<_cx_value>(val)
STC_INLINE _cx_Self _cx_MEMB(_make)(_cx_value val) {
    _cx_Self ptr;
    struct _cx_MEMB(_rep_)* rep = _i_alloc(struct _cx_MEMB(_rep_));
    *(ptr.use_count = &rep->counter) = 1;
    *(ptr.get = &rep->value) = val;
    return ptr;
}

STC_INLINE _cx_raw _cx_MEMB(_toraw)(const _cx_Self* self)
    { return i_keyto(self->get); }

STC_INLINE _cx_Self _cx_MEMB(_move)(_cx_Self* self) {
    _cx_Self ptr = *self;
    self->get = NULL, self->use_count = NULL;
    return ptr;
}

STC_INLINE void _cx_MEMB(_drop)(_cx_Self* self) {
    if (self->use_count && _i_atomic_dec_and_test(self->use_count)) {
        i_keydrop(self->get);
        if ((char *)self->get != (char *)self->use_count + offsetof(struct _cx_MEMB(_rep_), value))
            i_free(self->get);
        i_free((long*)self->use_count);
    }
}

STC_INLINE void _cx_MEMB(_reset)(_cx_Self* self) {
    _cx_MEMB(_drop)(self);
    self->use_count = NULL, self->get = NULL;
}

STC_INLINE void _cx_MEMB(_reset_to)(_cx_Self* self, _cx_value* p) {
    _cx_MEMB(_drop)(self);
    *self = _cx_MEMB(_from_ptr)(p);
}

#ifndef i_no_emplace
STC_INLINE _cx_Self _cx_MEMB(_from)(_cx_raw raw)
    { return _cx_MEMB(_make)(i_keyfrom(raw)); }
#else
STC_INLINE _cx_Self _cx_MEMB(_from)(_cx_value val)
    { return _cx_MEMB(_make)(val); }
#endif    

// does not use i_keyclone, so OK to always define.
STC_INLINE _cx_Self _cx_MEMB(_clone)(_cx_Self ptr) {
    if (ptr.use_count)
        _i_atomic_inc(ptr.use_count);
    return ptr;
}

// take ownership of unowned
STC_INLINE void _cx_MEMB(_take)(_cx_Self* self, _cx_Self unowned) {
    _cx_MEMB(_drop)(self);
    *self = unowned;
}
// share ownership with ptr
STC_INLINE void _cx_MEMB(_assign)(_cx_Self* self, _cx_Self ptr) {
    if (ptr.use_count)
        _i_atomic_inc(ptr.use_count);
    _cx_MEMB(_drop)(self);
    *self = ptr;
}

#if defined _i_has_cmp
    STC_INLINE int _cx_MEMB(_raw_cmp)(const _cx_raw* rx, const _cx_raw* ry)
        { return i_cmp(rx, ry); }

    STC_INLINE int _cx_MEMB(_cmp)(const _cx_Self* self, const _cx_Self* other) {
        _cx_raw rx = i_keyto(self->get), ry = i_keyto(other->get);
        return i_cmp((&rx), (&ry));
    }
#else
    STC_INLINE int _cx_MEMB(_raw_cmp)(const _cx_raw* rx, const _cx_raw* ry)
        { return c_default_cmp(&rx, &ry); }

    STC_INLINE int _cx_MEMB(_cmp)(const _cx_Self* self, const _cx_Self* other) {
        return c_default_cmp(&self->get, &other->get);
    }
#endif
#if defined _i_has_eq || defined _i_has_cmp
    STC_INLINE bool _cx_MEMB(_raw_eq)(const _cx_raw* rx, const _cx_raw* ry)
        { return i_eq(rx, ry); }

    STC_INLINE bool _cx_MEMB(_eq)(const _cx_Self* self, const _cx_Self* other) {
        _cx_raw rx = i_keyto(self->get), ry = i_keyto(other->get);
        return i_eq((&rx), (&ry));
    }
#else
    STC_INLINE bool _cx_MEMB(_raw_eq)(const _cx_raw* rx, const _cx_raw* ry)
        { return rx == ry; }

    STC_INLINE bool _cx_MEMB(_eq)(const _cx_Self* self, const _cx_Self* other) {
        return self->get == other->get;
    }
#endif
#if defined i_hash
    STC_INLINE uint64_t _cx_MEMB(_raw_hash)(const _cx_raw* rx)
        { return i_hash(rx); }

    STC_INLINE uint64_t _cx_MEMB(_hash)(const _cx_Self* self)
        { _cx_raw rx = i_keyto(self->get); return i_hash(&rx); }
#else
    STC_INLINE uint64_t _cx_MEMB(_raw_hash)(const _cx_raw* rx)
        { return c_default_hash(&rx); }

    STC_INLINE uint64_t _cx_MEMB(_hash)(const _cx_Self* self)
        { return c_default_hash(&self->get); }
#endif

#undef _i_atomic_inc
#undef _i_atomic_dec_and_test
#include "priv/template2.h"
#undef _i_carc