Skip to content

Commit

Permalink
Changed API of arc_X_assign().
Browse files Browse the repository at this point in the history
  • Loading branch information
tylov committed Oct 28, 2024
1 parent 4dbbdf2 commit 4bf06a4
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 14 deletions.
2 changes: 1 addition & 1 deletion docs/arc_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ arc_X arc_X_from_ptr(i_key* p); // create an arc
arc_X arc_X_make(i_key key); // create an arc from constructed key object. Faster than from_ptr().
arc_X arc_X_clone(arc_X other); // return other with increased use count
void arc_X_assign(arc_X* self, arc_X other); // shared assign (increases use count)
void arc_X_assign(arc_X* self, const arc_X* other); // shared assign (increases use count)
void arc_X_take(arc_X* self, arc_X unowned); // take ownership of unowned.
arc_X arc_X_move(arc_X* self); // transfer ownership to receiver; self becomes NULL
void arc_X_drop(arc_X* self); // destruct (decrease use count, free at 0)
Expand Down
7 changes: 4 additions & 3 deletions docs/box_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@ box_X box_X_from_ptr(i_key* ptr); // create a box
box_X box_X_make(i_key val); // create a box from unowned val object.
box_X box_X_clone(box_X other); // return deep copied clone
void box_X_assign(box_X* self, box_X* moved); // transfer ownership from moved to self; moved becomes NULL.
void box_X_assign(box_X* self, box_X* other); // transfer ownership from other to self; other set to NULL.
void box_X_take(box_X* self, box_X unowned); // take ownership of unowned box object.
box_X box_X_move(box_X* self); // transfer ownership to receiving box returned. self becomes NULL.
box_X box_X_move(box_X* self); // transfer ownership to receiving box. self set to NULL.
i_key* box_X_release(box_X* self); // release owned pointer; must be freed by receiver. self set NULL.
void box_X_drop(box_X* self); // destruct the contained object and free its heap memory.
void box_X_reset_to(box_X* self, i_key* p); // assign new box from ptr. Takes ownership of p.
void box_X_reset_to(box_X* self, i_key* ptr); // assign ptr, and take ownership of ptr.
size_t box_X_hash(const box_X* x); // hash value
int box_X_cmp(const box_X* x, const box_X* y); // compares pointer addresses if no `i_cmp` is specified
Expand Down
6 changes: 3 additions & 3 deletions include/stc/arc.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@ STC_INLINE void _c_MEMB(_take)(Self* self, Self unowned) {
}

// make shared ownership with owned arc
STC_INLINE void _c_MEMB(_assign)(Self* self, Self owned) {
if (owned.use_count) _i_atomic_inc(owned.use_count);
STC_INLINE void _c_MEMB(_assign)(Self* self, const Self* owned) {
if (owned->use_count) _i_atomic_inc(owned->use_count);
_c_MEMB(_drop)(self);
*self = owned;
*self = *owned;
}

// clone by sharing. Does not use i_keyclone, so OK to always define.
Expand Down
14 changes: 7 additions & 7 deletions misc/tests/vec_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ TEST(vec, basics) {
c_forrange32 (i, 2, 9)
IVec_push_back(&d, i*10);

IVec res1 = c_init(IVec, {6, 7, 8, 9, 10, 11, 12, 20, 30, 40, 50, 60, 70, 80});
EXPECT_TRUE(IVec_eq(&res1, &d));
IVec res = c_init(IVec, {6, 7, 8, 9, 10, 11, 12, 20, 30, 40, 50, 60, 70, 80});
EXPECT_TRUE(IVec_eq(&res, &d));
EXPECT_EQ(14, IVec_size(&d));

IVec_erase_n(&d, 7, 4);

IVec res2 = c_init(IVec, {6, 7, 8, 9, 10, 11, 12, 60, 70, 80});
EXPECT_TRUE(IVec_eq(&res2, &d));
IVec_take(&res, c_init(IVec, {6, 7, 8, 9, 10, 11, 12, 60, 70, 80}));
EXPECT_TRUE(IVec_eq(&res, &d));

int nums[] = {200, 300, 400, 500};
IVec_insert_n(&d, 7, nums, 4);

IVec res3 = c_init(IVec, {6, 7, 8, 9, 10, 11, 12, 200, 300, 400, 500, 60, 70, 80});
EXPECT_TRUE(IVec_eq(&res3, &d));
IVec_take(&res, c_init(IVec, {6, 7, 8, 9, 10, 11, 12, 200, 300, 400, 500, 60, 70, 80}));
EXPECT_TRUE(IVec_eq(&res, &d));

EXPECT_EQ(14, IVec_size(&d));
EXPECT_EQ(200, *IVec_at(&d, 7));

c_drop(IVec, &d, &res1, &res2, &res3);
c_drop(IVec, &d, &res);
}

0 comments on commit 4bf06a4

Please sign in to comment.