diff options
| -rw-r--r-- | docs/carc_api.md | 10 | ||||
| -rw-r--r-- | docs/cbox_api.md | 14 | ||||
| -rw-r--r-- | include/stc/carc.h | 8 | ||||
| -rw-r--r-- | include/stc/cbox.h | 14 |
4 files changed, 27 insertions, 19 deletions
diff --git a/docs/carc_api.md b/docs/carc_api.md index 657d7257..f91975a7 100644 --- a/docs/carc_api.md +++ b/docs/carc_api.md @@ -36,14 +36,14 @@ See similar c++ class [std::shared_ptr](https://en.cppreference.com/w/cpp/memory ## Methods ```c carc_X carc_X_init(); // empty shared pointer -carc_X carc_X_new(i_valraw raw); // create an carc from raw type (available if i_valraw defined by user). -carc_X carc_X_from(i_val val); // create an carc from constructed val object. Faster than from_ptr(). +carc_X carc_X_from(i_valraw raw); // create an carc from raw type (available if i_valraw defined by user). carc_X carc_X_from_ptr(i_val* p); // create an carc from raw pointer. Takes ownership of p. +carc_X carc_X_make(i_val val); // create an carc from constructed val object. Faster than from_ptr(). carc_X carc_X_clone(carc_X other); // return other with increased use count -carc_X carc_X_move(carc_X* self); // transfer ownership to another carc. -void carc_X_take(carc_X* self, carc_X other); // take ownership of other. -void carc_X_copy(carc_X* self, carc_X other); // shared assign (increase use count) +carc_X carc_X_move(carc_X* self); // transfer ownership to receiver; self becomes NULL +void carc_X_take(carc_X* self, carc_X unowned); // take ownership of unowned. +void carc_X_assign(carc_X* self, carc_X other); // shared assign (increases use count) void carc_X_drop(carc_X* self); // destruct (decrease use count, free at 0) long carc_X_use_count(const carc_X* self); diff --git a/docs/cbox_api.md b/docs/cbox_api.md index 5f936b1c..8906f154 100644 --- a/docs/cbox_api.md +++ b/docs/cbox_api.md @@ -35,15 +35,15 @@ compare the pointer addresses when used. Additionally, `c_no_clone` or `i_is_fwd ## Methods ```c cbox_X cbox_X_init(); // return an empty cbox -cbox_X cbox_X_new(i_valraw raw); // create a cbox from raw type. Avail if i_valraw user defined. -cbox_X cbox_X_from(i_val val); // create a cbox from constructed val object. -cbox_X cbox_X_from_ptr(i_val* p); // create a cbox from a pointer. Takes ownership of p. +cbox_X cbox_X_from(i_valraw raw); // create a cbox from raw type. Avail if i_valraw user defined. +cbox_X cbox_X_from_ptr(i_val* ptr); // create a cbox from a pointer. Takes ownership of ptr. +cbox_X cbox_X_make(i_val val); // create a cbox from unowned val object. cbox_X cbox_X_clone(cbox_X other); // return deep copied clone -cbox_X cbox_X_move(cbox_X* self); // transfer ownership to another cbox. -void cbox_X_take(cbox_X* self, cbox_X other); // take ownership of other. -void cbox_X_copy(cbox_X* self, cbox_X other); // deep copy to self -void cbox_X_drop(cbox_X* self); // destruct the contained object and free's it. +cbox_X cbox_X_move(cbox_X* self); // transfer ownership to receiving cbox returned. self becomes NULL. +void cbox_X_take(cbox_X* self, cbox_X unowned); // take ownership of unowned box object. +void cbox_X_assign(cbox_X* self, cbox_X* dying); // transfer ownership from dying to self; dying becomes NULL. +void cbox_X_drop(cbox_X* self); // destruct the contained object and free its heap memory. void cbox_X_reset(cbox_X* self); void cbox_X_reset_to(cbox_X* self, i_val* p); // assign new cbox from ptr. Takes ownership of p. diff --git a/include/stc/carc.h b/include/stc/carc.h index 7f7789ab..f14fdd65 100644 --- a/include/stc/carc.h +++ b/include/stc/carc.h @@ -157,17 +157,17 @@ STC_INLINE _cx_self _cx_memb(_clone)(_cx_self ptr) { return ptr; } -STC_INLINE void _cx_memb(_copy)(_cx_self* self, _cx_self 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; } -STC_INLINE void _cx_memb(_take)(_cx_self* self, _cx_self ptr) { - if (self->get != ptr.get) +STC_INLINE void _cx_memb(_take)(_cx_self* self, _cx_self unowned) { + if (self->get != unowned.get) _cx_memb(_drop)(self); - *self = ptr; + *self = unowned; } #ifndef i_no_cmp diff --git a/include/stc/cbox.h b/include/stc/cbox.h index a0966fcc..b8a61375 100644 --- a/include/stc/cbox.h +++ b/include/stc/cbox.h @@ -142,10 +142,18 @@ STC_INLINE _cx_self _cx_memb(_from)(_cx_value val) } #endif // !i_no_clone -STC_INLINE void _cx_memb(_take)(_cx_self* self, _cx_self other) { - if (other.get != self->get) +STC_INLINE void _cx_memb(_take)(_cx_self* self, _cx_self unowned) { + if (unowned.get != self->get) _cx_memb(_drop)(self); - *self = other; + *self = unowned; +} +/* transfer ownership; set dying to NULL */ +STC_INLINE void _cx_memb(_assign)(_cx_self* self, _cx_self* dying) { + if (dying->get == self->get) + return; + _cx_memb(_drop)(self); + *self = *dying; + dying->get = NULL; } #ifndef i_no_cmp |
