From f217a83c018fb2d912bc0f39754c3b47f6c49855 Mon Sep 17 00:00:00 2001 From: wulei Date: Thu, 10 Mar 2022 20:42:23 +0800 Subject: [PATCH 1/3] get the correct pointer to user's meta in the smart-array --- src/mman.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mman.c b/src/mman.c index 5df136c..ad6c3e8 100644 --- a/src/mman.c +++ b/src/mman.c @@ -136,7 +136,7 @@ CSPTR_INLINE static void dealloc_entry(s_meta *meta, void *ptr) { if (meta->kind & ARRAY) { s_meta_array *arr_meta = (void *) (meta + 1); for (size_t i = 0; i < arr_meta->nmemb; ++i) - meta->dtor((char *) ptr + arr_meta->size * i, user_meta); + meta->dtor((char *) ptr + arr_meta->size * i, arr_meta + 1); } else meta->dtor(ptr, user_meta); } From 2021450671cfb356a1a2fcae6d435a238803dd85 Mon Sep 17 00:00:00 2001 From: wulei Date: Thu, 10 Mar 2022 20:57:35 +0800 Subject: [PATCH 2/3] copy array elements to a smart_arr --- include/csptr/smart_ptr.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/csptr/smart_ptr.h b/include/csptr/smart_ptr.h index 8087b99..272a3cd 100644 --- a/include/csptr/smart_ptr.h +++ b/include/csptr/smart_ptr.h @@ -70,7 +70,7 @@ CSPTR_INLINE void sfree_stack(void *ptr) { ({ \ struct s_tmp { \ CSPTR_SENTINEL_DEC \ - __typeof__(__typeof__(Type)[Length]) value; \ + __typeof__(Type) *value; \ f_destructor dtor; \ struct { \ const void *ptr; \ @@ -82,7 +82,7 @@ CSPTR_INLINE void sfree_stack(void *ptr) { }; \ void *var = smalloc(sizeof (Type), Length, Kind, ARGS_); \ if (var != NULL) \ - memcpy(var, &args.value, sizeof (Type)); \ + memcpy(var, args.value, sizeof (Type) * Length); \ var; \ }) From a9f542036869bab36ce2b4851110f269569e9607 Mon Sep 17 00:00:00 2001 From: wulei Date: Thu, 10 Mar 2022 22:25:48 +0800 Subject: [PATCH 3/3] args value can be absense, initializing an array can be separate from allocating its memory --- include/csptr/smart_ptr.h | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/include/csptr/smart_ptr.h b/include/csptr/smart_ptr.h index 272a3cd..e8c9cf2 100644 --- a/include/csptr/smart_ptr.h +++ b/include/csptr/smart_ptr.h @@ -70,7 +70,7 @@ CSPTR_INLINE void sfree_stack(void *ptr) { ({ \ struct s_tmp { \ CSPTR_SENTINEL_DEC \ - __typeof__(Type) *value; \ + __typeof__(Type) *value; \ f_destructor dtor; \ struct { \ const void *ptr; \ @@ -81,8 +81,12 @@ CSPTR_INLINE void sfree_stack(void *ptr) { __VA_ARGS__ \ }; \ void *var = smalloc(sizeof (Type), Length, Kind, ARGS_); \ - if (var != NULL) \ - memcpy(var, args.value, sizeof (Type) * Length); \ + if (var != NULL) { \ + if (args.value != NULL) \ + memcpy(var, args.value, sizeof (Type) * Length); \ + else \ + memset(var, 0, sizeof(Type) * Length); \ + } \ var; \ })