From 2ff980a937dce4ae07a468e6e542740032e2eb3d Mon Sep 17 00:00:00 2001 From: Allanis Date: Tue, 27 May 2014 15:40:49 +0100 Subject: [PATCH] [Fix] Fixed hooks being freed twise in some situations. --- dat/ship.xml | 2 +- src/hook.c | 64 +++++++++++++++++++++++++++++++++++++++++----------- src/hook.h | 23 ++++++++++--------- 3 files changed, 64 insertions(+), 25 deletions(-) diff --git a/dat/ship.xml b/dat/ship.xml index d405513..6c42e7e 100644 --- a/dat/ship.xml +++ b/dat/ship.xml @@ -32,7 +32,7 @@ Ripper MK2 Lancelot Fighter Bay - Lancelot Fighter + Lancelot Fighter Banshee Launcher Banshee Rocket Headhunter Launcher diff --git a/src/hook.c b/src/hook.c index 467aaa6..992e232 100644 --- a/src/hook.c +++ b/src/hook.c @@ -84,8 +84,17 @@ unsigned int hook_add(unsigned int parent, char* func, char* stack) { return new_hook->id; } -void hook_rm(unsigned int id) { - int l, m, h; +/** + * @brief Remove a hook. + * @param id Identifier of the hook to remove. + * @return 1 if hook was removed, 2 if hook was scheuled for removal and + * 0 if it wasn't removed. + */ +int hook_rm(unsigned int id) { + int l, m, h, f; + + /* Binary search. */ + f = 0; l = 0; h = hook_nstack-1; @@ -93,13 +102,20 @@ void hook_rm(unsigned int id) { m = (l+h)/2; if(hook_stack[m].id > id) h = m-1; else if(hook_stack[m].id < id) l = m+1; - else break; + else { + f = 1; + break; + } } + /* Check if hook was found. */ + if(f == 0) + return 0; + /* Mark to delete, but do not delete yet, hooks are running. */ if(hook_runningstack) { hook_stack[m].delete = 1; - return; + return 2; } /* Free the hook. */ @@ -108,24 +124,34 @@ void hook_rm(unsigned int id) { /* Last hook, just clip the stack. */ if(m == (hook_nstack-1)) { hook_nstack--; - return; + return 1; } /* Move it! */ memmove(&hook_stack[m], &hook_stack[m+1], sizeof(Hook)*(hook_nstack-(m+1))); hook_nstack--; + return 1; } +/** + * @brief Remove all hooks belonging to parent. + * @param parent Parent id to remove all hooks belonging to. + */ void hook_rmParent(unsigned int parent) { int i; for(i = 0; i < hook_nstack; i++) if(parent == hook_stack[i].parent) { - hook_rm(hook_stack[i].id); - if(!hook_runningstack) i--; + /* Only decrement if hook was actually removed. */ + if(hook_rm(hook_stack[i].id) == 1) + i--; } } -/* Run all hooks off the stack. */ +/** + * @brief Run all the hooks of stack. + * @param stack Stack to run. + * @return 0 on success. + */ int hooks_run(char* stack) { int i; @@ -145,16 +171,28 @@ int hooks_run(char* stack) { return 0; } -/* Run a single hook by id. */ +/** + * @brief Run a single hook by id. + * @param id Identifier of the hook to run. + * @return The ID of the hook or 0 if it got deleted. + */ void hook_runID(unsigned int id) { - int i; + Hook* h; + int i, ret; + /* Try to find the hook and run it. */ + ret = 0; for(i = 0; i < hook_nstack; i++) if(hook_stack[i].id == id) { - hook_run(&hook_stack[i]); - return; + h = &hook_stack[i]; + hook_run(h); + ret = 1; + break; } - DEBUG("Attempting to run hook of id '%d' which is not in the stack", id); + + /* Hook not found. */ + if(ret == 0) + DEBUG("Attempting to run hook of id '%d' which is not in the stack", id); } /* Free a hook. */ diff --git a/src/hook.h b/src/hook.h index eca95d9..688e8fb 100644 --- a/src/hook.h +++ b/src/hook.h @@ -3,19 +3,20 @@ /* Add/Run hooks. */ unsigned int hook_add(unsigned int parent, char* func, char* stack); -void hook_rm(unsigned int id); +int hook_rm(unsigned int id); void hook_rmParent(unsigned int parent); -/* ======================================================== */ -/* Run Hooks: */ -/* */ -/* Currently used: */ -/* -- "land" - When landed. */ -/* -- "takeoff" - When taking off. */ -/* -- "jump" - When changing systems. */ -/* -- "time" - When time is increment drastically */ -/* (hyperspace and taking off. */ -/* ======================================================== */ +/* ======================================================== + * Run Hooks: + * + * Currently used: + * -- "land" - When landed. + * -- "takeoff" - When taking off. + * -- "jump" - When changing systems. + * -- "time" - When time is increment drastically + * (hyperspace and taking off. + * ======================================================== + */ int hooks_run(char* stack); void hook_runID(unsigned int id); /* Runs hook of specific id. */