Skip to content

Commit 287664d

Browse files
committed
Added smart array alternative macro for nontrivial types
1 parent 44e6737 commit 287664d

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

include/csptr/smart_ptr.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,30 @@ inline void sfree_stack(void *ptr) {
6262
var; \
6363
})
6464

65+
# define smart_arr(Kind, Type, Length, Args...) \
66+
({ \
67+
struct s_tmp { \
68+
int sentinel_; \
69+
__typeof__(__typeof__(Type)[Length]) value; \
70+
f_destructor dtor; \
71+
struct { \
72+
const void *ptr; \
73+
size_t size; \
74+
} meta; \
75+
} args = { \
76+
.sentinel_ = 0, \
77+
Args \
78+
}; \
79+
void *var = smalloc(sizeof (Type), Length, Kind, ARGS_); \
80+
if (var != NULL) \
81+
memcpy(var, &args.value, sizeof (Type)); \
82+
var; \
83+
})
84+
6585
# define shared_ptr(Type, Args...) smart_ptr(SHARED, Type, Args)
6686
# define unique_ptr(Type, Args...) smart_ptr(UNIQUE, Type, Args)
6787

88+
# define shared_arr(Type, Length, Args...) smart_arr(SHARED, Type, Length, Args)
89+
# define unique_arr(Type, Length, Args...) smart_arr(UNIQUE, Type, Length, Args)
90+
6891
#endif /* !CSPTR_SMART_PTR_H_ */

0 commit comments

Comments
 (0)