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
|
/* MIT License
*
* Copyright (c) 2021 Tyge Løvset, NORCE, www.norceresearch.no
*
* 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.
*/
/* csptr: shared_ptr type
#include <stc/cstr.h>
typedef struct { cstr name, last; } Person;
Person Person_new(const char* name, const char* last) {
return (Person){.name = cstr_from(name), .last = cstr_from(last)};
}
void Person_drop(Person* p) {
printf("drop: %s %s\n", p->name.str, p->last.str);
c_drop(cstr, &p->name, &p->last);
}
#define i_tag person
#define i_val Person
#define i_valdrop Person_drop
#include <stc/csptr.h>
int main() {
csptr_person p = csptr_person_new(Person_new("John", "Smiths"));
csptr_person q = csptr_person_clone(p); // share the pointer
printf("%s %s. uses: %zu\n", q.get->name.str, q.get->last.str, *q.use_count);
c_drop(csptr_person, &p, &q);
}
*/
#ifndef CSPTR_H_INCLUDED
#define CSPTR_H_INCLUDED
#include "ccommon.h"
#include "forward.h"
#include <stdlib.h>
#if defined(__GNUC__) || defined(__clang__)
#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>
#define c_atomic_inc(v) (void)_InterlockedIncrement(v)
#define c_atomic_dec_and_test(v) !_InterlockedDecrement(v)
#else
#include <stdatomic.h>
#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 csptr_null {NULL, NULL}
#define _cx_csptr_rep struct _cx_memb(_rep_)
#endif // CSPTR_H_INCLUDED
#ifndef _i_prefix
#define _i_prefix csptr_
#endif
#define _i_has_internal_clone
#include "template.h"
typedef i_valraw _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
#if !c_option(c_is_fwd)
_cx_deftypes(_c_csptr_types, _cx_self, i_val);
#endif
_cx_csptr_rep { long counter; i_val value; };
STC_INLINE _cx_self
_cx_memb(_init)(void) { return c_make(_cx_self){NULL, NULL}; }
STC_INLINE long
_cx_memb(_use_count)(_cx_self ptr) { return ptr.use_count ? *ptr.use_count : 0; }
STC_INLINE _cx_self
_cx_memb(_from_ptr)(_cx_value* p) {
_cx_self ptr = {p};
if (p) *(ptr.use_count = c_alloc(long)) = 1;
return ptr;
}
STC_INLINE _cx_self
_cx_memb(_from)(i_val val) {
_cx_self ptr; _cx_csptr_rep *rep = c_alloc(_cx_csptr_rep);
*(ptr.use_count = &rep->counter) = 1;
*(ptr.get = &rep->value) = val;
return ptr;
}
STC_INLINE i_val _cx_memb(_toraw)(const _cx_self* self) {
return *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_valdrop(self->get);
if (self->get != &((_cx_csptr_rep *)self->use_count)->value)
c_free(self->get);
c_free(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_from)(_cx_self* self, i_val val) {
_cx_memb(_drop)(self);
*self = _cx_memb(_from)(val);
}
#if !c_option(c_no_clone) && !defined _i_valraw_default
STC_INLINE _cx_self _cx_memb(_new)(i_valraw raw) {
return _cx_memb(_from)(i_valfrom(raw));
}
#endif
// does not use i_valfrom, so we can bypass c_no_clone
STC_INLINE _cx_self
_cx_memb(_clone)(_cx_self ptr) {
if (ptr.use_count) _i_atomic_inc(ptr.use_count);
return ptr;
}
STC_INLINE void
_cx_memb(_copy)(_cx_self* self, _cx_self ptr) {
if (ptr.use_count) _i_atomic_inc(ptr.use_count);
_cx_memb(_drop)(self);
*self = ptr;
}
STC_INLINE void
_cx_memb(_take)(_cx_self* self, _cx_self ptr) {
if (self->get != ptr.get) _cx_memb(_drop)(self);
*self = ptr;
}
STC_INLINE uint64_t
_cx_memb(_value_hash)(const _cx_value* x, size_t n) {
#if c_option(c_no_cmp) && UINTPTR_MAX == UINT64_MAX
return c_hash64(&x, 8);
#elif c_option(c_no_cmp)
return c_hash32(&x, 4);
#else
i_valraw rx = i_valto(x);
return i_hash(&rx, sizeof rx);
#endif
}
STC_INLINE int
_cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y) {
#if c_option(c_no_cmp)
return c_default_cmp(&x, &y);
#else
i_valraw rx = i_valto(x), ry = i_valto(x);
return i_cmp(&rx, &ry);
#endif
}
STC_INLINE bool
_cx_memb(_value_eq)(const _cx_value* x, const _cx_value* y) {
return !_cx_memb(_value_cmp)(x, y);
}
#undef _i_atomic_inc
#undef _i_atomic_dec_and_test
#include "template.h"
|