From 8b6d127bb14b9ad7ef28da1fc5f8c965df2123cd Mon Sep 17 00:00:00 2001 From: Patrick Venture Date: Wed, 5 Dec 2018 08:55:59 -0800 Subject: [PATCH] helper: add free_n method to handle clearing pointers free_n() will free the memory and clear the pointer, which will reduce the probability a developer will forget to clear the pointer after freeing. Resolves: #79 Signed-off-by: Patrick Venture --- include/ipmitool/helper.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/include/ipmitool/helper.h b/include/ipmitool/helper.h index ed8e950..ad746e7 100644 --- a/include/ipmitool/helper.h +++ b/include/ipmitool/helper.h @@ -119,6 +119,17 @@ uint16_t ipmi_get_oem_id(struct ipmi_intf *intf); #define IS_SET(v, b) ((v) & (1 << (b))) +/** + * Free the memory and clear the pointer. + * @param[in] ptr - a pointer to your pointer to free. + */ +static inline void free_n(void **ptr) { + if (ptr && *ptr) { + free(*ptr); + *ptr = NULL; + } +} + /* le16toh(), hto16le(), et. al. don't exist for Windows or Apple */ /* For portability, let's simply define our own versions here */