From 773ddd98a8c3797401c2ce37a93b24d81e0c6c88 Mon Sep 17 00:00:00 2001 From: Sryder Date: Sun, 9 Jun 2019 19:36:36 +0100 Subject: [PATCH] Add function for freeing binary heap. --- src/k_bheap.c | 24 ++++++++++++++++++++++++ src/k_bheap.h | 16 ++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/k_bheap.c b/src/k_bheap.c index 6b4ebe75d..527af1bfc 100644 --- a/src/k_bheap.c +++ b/src/k_bheap.c @@ -645,3 +645,27 @@ size_t K_BHeapContains(bheap_t *const heap, void *const data, size_t index) return heapindexwithdata; } + +boolean K_BHeapFree(bheap_t *const heap) +{ + boolean freesuccess = false; + + if (heap == NULL) + { + CONS_Debug(DBG_GAMELOGIC, "NULL heap in K_BHeapFree.\n"); + } + else if (!K_BHeapValid(heap)) + { + CONS_Debug(DBG_GAMELOGIC, "Uninitialised heap in K_BHeapFree.\n"); + } + else + { + Z_Free(heap->array); + heap->array = NULL; + heap->capacity = 0U; + heap->count = 0U; + freesuccess = true; + } + + return freesuccess; +} diff --git a/src/k_bheap.h b/src/k_bheap.h index e56d7299b..f1c6d2d5c 100644 --- a/src/k_bheap.h +++ b/src/k_bheap.h @@ -122,4 +122,20 @@ boolean K_UpdateBHeapItemValue(bheapitem_t *const item, const UINT32 newvalue); size_t K_BHeapContains(bheap_t *const heap, void *const data, size_t index); + +/*-------------------------------------------------- + boolean K_BHeapFree(bheap_t *const heap) + + Free the binary heap. + This does NOT free the data held within the binary heap items. Make sure those can still be freed manually. + + Input Arguments:- + heap - The heap to free + + Return:- + True if the heap was freed successfully, false if the heap wasn't valid to free +--------------------------------------------------*/ + +boolean K_BHeapFree(bheap_t *const heap); + #endif