Initial original import from: fuse-2.4.2-2.fc4
[captive.git] / src / libcaptive / include / captive / macros.h
index 0259c49..8292a8f 100644 (file)
@@ -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); \
                })