Fix setptr to work in an 'if' clause & improve documentation

Use do {...} while (0) instead of {...}.
Document the order of events and purpose of setptr properly.
This commit is contained in:
Nick Treleaven 2011-11-15 13:19:27 +00:00
parent 2e56a84b2a
commit 736c398467

View File

@ -35,17 +35,16 @@
#define NZV(ptr) \
((ptr) && (ptr)[0])
/**
* Frees @a ptr (if not @c NULL), then assigns @a result to it.
* @a result can be an expression using the 'old' value of @a ptr.
* It prevents a memory leak compared with: @code ptr = func(ptr); @endcode
/** Assigns @a result to @a ptr, then frees the old value.
* @a result can be an expression using the 'old' value of @a ptr.
* E.g. @code setptr(str, g_strndup(str, 5)); @endcode
**/
#define setptr(ptr, result) \
{\
do {\
gpointer setptr_tmp = ptr;\
ptr = result;\
g_free(setptr_tmp);\
}
} while (0)
/** Duplicates a string on the stack using @c g_alloca().
* Like glibc's @c strdupa(), but portable.