diff --git a/include/akgl/heap.h b/include/akgl/heap.h index 7b11e24..0c56ac6 100644 --- a/include/akgl/heap.h +++ b/include/akgl/heap.h @@ -22,14 +22,19 @@ #ifndef AKGL_MAX_HEAP_STRING #define AKGL_MAX_HEAP_STRING 256 #endif +#ifndef AKGL_MAX_HEAP_LIST +#define AKGL_MAX_HEAP_LIST AKGL_MAX_HEAP_ACTOR +#endif extern akgl_Actor HEAP_ACTOR[AKGL_MAX_HEAP_ACTOR]; extern akgl_Sprite HEAP_SPRITE[AKGL_MAX_HEAP_SPRITE]; extern akgl_SpriteSheet HEAP_SPRITESHEET[AKGL_MAX_HEAP_SPRITESHEET]; extern akgl_Character HEAP_CHARACTER[AKGL_MAX_HEAP_CHARACTER]; extern akgl_String HEAP_STRING[AKGL_MAX_HEAP_STRING]; +extern aksl_ListNode HEAP_LIST[AKGL_MAX_HEAP_LIST]; akerr_ErrorContext AKERR_NOIGNORE *akgl_heap_init(); +akerr_ErrorContext AKERR_NOIGNORE *akgl_heap_init_list(); akerr_ErrorContext AKERR_NOIGNORE *akgl_heap_init_actor(); akerr_ErrorContext AKERR_NOIGNORE *akgl_heap_next_actor(akgl_Actor **dest); akerr_ErrorContext AKERR_NOIGNORE *akgl_heap_next_sprite(akgl_Sprite **dest); diff --git a/src/heap.c b/src/heap.c index e172fa9..caa2eef 100644 --- a/src/heap.c +++ b/src/heap.c @@ -14,6 +14,9 @@ akgl_Sprite HEAP_SPRITE[AKGL_MAX_HEAP_SPRITE]; akgl_SpriteSheet HEAP_SPRITESHEET[AKGL_MAX_HEAP_SPRITESHEET]; akgl_Character HEAP_CHARACTER[AKGL_MAX_HEAP_CHARACTER]; akgl_String HEAP_STRING[AKGL_MAX_HEAP_STRING]; +aksl_ListNode HEAP_LIST[AKGL_MAX_HEAP_LIST]; + +int AKGL_LIST_SENTINEL = 65535; akerr_ErrorContext *akgl_heap_init() { @@ -36,6 +39,16 @@ akerr_ErrorContext *akgl_heap_init() SUCCEED_RETURN(errctx); } +akerr_ErrorContext *akgl_heap_init_list(void) +{ + PREPARE_ERROR(e); + for ( int i = 0; i < AKGL_MAX_HEAP_LIST; i++) { + memset(&HEAP_LIST[i], 0x00, sizeof(aksl_ListNode)); + HEAP_LIST[i].data = (void *)&AKGL_LIST_SENTINEL; + } + SUCCEED_RETURN(e); +} + akerr_ErrorContext *akgl_heap_init_actor(void) { PREPARE_ERROR(e); @@ -45,6 +58,30 @@ akerr_ErrorContext *akgl_heap_init_actor(void) SUCCEED_RETURN(e); } +akerr_ErrorContext *akgl_heap_next_list(aksl_ListNode **dest) +{ + PREPARE_ERROR(errctx); + for (int i = 0; i < AKGL_MAX_HEAP_LIST; i++ ) { + if ( HEAP_LIST[i].data != &AKGL_LIST_SENTINEL ) { + continue; + } + *dest = &HEAP_LIST[i]; + HEAP_LIST[i].data = NULL; + SUCCEED_RETURN(errctx); + } + FAIL_RETURN(errctx, AKERR_HEAP, "Unable to find unused list on the heap"); +} + +akerr_ErrorContext *akgl_heap_release_list(aksl_ListNode *list) +{ + PREPARE_ERROR(e); + FAIL_ZERO_RETURN(e, list, AKERR_NULLPOINTER, "list"); + list->next = NULL; + list->prev = NULL; + list->data = (void *)&AKGL_LIST_SENTINEL; + SUCCEED_RETURN(e); +} + akerr_ErrorContext *akgl_heap_next_actor(akgl_Actor **dest) { PREPARE_ERROR(errctx);