aboutsummaryrefslogtreecommitdiff
path: root/analyzer/utils/memory.h
diff options
context:
space:
mode:
authorMaciej Borowiec <81316152+Mosder@users.noreply.github.com>2026-07-23 20:17:15 +0200
committerGitHub <noreply@github.com>2026-07-23 20:17:15 +0200
commit3419cad016b3c34d0630456d4390c70063462d91 (patch)
tree3a1aa4416e45f134d38f83d4ea2285f1972f9391 /analyzer/utils/memory.h
parent2e3f7fe39791cd671a68330f7ba7a5bb5fc6f9ae (diff)
parent682f9fc8288fdd01de1d88af87495d9e43f56b06 (diff)
LGTM
Diffstat (limited to 'analyzer/utils/memory.h')
-rw-r--r--analyzer/utils/memory.h46
1 files changed, 46 insertions, 0 deletions
diff --git a/analyzer/utils/memory.h b/analyzer/utils/memory.h
new file mode 100644
index 0000000..0973623
--- /dev/null
+++ b/analyzer/utils/memory.h
@@ -0,0 +1,46 @@
+// utils/memory.h
+// helper functions for dealing with memory management
+
+#include <stddef.h>
+
+#define ARR_LEN(arr) (sizeof(arr) / sizeof(arr[0]))
+
+#define MAX_ALLOC_ATTEMPTS 3
+
+// tries malloc MAX_ALLOC_ATTEMPTS times, if all fail - exits the program
+// params:
+// - size - count of bytes to allocate
+// returns:
+// pointer to the allocated memory
+extern void *safe_malloc(size_t size);
+
+// tries realloc MAX_ALLOC_ATTEMPTS times, if all fail - exits the program
+// params:
+// - ptr - pointer to the memory to reallocate
+// - size - new size of the memory
+// returns:
+// pointer to the reallocated memory
+extern void *safe_realloc(void *ptr, size_t size);
+
+// append to array, expand if necessary
+// params:
+// - arr - array to append to
+// - el_size - size of an array element
+// - len - pointer to the length of the array
+// - cap - pointer to the capacity of the array
+// - val - pointer to the value to append to the array
+// returns:
+// new pointer to the array (different if it was realloced)
+extern void *array_append(void *arr, size_t el_size, size_t *len, size_t *cap, void *val);
+
+// concatenate 2 arrays, expand if necessary
+// params:
+// - arr1 - first array (destination)
+// - el_size - size of an array element
+// - len1 - pointer to the length of arr1
+// - cap - pointer to the capacity of arr1
+// - arr2 - second array
+// - len2 - length of arr2
+// returns:
+// new pointer to the array (different if it was realloced)
+extern void *array_concat(void *arr1, size_t el_size, size_t *len1, size_t *cap, void *arr2, size_t len2);