From 42612cd91fda97600a39dce821b7c62d1bb71b27 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Mon, 8 Nov 2021 07:29:56 +0100 Subject: Added c_make_ptr(T, value) macro. --- include/stc/ccommon.h | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index 6e6aefd2..70152175 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -74,13 +74,16 @@ ((type *)((char *)(ptr) - offsetof(type, member))) #if __cplusplus -#define c_new(T) static_cast(c_malloc(sizeof(T))) -#define c_new_n(T, n) static_cast(c_malloc(sizeof(T)*(n))) -#define c_make(T) T +# include +# define c_new(T) static_cast(c_malloc(sizeof(T))) +# define c_new_n(T, n) static_cast(c_malloc(sizeof(T)*(n))) +# define c_make(T) T +# define c_make_ptr(T, value) new (c_new(T)) T{value} #else -#define c_new(T) c_malloc(sizeof(T)) -#define c_new_n(T, n) c_malloc(sizeof(T)*(n)) -#define c_make(T) (T) +# define c_new(T) c_malloc(sizeof(T)) +# define c_new_n(T, n) c_malloc(sizeof(T)*(n)) +# define c_make(T) (T) +# define c_make_ptr(T, value) memcpy(c_new(T), &(T){value}, sizeof(T)) #endif #ifndef c_malloc #define c_malloc(sz) malloc(sz) -- cgit v1.2.3