summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2021-09-22 12:53:24 +0200
committerTyge Løvset <[email protected]>2021-09-22 12:53:24 +0200
commit7f542d18fedae0e473ed779c2b93479e473b9feb (patch)
tree964541d9d63a7a4fae0457f30815bf113157da1f
parentbc09a3419e12d069a62c020027c7f1f598eb0c30 (diff)
downloadSTC-modified-7f542d18fedae0e473ed779c2b93479e473b9feb.tar.gz
STC-modified-7f542d18fedae0e473ed779c2b93479e473b9feb.zip
Reworked shared pointers, with some smaller API changes.
-rw-r--r--docs/csptr_api.md13
-rw-r--r--include/stc/csptr.h26
2 files changed, 21 insertions, 18 deletions
diff --git a/docs/csptr_api.md b/docs/csptr_api.md
index 95c4f93c..8aa04ed7 100644
--- a/docs/csptr_api.md
+++ b/docs/csptr_api.md
@@ -33,18 +33,19 @@ The *csptr_X_compare()*, *csptr_X_del()* methods are defined based on the `i_cmp
Use *csptr_X_clone(p)* when sharing ownership of the pointed-to object. For shared pointers stored in containers, define `i_val_csptr` to the shared pointers tag instead of a `i_val` macro. See example below.
```c
csptr_X csptr_X_init(); // empty constructor
-csptr_X csptr_X_make(Value val); // make_shared constructor, fast
+csptr_X csptr_X_make(Value val); // make_shared constructor, like std::make_shared()
csptr_X csptr_X_from(Value* p); // construct from raw pointer
-csptr_X csptr_X_clone(csptr_X ptr); // clone shared (increase use count)
-csptr_X csptr_X_move(csptr_X* self); // fast transfer ownership to another sptr.
-csptr_X_value_t* csptr_X_copy(csptr_X* self, csptr_X other); // copy shared (increase use count)
+csptr_X csptr_X_clone(csptr_X ptr); // return ptr with increased use count
+csptr_X csptr_X_move(csptr_X* self); // transfer ownership to another sptr.
+void csptr_X_take(csptr_X* self, csptr_X other); // take a new-created or moved csptr
+void csptr_X_copy(csptr_X* self, csptr_X other); // copy shared (increase use count)
void csptr_X_del(csptr_X* self); // destruct (decrease use count, free at 0)
long csptr_X_use_count(csptr_X ptr);
void csptr_X_reset(csptr_X* self);
-csptr_X_value_t* csptr_X_reset_make(csptr_X* self, Value val); // assign new sptr with value
-csptr_X_value_t* csptr_X_reset_with(csptr_X* self, Value* p); // slower than reset_make().
+void csptr_X_reset_with(csptr_X* self, Value val); // make and assign new csptr with value
+void csptr_X_reset_from(csptr_X* self, Value* p); // create csptr from p.
int csptr_X_compare(const csptr_X* x, const csptr_X* y);
bool csptr_X_equals(const csptr_X* x, const csptr_X* y);
diff --git a/include/stc/csptr.h b/include/stc/csptr.h
index b0857a25..58392bb1 100644
--- a/include/stc/csptr.h
+++ b/include/stc/csptr.h
@@ -91,7 +91,7 @@ typedef long atomic_count_t;
cx_deftypes(_c_csptr_types, Self, i_val);
#endif
#define cx_csptr_rep struct cx_memb(_rep_)
-cx_csptr_rep { atomic_count_t cnt; cx_value_t val; };
+cx_csptr_rep { atomic_count_t counter; cx_value_t value; };
STC_INLINE Self
cx_memb(_init)(void) { return c_make(Self){NULL, NULL}; }
@@ -109,8 +109,8 @@ cx_memb(_from)(cx_value_t* p) {
STC_INLINE Self
cx_memb(_make)(cx_value_t val) {
Self ptr; cx_csptr_rep *rep = c_new(cx_csptr_rep);
- *(ptr.use_count = &rep->cnt) = 1;
- *(ptr.get = &rep->val) = val;
+ *(ptr.use_count = &rep->counter) = 1;
+ *(ptr.get = &rep->value) = val;
return ptr;
}
@@ -131,7 +131,7 @@ STC_INLINE void
cx_memb(_del)(Self* self) {
if (self->use_count && cx_decrement(self->use_count) == 0) {
i_valdel(self->get);
- if (self->get != &((cx_csptr_rep*)self->use_count)->val)
+ if (self->get != &((cx_csptr_rep *)self->use_count)->value)
c_free(self->get);
c_free(self->use_count);
}
@@ -143,25 +143,27 @@ cx_memb(_reset)(Self* self) {
self->use_count = NULL, self->get = NULL;
}
-STC_INLINE cx_value_t*
-cx_memb(_reset_with)(Self* self, cx_value_t* p) {
+STC_INLINE void
+cx_memb(_reset_from)(Self* self, cx_value_t* p) {
cx_memb(_del)(self);
*self = cx_memb(_from)(p);
- return self->get;
}
-STC_INLINE cx_value_t*
-cx_memb(_reset_make)(Self* self, cx_value_t val) {
+STC_INLINE void
+cx_memb(_reset_with)(Self* self, cx_value_t val) {
cx_memb(_del)(self);
*self = cx_memb(_make)(val);
- return self->get;
}
STC_INLINE void
cx_memb(_copy)(Self* self, Self ptr) {
if (ptr.use_count) cx_increment(ptr.use_count);
- cx_memb(_del)(self);
- *self = ptr;
+ cx_memb(_del)(self); *self = ptr;
+}
+
+STC_INLINE void
+cx_memb(_take)(Self* self, Self ptr) {
+ cx_memb(_del)(self); *self = ptr;
}
STC_INLINE int