X-Git-Url: http://git.jankratochvil.net/?a=blobdiff_plain;f=src%2Flibcaptive%2Finclude%2Fcaptive%2Fmacros.h;h=8292a8f76b0e7ab4e67bd578f7ddf9cce194b2c1;hb=a2dd38f86df22c46ae18f3ad7d9850eaacb02b92;hp=0259c49129d5015c1865ebb9940537df1315f2fa;hpb=bd6d8b64ccb401297a712e89073dded0084f88b5;p=captive.git diff --git a/src/libcaptive/include/captive/macros.h b/src/libcaptive/include/captive/macros.h index 0259c49..8292a8f 100644 --- a/src/libcaptive/include/captive/macros.h +++ b/src/libcaptive/include/captive/macros.h @@ -206,6 +206,28 @@ G_BEGIN_DECLS /** + * captive_memdup: + * @destp: Variable with the pointer to the target object wished to be allocated. + * Original value is discarded. + * @srcp: Pointer to the source object to be copied to @destp. + * + * Macro to similiar to g_memdup() but the object size is detected automatically. + * Size of @destp object and @srcp object must be the same. + * + * You must free the allocated memory of @destp by g_free(). + * + * @Returns: Initialized @destp value as the copied memory of size #sizeof(typeof(*srcp)). + */ +#define captive_memdup(destp,srcp) ({ \ + typeof(&(destp)) _captive_captive_memdup_destpp=&(destp); \ + g_assert(sizeof(*(destp))==sizeof(*(srcp))); \ + captive_new(*_captive_captive_memdup_destpp); \ + memcpy(*_captive_captive_memdup_destpp,(srcp),sizeof(*(srcp))); \ + (*_captive_captive_memdup_destpp); \ + }) + + +/** * captive_va_arg: * @objp: Variable to be filled from the next argument of @ap. * @ap: Initialized #va_list type. @@ -272,9 +294,9 @@ gsize r; */ #define captive_strdup_alloca(string) ({ \ const gchar *_captive_strdup_alloca_string=(string); \ - gchar *_captive_strdup_alloca_r=g_alloca(strlen(_captive_strdup_alloca_string)+1); \ - strcpy(_captive_strdup_alloca_r,_captive_strdup_alloca_string); \ - (const gchar *)(_captive_strdup_alloca_string); \ + const gchar *_captive_strdup_alloca_r=g_alloca(strlen(_captive_strdup_alloca_string)+1); \ + strcpy((gchar *)_captive_strdup_alloca_r,_captive_strdup_alloca_string); \ + (const gchar *)(_captive_strdup_alloca_r); \ })