aboutsummaryrefslogtreecommitdiff
path: root/analyzer/structures/heap.c
diff options
context:
space:
mode:
authorMosder <xmosder12@gmail.com>2026-07-23 17:35:04 +0200
committerMosder <xmosder12@gmail.com>2026-07-23 17:35:04 +0200
commit8317c3b33b9b1c70e5ae4c87c7efe33d9b9ce09a (patch)
tree8a6b5e20196a253c75b44762de223f1ea2b4a067 /analyzer/structures/heap.c
parent33fcaa8140784ab32cfc8c14a6fc7fdb71435265 (diff)
feat: added helper functions for dealing with memory management and refactored the codebase to use them
Diffstat (limited to 'analyzer/structures/heap.c')
-rw-r--r--analyzer/structures/heap.c10
1 files changed, 6 insertions, 4 deletions
diff --git a/analyzer/structures/heap.c b/analyzer/structures/heap.c
index 6102940..1267668 100644
--- a/analyzer/structures/heap.c
+++ b/analyzer/structures/heap.c
@@ -3,9 +3,11 @@
#include <stdlib.h>
#include <string.h>
+#include "utils/memory.h"
+
Heap *heap_new(size_t element_size, int (*compare)(void *element1, void *element2), void (*element_cleanup)(void *element)) {
- Heap *heap = malloc(sizeof(Heap));
- heap->data = malloc(INIT_HEAP_CAPACITY * sizeof(void *));
+ Heap *heap = safe_malloc(sizeof(Heap));
+ heap->data = safe_malloc(INIT_HEAP_CAPACITY * sizeof(void *));
heap->n_elements = 0;
heap->element_size = element_size;
heap->capacity = INIT_HEAP_CAPACITY;
@@ -40,7 +42,7 @@ void swap(void **p_el1, void **p_el2) {
void expand_heap(Heap *heap, size_t el_count) {
while (heap->capacity < el_count)
heap->capacity = (heap->capacity << 1) | 1;
- heap->data = realloc(heap->data, heap->capacity * sizeof(void *));
+ heap->data = safe_realloc(heap->data, heap->capacity * sizeof(void *));
}
void heap_insert(Heap *heap, void *element) {
@@ -50,7 +52,7 @@ void heap_insert(Heap *heap, void *element) {
// insert into the last position
size_t i = heap->n_elements;
- heap->data[i] = malloc(heap->element_size);
+ heap->data[i] = safe_malloc(heap->element_size);
memcpy(heap->data[i], element, heap->element_size);
heap->n_elements++;