aboutsummaryrefslogtreecommitdiffstats
path: root/src/libmdsserver/macros.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/libmdsserver/macros.h')
-rw-r--r--src/libmdsserver/macros.h51
1 files changed, 49 insertions, 2 deletions
diff --git a/src/libmdsserver/macros.h b/src/libmdsserver/macros.h
index b39d750..672c0a3 100644
--- a/src/libmdsserver/macros.h
+++ b/src/libmdsserver/macros.h
@@ -339,6 +339,17 @@
/**
+ * `malloc` wrapper that returns whether the allocation was not successful
+ *
+ * @param var:type* The variable to which to assign the allocation
+ * @param bytes:size_t The number of bytes to allocate
+ * @return :int Evaluates to true if an only if the allocation failed
+ */
+#define xbmalloc(var, elements) \
+ ((var = malloc(elements)) == NULL)
+
+
+/**
* `calloc` wrapper that returns whether the allocation was not successful
*
* @param var:type* The variable to which to assign the allocation
@@ -351,6 +362,17 @@
/**
+ * `calloc` wrapper that returns whether the allocation was not successful
+ *
+ * @param var:type* The variable to which to assign the allocation
+ * @param bytes:size_t The number of bytes to allocate
+ * @return :int Evaluates to true if an only if the allocation failed
+ */
+#define xbcalloc(var, bytes) \
+ ((var = calloc(bytes, sizeof(char))) == NULL)
+
+
+/**
* `realloc` wrapper that returns whether the allocation was not successful
*
* @param var:type* The variable to which to assign the reallocation
@@ -365,7 +387,7 @@
/**
* `xrealloc` that stores the old variable
*
- * @param old:type* The variable to which to store with the old variable that needs
+ * @param old:type* The variable to which to store with the old variable that needs
* to be `free`:ed on failure, and set to `NULL` on success.
* @param var:type* The variable to which to assign the reallocation
* @param elements:size_t The number of elements to allocate
@@ -373,7 +395,21 @@
* @return :int Evaluates to true if an only if the allocation failed
*/
#define xxrealloc(old, var, elements, type) \
- (old = var, (xrealloc(var, elements, type) ? 1 : (old = NULL, 0)))
+ (old = var, (((var = realloc(var, (elements) * sizeof(type))) == NULL) ? 1 : (old = NULL, 0)))
+
+
+/**
+ * `xrealloc` that restores the variable on failure
+ *
+ * @param tmp:type* The variable to which to store with the old variable temporarily
+ * @param var:type* The variable to which to assign the reallocation
+ * @param elements:size_t The number of elements to allocate
+ * @param type The data type of the elements for which to create an allocation
+ * @return :int Evaluates to true if an only if the allocation failed
+ */
+#define yrealloc(tmp, var, elements, type) \
+ ((tmp = var, (var = realloc(var, (elements) * sizeof(type))) == NULL) \
+ ? (var = tmp, tmp = NULL, 1) : (tmp = NULL, 0))
/**
@@ -390,6 +426,17 @@
/**
+ * `strdup` wrapper that returns whether the allocation was not successful
+ *
+ * @param var:char* The variable to which to assign the duplicate
+ * @param original:const char* The string to duplicate
+ * @return :int Evaluates to true if an only if the allocation failed
+ */
+#define xstrdup(var, original) \
+ (original ? ((var = strdup(original)) == NULL) : (var = NULL, 0))
+
+
+/**
* Call `perror` if `errno` is non-zero and set `errno` to zero
*
* @param str:const char* The argument passed to `perror`