[Fix] Fixed hooks being freed twise in some situations.

This commit is contained in:
Allanis 2014-05-27 15:40:49 +01:00
parent be72a73740
commit 2ff980a937
3 changed files with 64 additions and 25 deletions

View File

@ -32,7 +32,7 @@
<outfits> <outfits>
<outfit quantity="2">Ripper MK2</outfit> <outfit quantity="2">Ripper MK2</outfit>
<outfit quantity="1">Lancelot Fighter Bay</outfit> <outfit quantity="1">Lancelot Fighter Bay</outfit>
<outfit quantity="2">Lancelot Fighter</outfit> <outfit quantity="100">Lancelot Fighter</outfit>
<outfit quantity="1">Banshee Launcher</outfit> <outfit quantity="1">Banshee Launcher</outfit>
<outfit quantity="50">Banshee Rocket</outfit> <outfit quantity="50">Banshee Rocket</outfit>
<outfit quantity="1">Headhunter Launcher</outfit> <outfit quantity="1">Headhunter Launcher</outfit>

View File

@ -84,8 +84,17 @@ unsigned int hook_add(unsigned int parent, char* func, char* stack) {
return new_hook->id; 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; l = 0;
h = hook_nstack-1; h = hook_nstack-1;
@ -93,13 +102,20 @@ void hook_rm(unsigned int id) {
m = (l+h)/2; m = (l+h)/2;
if(hook_stack[m].id > id) h = m-1; if(hook_stack[m].id > id) h = m-1;
else if(hook_stack[m].id < id) l = 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. */ /* Mark to delete, but do not delete yet, hooks are running. */
if(hook_runningstack) { if(hook_runningstack) {
hook_stack[m].delete = 1; hook_stack[m].delete = 1;
return; return 2;
} }
/* Free the hook. */ /* Free the hook. */
@ -108,24 +124,34 @@ void hook_rm(unsigned int id) {
/* Last hook, just clip the stack. */ /* Last hook, just clip the stack. */
if(m == (hook_nstack-1)) { if(m == (hook_nstack-1)) {
hook_nstack--; hook_nstack--;
return; return 1;
} }
/* Move it! */ /* Move it! */
memmove(&hook_stack[m], &hook_stack[m+1], sizeof(Hook)*(hook_nstack-(m+1))); memmove(&hook_stack[m], &hook_stack[m+1], sizeof(Hook)*(hook_nstack-(m+1)));
hook_nstack--; 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) { void hook_rmParent(unsigned int parent) {
int i; int i;
for(i = 0; i < hook_nstack; i++) for(i = 0; i < hook_nstack; i++)
if(parent == hook_stack[i].parent) { if(parent == hook_stack[i].parent) {
hook_rm(hook_stack[i].id); /* Only decrement if hook was actually removed. */
if(!hook_runningstack) i--; 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 hooks_run(char* stack) {
int i; int i;
@ -145,15 +171,27 @@ int hooks_run(char* stack) {
return 0; 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) { 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++) for(i = 0; i < hook_nstack; i++)
if(hook_stack[i].id == id) { if(hook_stack[i].id == id) {
hook_run(&hook_stack[i]); h = &hook_stack[i];
return; hook_run(h);
ret = 1;
break;
} }
/* Hook not found. */
if(ret == 0)
DEBUG("Attempting to run hook of id '%d' which is not in the stack", id); DEBUG("Attempting to run hook of id '%d' which is not in the stack", id);
} }

View File

@ -3,19 +3,20 @@
/* Add/Run hooks. */ /* Add/Run hooks. */
unsigned int hook_add(unsigned int parent, char* func, char* stack); 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); void hook_rmParent(unsigned int parent);
/* ======================================================== */ /* ========================================================
/* Run Hooks: */ * Run Hooks:
/* */ *
/* Currently used: */ * Currently used:
/* -- "land" - When landed. */ * -- "land" - When landed.
/* -- "takeoff" - When taking off. */ * -- "takeoff" - When taking off.
/* -- "jump" - When changing systems. */ * -- "jump" - When changing systems.
/* -- "time" - When time is increment drastically */ * -- "time" - When time is increment drastically
/* (hyperspace and taking off. */ * (hyperspace and taking off.
/* ======================================================== */ * ========================================================
*/
int hooks_run(char* stack); int hooks_run(char* stack);
void hook_runID(unsigned int id); /* Runs hook of specific id. */ void hook_runID(unsigned int id); /* Runs hook of specific id. */