From b8d6dddea5dfdcefad896c639b5d34a34e5c6723 Mon Sep 17 00:00:00 2001 From: Borja Ferrer Date: Sat, 19 Aug 2006 20:52:42 +0000 Subject: [PATCH] fixed bsearch in debug API as its a lower bound one --HG-- extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%4066 --- sourcepawn/vm/sp_vm.c | 8 ++--- sourcepawn/vm/sp_vm_debug.c | 62 ++++++++++++++++++++----------------- 2 files changed, 38 insertions(+), 32 deletions(-) diff --git a/sourcepawn/vm/sp_vm.c b/sourcepawn/vm/sp_vm.c index 6a2b0a1b..303b92c3 100644 --- a/sourcepawn/vm/sp_vm.c +++ b/sourcepawn/vm/sp_vm.c @@ -7,10 +7,10 @@ #define CELLBOUNDMAX (INT_MAX/sizeof(cell_t)) #define STACKMARGIN ((cell_t)(16*sizeof(cell_t))) -int main() +/*int main() { /** temporary testing area */ - sp_context_t ctx; + /*sp_context_t ctx; cell_t l, *p; cell_t arr1[] = {1,3,3,7}; cell_t arr2[] = {123,1234,12345,123456}; @@ -38,7 +38,7 @@ int main() assert(SP_HeapRelease(&ctx, l) == SP_ERR_NONE); return 0; -} +}*/ int SP_HeapAlloc(sp_context_t *ctx, unsigned int cells, cell_t *local_addr, cell_t **phys_addr) { @@ -516,7 +516,7 @@ int SP_CreateBaseContext(sp_plugin_t *plugin, sp_context_t **ctx) const char *strbase; sp_fdbg_symbol_t *sym; sp_fdbg_arraydim_t *arr; - sp_context_t *context = *ctx; + sp_context_t *context; context = (sp_context_t *)malloc(sizeof(sp_context_t)); memset(context, 0, sizeof(sp_context_t)); diff --git a/sourcepawn/vm/sp_vm_debug.c b/sourcepawn/vm/sp_vm_debug.c index f97812ae..862af16d 100644 --- a/sourcepawn/vm/sp_vm_debug.c +++ b/sourcepawn/vm/sp_vm_debug.c @@ -1,30 +1,34 @@ #include "sp_vm.h" #include "sp_vm_debug.h" +#define USHR(x) ((unsigned int)(x)>>1) + int SP_DbgLookupFile(sp_context_t *ctx, ucell_t addr, const char **filename) { - int diff, high, low; - uint32_t mid; + int high, low, mid; - high = ctx->plugin->debug.files_num - 1; - low = 0; + high = ctx->plugin->debug.files_num; + low = -1; - while (low <= high) + while (high - low > 1) { - mid = (low + high) / 2; - diff = ctx->files[mid].addr - addr; - if (diff == 0) + mid = USHR(low + high); + if (ctx->files[mid].addr <= addr) { - *filename = ctx->files[mid].name; - return SP_ERR_NONE; - } else if (diff < 0) { - low = mid + 1; + low = mid; } else { - high = mid - 1; + high = mid; } } - return SP_ERR_NOT_FOUND; + if (low == -1) + { + return SP_ERR_NOT_FOUND; + } + + *filename = ctx->files[low].name; + + return SP_ERR_NONE; } int SP_DbgLookupFunction(sp_context_t *ctx, ucell_t addr, const char **name) @@ -53,28 +57,30 @@ int SP_DbgLookupFunction(sp_context_t *ctx, ucell_t addr, const char **name) int SP_DbgLookupLine(sp_context_t *ctx, ucell_t addr, uint32_t *line) { - int diff, high, low; - uint32_t mid; + int high, low, mid; - high = ctx->plugin->debug.lines_num - 1; - low = 0; + high = ctx->plugin->debug.lines_num; + low = -1; - while (low <= high) + while (high - low > 1) { - mid = (low + high) / 2; - diff = ctx->lines[mid].addr - addr; - if (diff == 0) + mid = USHR(low + high); + if (ctx->lines[mid].addr <= addr) { - *line = ctx->lines[mid].line; - return SP_ERR_NONE; - } else if (diff < 0) { - low = mid + 1; + low = mid; } else { - high = mid - 1; + high = mid; } } - return SP_ERR_NOT_FOUND; + if (low == -1) + { + return SP_ERR_NOT_FOUND; + } + + *line = ctx->lines[low].line; + + return SP_ERR_NONE; } int SP_DbgInstallBreak(sp_context_t *ctx, SPVM_DEBUGBREAK newpfn, SPVM_DEBUGBREAK *oldpfn)