summaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
authorMole Shang <[email protected]>2024-03-02 12:55:03 +0800
committerMole Shang <[email protected]>2024-03-02 12:55:03 +0800
commit74ebee5d3a81a39766ba8cd436a548449ea887b0 (patch)
tree69c238d2bff0d69aa4778d22e21de8d519b00dff /src/utils
parent62c193bd4e464ec9d847b8abff21e10dfc7b511e (diff)
downloadhinata-74ebee5d3a81a39766ba8cd436a548449ea887b0.tar.gz
hinata-74ebee5d3a81a39766ba8cd436a548449ea887b0.tar.bz2
hinata-74ebee5d3a81a39766ba8cd436a548449ea887b0.zip
tree-wide: use FREE_AND_NULLIFY macro and reformat code
Jeez idk why i forgot the pass-by-value feature, so the original version never gets the pointer nullified. Fix it by using our favourite C-style macro.
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/types.c6
-rw-r--r--src/utils/utils.c7
-rw-r--r--src/utils/utils.h5
3 files changed, 5 insertions, 13 deletions
diff --git a/src/utils/types.c b/src/utils/types.c
index e414f92..b9aab7b 100644
--- a/src/utils/types.c
+++ b/src/utils/types.c
@@ -15,7 +15,7 @@ generic_array_t create_array(size_t elem_size, size_t n) {
}
void free_array(generic_array_t *array) {
- free_and_nullify(array->data);
+ FREE_AND_NULLIFY(array->data);
array->n = 0;
}
@@ -54,7 +54,7 @@ void free_str_array(str_array_t *array) {
for (size_t i = 0; i < array->n; i++) {
free(array->str[i]);
}
- free_and_nullify(array->str);
+ FREE_AND_NULLIFY(array->str);
array->n = 0;
}
@@ -116,7 +116,7 @@ data_t dequeue(queue_t *queue) {
node_t *temp = queue->front;
data_t data = temp->data;
queue->front = temp->next;
- free_and_nullify(temp);
+ FREE_AND_NULLIFY(temp);
if (queue->front == NULL) {
queue->rear = NULL;
diff --git a/src/utils/utils.c b/src/utils/utils.c
index ae60eee..7b94ff3 100644
--- a/src/utils/utils.c
+++ b/src/utils/utils.c
@@ -169,10 +169,3 @@ int repchr(char *str, char t, char r) {
}
return c;
}
-
-void free_and_nullify(void *p) {
- if (p) {
- free(p);
- p = NULL;
- }
-}
diff --git a/src/utils/utils.h b/src/utils/utils.h
index 5a26cf2..3b44f3f 100644
--- a/src/utils/utils.h
+++ b/src/utils/utils.h
@@ -4,6 +4,8 @@
#include <stddef.h>
#include "types.h"
+#define FREE_AND_NULLIFY(x) do{if(x)free(x); x = NULL;}while(0)
+
int regex_match(const char *, str_array_t, str_array_t *);
int substitute_str(const char *subject, const char *pattern,
@@ -13,7 +15,4 @@ const char *mimeType2ext(const char *mimeType);
int repchr(char *str, char t, char r);
-/* NOTICE: pass a pointer-to-pointer to free the original pointer. */
-void free_and_nullify(void *p);
-
#endif