diff options
| author | Tyge Lovset <[email protected]> | 2022-08-12 11:38:10 +0200 |
|---|---|---|
| committer | Tyge Lovset <[email protected]> | 2022-08-12 11:38:10 +0200 |
| commit | a417e5affc99233abb6dbb685154bfdea1b726e3 (patch) | |
| tree | 211f1e1c000d57ea7e5ca318c625996ce094dc76 /include | |
| parent | f534db7ac4a993a05074868b8840a3a674ac76b4 (diff) | |
| download | STC-modified-a417e5affc99233abb6dbb685154bfdea1b726e3.tar.gz STC-modified-a417e5affc99233abb6dbb685154bfdea1b726e3.zip | |
More misc changes carc/cbox, cdeq/cvec.
Diffstat (limited to 'include')
| -rw-r--r-- | include/stc/carc.h | 22 | ||||
| -rw-r--r-- | include/stc/cbox.h | 16 | ||||
| -rw-r--r-- | include/stc/cdeq.h | 29 | ||||
| -rw-r--r-- | include/stc/cvec.h | 17 | ||||
| -rw-r--r-- | include/stc/forward.h | 2 | ||||
| -rw-r--r-- | include/stc/template.h | 8 |
6 files changed, 45 insertions, 49 deletions
diff --git a/include/stc/carc.h b/include/stc/carc.h index 20ba26b0..6e8f7d38 100644 --- a/include/stc/carc.h +++ b/include/stc/carc.h @@ -56,14 +56,17 @@ int main() { #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 @@ -88,18 +91,18 @@ typedef i_keyraw _cx_raw; #if !c_option(c_is_fwd) _cx_deftypes(_c_carc_types, _cx_self, i_key); #endif -_cx_carc_rep { long counter; i_key value; }; +_cx_carc_rep { catomic_long counter; i_key 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 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 = c_alloc(long)) = 1; + *(ptr.use_count = c_alloc(catomic_long)) = 1; return ptr; } @@ -111,16 +114,13 @@ STC_INLINE _cx_self _cx_memb(_from)(_cx_value val) { *(ptr.get = &rep->value) = val; return ptr; } -// [deprecated] -STC_INLINE _cx_self _cx_memb(_make)(_cx_value val) + +STC_INLINE _cx_self _cx_memb(_make)(_cx_value val) // [deprecated] { return _cx_memb(_from)(val); } STC_INLINE _cx_raw _cx_memb(_toraw)(const _cx_self* self) { return i_keyto(self->get); } -STC_INLINE _cx_value _cx_memb(_toval)(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; @@ -132,7 +132,7 @@ STC_INLINE void _cx_memb(_drop)(_cx_self* self) { i_keydrop(self->get); if ((char *)self->get != (char *)self->use_count + offsetof(_cx_carc_rep, value)) c_free(self->get); - c_free(self->use_count); + c_free((long*)self->use_count); } } @@ -158,7 +158,7 @@ STC_INLINE _cx_self _cx_memb(_clone)(_cx_self ptr) { return ptr; } -STC_INLINE void _cx_memb(_assign)(_cx_self* self, _cx_self 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); diff --git a/include/stc/cbox.h b/include/stc/cbox.h index d26a63c8..02a73fdc 100644 --- a/include/stc/cbox.h +++ b/include/stc/cbox.h @@ -82,8 +82,8 @@ _cx_deftypes(_c_cbox_types, _cx_self, i_key); STC_INLINE _cx_self _cx_memb(_init)(void) { return c_make(_cx_self){NULL}; } -STC_INLINE long _cx_memb(_use_count)(_cx_self box) - { return (long)(box.get != NULL); } +STC_INLINE long _cx_memb(_use_count)(const _cx_self* self) + { return (long)(self->get != NULL); } STC_INLINE _cx_self _cx_memb(_from_ptr)(_cx_value* p) { return c_make(_cx_self){p}; } @@ -100,9 +100,6 @@ STC_INLINE _cx_self _cx_memb(_make)(_cx_value val) STC_INLINE _cx_raw _cx_memb(_toraw)(const _cx_self* self) { return i_keyto(self->get); } -STC_INLINE _cx_value _cx_memb(_toval)(const _cx_self* self) - { return *self->get; } - // destructor STC_INLINE void _cx_memb(_drop)(_cx_self* self) { if (self->get) { @@ -129,11 +126,11 @@ STC_INLINE void _cx_memb(_reset_to)(_cx_self* self, _cx_value* p) { self->get = p; } -#if !defined _i_no_clone #if !defined _i_no_emplace STC_INLINE _cx_self _cx_memb(_new)(_cx_raw raw) { return _cx_memb(_from)(i_keyfrom(raw)); } #endif +#if !defined _i_no_clone STC_INLINE _cx_self _cx_memb(_clone)(_cx_self other) { if (!other.get) return other; @@ -141,13 +138,6 @@ STC_INLINE void _cx_memb(_reset_to)(_cx_self* self, _cx_value* p) { *out.get = i_keyclone(*other.get); return out; } - - STC_INLINE void _cx_memb(_assign)(_cx_self* self, const _cx_self ptr) { - if (self->get == ptr.get) - return; - _cx_memb(_drop)(self); - *self = _cx_memb(_clone)(ptr); - } #endif // !_i_no_clone STC_INLINE void _cx_memb(_take)(_cx_self* self, _cx_self other) { diff --git a/include/stc/cdeq.h b/include/stc/cdeq.h index afe26f4b..ef6c9f26 100644 --- a/include/stc/cdeq.h +++ b/include/stc/cdeq.h @@ -48,15 +48,12 @@ STC_API void _cx_memb(_clear)(_cx_self* self); STC_API void _cx_memb(_drop)(_cx_self* self); STC_API _cx_value* _cx_memb(_push)(_cx_self* self, i_key value); STC_API void _cx_memb(_shrink_to_fit)(_cx_self *self); + #if !defined _i_queue #if !defined _i_no_emplace STC_API _cx_iter _cx_memb(_emplace_range_p)(_cx_self* self, _cx_value* pos, const _cx_raw* p1, const _cx_raw* p2); #endif // _i_no_emplace -#if !defined _i_no_clone -STC_API _cx_iter _cx_memb(_clone_range_p)(_cx_self* self, _cx_value* pos, - const _cx_value* p1, const _cx_value* p2); -#endif // !_i_no_clone #if !c_option(c_no_cmp) STC_API _cx_iter _cx_memb(_find_in)(_cx_iter p1, _cx_iter p2, _cx_raw raw); @@ -68,19 +65,26 @@ STC_API _cx_iter _cx_memb(_insert_range_p)(_cx_self* self, _cx_value* pos const _cx_value* p1, const _cx_value* p2); #endif // !_i_queue -#if !defined _i_no_clone -STC_API _cx_self _cx_memb(_clone)(_cx_self cx); #if !defined _i_no_emplace STC_INLINE _cx_value* _cx_memb(_emplace)(_cx_self* self, _cx_raw raw) { return _cx_memb(_push)(self, i_keyfrom(raw)); } #endif -STC_INLINE i_key _cx_memb(_value_clone)(i_key val) - { return i_keyclone(val); } + +#if !defined _i_no_clone +#if !defined _i_queue +STC_API _cx_iter _cx_memb(_copy_range_p)(_cx_self* self, _cx_value* pos, + const _cx_value* p1, const _cx_value* p2); + STC_INLINE void _cx_memb(_copy)(_cx_self *self, const _cx_self* other) { if (self->data == other->data) return; - _cx_memb(_drop)(self); - *self = _cx_memb(_clone)(*other); + _cx_memb(_clear)(self); + _cx_memb(_copy_range_p)(self, self->data, other->data, + other->data + cdeq_rep_(other)->size); } +#endif // !_i_queue +STC_API _cx_self _cx_memb(_clone)(_cx_self cx); +STC_INLINE i_key _cx_memb(_value_clone)(i_key val) + { return i_keyclone(val); } #endif // !_i_no_clone STC_INLINE size_t _cx_memb(_size)(const _cx_self* cx) { return cdeq_rep_(cx)->size; } STC_INLINE size_t _cx_memb(_capacity)(const _cx_self* cx) { return cdeq_rep_(cx)->cap; } @@ -222,6 +226,7 @@ _cx_memb(_clear)(_cx_self* self) { --q; i_keydrop(q); } rep->size = 0; + self->data = self->_base; } } @@ -409,8 +414,8 @@ _cx_memb(_emplace_range_p)(_cx_self* self, _cx_value* pos, const _cx_raw* p1, co #if !defined _i_no_clone STC_DEF _cx_iter -_cx_memb(_clone_range_p)(_cx_self* self, _cx_value* pos, - const _cx_value* p1, const _cx_value* p2) { +_cx_memb(_copy_range_p)(_cx_self* self, _cx_value* pos, + const _cx_value* p1, const _cx_value* p2) { _cx_iter it = _cx_memb(_insert_uninit_p)(self, pos, p2 - p1); if (it.ref) for (_cx_iter j; p1 != p2; ++p1) *j.ref++ = i_keyclone((*p1)); diff --git a/include/stc/cvec.h b/include/stc/cvec.h index 371d6a29..a7828708 100644 --- a/include/stc/cvec.h +++ b/include/stc/cvec.h @@ -112,14 +112,15 @@ _cx_memb(_emplace_at)(_cx_self* self, _cx_iter it, _cx_raw raw) { #if !defined _i_no_clone STC_API _cx_self _cx_memb(_clone)(_cx_self cx); -STC_API _cx_iter _cx_memb(_clone_range_p)(_cx_self* self, _cx_value* pos, - const _cx_value* p1, const _cx_value* p2); +STC_API _cx_iter _cx_memb(_copy_range_p)(_cx_self* self, _cx_value* pos, + const _cx_value* p1, const _cx_value* p2); STC_INLINE i_key _cx_memb(_value_clone)(_cx_value val) { return i_keyclone(val); } STC_INLINE void _cx_memb(_copy)(_cx_self* self, const _cx_self* other) { if (self->data == other->data) return; - _cx_memb(_drop)(self); - *self = _cx_memb(_clone)(*other); + _cx_memb(_clear)(self); + _cx_memb(_copy_range_p)(self, self->data, other->data, + other->data + cvec_rep_(other)->size); } #endif // !_i_no_clone @@ -372,19 +373,20 @@ _cx_memb(_clone)(_cx_self cx) { const size_t len = cvec_rep_(&cx)->size; _cx_self out = _cx_memb(_with_capacity)(len); if (cvec_rep_(&out)->cap) - _cx_memb(_clone_range_p)(&out, out.data, cx.data, cx.data + len); + _cx_memb(_copy_range_p)(&out, out.data, cx.data, cx.data + len); return out; } STC_DEF _cx_iter -_cx_memb(_clone_range_p)(_cx_self* self, _cx_value* pos, - const _cx_value* p1, const _cx_value* p2) { +_cx_memb(_copy_range_p)(_cx_self* self, _cx_value* pos, + const _cx_value* p1, const _cx_value* p2) { _cx_iter it = _cx_memb(_insert_uninit_p)(self, pos, p2 - p1); if (it.ref) for (_cx_iter j = it; p1 != p2; ++p1) *j.ref++ = i_keyclone((*p1)); return it; } +#endif // !_i_no_clone #if !defined _i_no_emplace STC_DEF _cx_iter @@ -397,7 +399,6 @@ _cx_memb(_emplace_range_p)(_cx_self* self, _cx_value* pos, return it; } #endif // !_i_no_emplace -#endif // !_i_no_clone #if !c_option(c_no_cmp) STC_DEF _cx_iter diff --git a/include/stc/forward.h b/include/stc/forward.h index 8a0278a5..264d6939 100644 --- a/include/stc/forward.h +++ b/include/stc/forward.h @@ -68,7 +68,7 @@ typedef union { \ typedef struct { \ SELF##_value* get; \ - long* use_count; \ + catomic_long* use_count; \ } SELF #define _c_carr2_types(SELF, VAL) \ diff --git a/include/stc/template.h b/include/stc/template.h index eeccc39e..4597e02c 100644 --- a/include/stc/template.h +++ b/include/stc/template.h @@ -109,9 +109,9 @@ #endif #elif defined i_key_arcbox #define i_key_bind i_key_arcbox - #define i_keyfrom c_paste(i_key_arcbox, _from) #define i_keyraw c_paste(i_key_arcbox, _value) - #define i_keyto c_paste(i_key_arcbox, _toval) + #define i_keyfrom c_paste(i_key_arcbox, _from) + #define i_keyto(x) *(x)->get #define i_eq c_paste(i_key_arcbox, _value_eq) #endif @@ -200,9 +200,9 @@ #define i_valto cstr_sv #elif defined i_val_arcbox #define i_val_bind i_val_arcbox - #define i_valfrom c_paste(i_val_arcbox, _from) #define i_valraw c_paste(i_val_arcbox, _value) - #define i_valto c_paste(i_val_arcbox, _toval) + #define i_valfrom c_paste(i_val_arcbox, _from) + #define i_valto(x) *(x)->get #endif #ifdef i_val_bind |
