AmendHub

Download:

jcs

/

subtext

/

amendments

/

593

util: Make xalloc tables dynamically allocated

Also reduce err_str size

jcs made amendment 593 10 months ago
--- util.c Thu Feb 15 17:02:43 2024 +++ util.c Fri Feb 16 15:30:48 2024 @@ -31,7 +31,7 @@ /* ALRT resources */ #define ASK_ALERT_ID 130 -#define ERROR_STRING_SIZE 1024 +#define ERROR_STRING_SIZE 256 static char err_str[ERROR_STRING_SIZE]; /* basic DITL with an ok button (1), text area (2), and icon (3) */ @@ -120,15 +120,17 @@ util_init(void) #error #endif -#define XALLOC_SYMBOL_SIZE 32 -#define XALLOC_MAX_STACK_DEPTH 10 -static char symbols[64][XALLOC_SYMBOL_SIZE]; +#define XALLOC_SYMBOL_SIZE 32 +#define XALLOC_NSYMBOLS 64 +#define XALLOC_MAX_STACK_DEPTH 10 +static char *symbols = NULL; struct xalloc { char *stack[XALLOC_MAX_STACK_DEPTH]; unsigned long addr; unsigned long size; }; -static struct xalloc xallocs[256] = { 0 }; +#define XALLOC_NXALLOCS 512 +static struct xalloc *xallocs = NULL; #endif void * @@ -136,7 +138,7 @@ xmalloc(size_t size) { void *ptr; #ifdef MALLOC_DEBUG - static char tsym[XALLOC_SYMBOL_SIZE], *sym; + static char cursym[XALLOC_SYMBOL_SIZE], *tsym, *sym; struct xalloc *xa; unsigned long _a6, ret; unsigned char *code; @@ -155,9 +157,23 @@ xmalloc(size_t size) asm { move.l a6,_a6 }; - + + if (symbols == NULL) { + symbols = NewPtr(n = XALLOC_SYMBOL_SIZE * XALLOC_NSYMBOLS); + if (symbols == NULL) + panic("NewPtr failed for xalloc symbols"); + memset(symbols, 0, n); + } + if (xallocs == NULL) { + xallocs = (struct xalloc *)NewPtr(n = (XALLOC_NXALLOCS * + sizeof(struct xalloc))); + if (xallocs == NULL) + panic("NewPtr failed for xallocs"); + memset(xallocs, 0, n); + } + xa = NULL; - for (n = 0; n < nitems(xallocs); n++) { + for (n = 0; n < XALLOC_NXALLOCS; n++) { if (xallocs[n].addr == 0) { xa = &xallocs[n]; break; @@ -178,34 +194,37 @@ xmalloc(size_t size) ret = *(unsigned long *)(_a6 + 4); code = (unsigned char *)ret; - tsym[0] = '\0'; + cursym[0] = '\0'; for (; ; code += 2) { if (code[0] == 0x4e && code[1] == 0x75) { /* RTS */ code += 3; - strlcpy(tsym, (char *)code, sizeof(tsym)); + strlcpy(cursym, (char *)code, sizeof(cursym)); break; } } - if (!tsym[0]) + if (!cursym[0]) break; sym = NULL; - for (n = 0; n < nitems(symbols); n++) { - if (symbols[n][0] == '\0') + for (n = 0; n < XALLOC_NSYMBOLS; n++) { + tsym = symbols + (XALLOC_SYMBOL_SIZE * n); + + if (tsym[0] == '\0') break; - if (strcmp(symbols[n], tsym) == 0) { - sym = (char *)&symbols[n]; + if (strcmp(tsym, cursym) == 0) { + sym = tsym; break; } } if (sym == NULL) { - for (n = 0; n < nitems(symbols); n++) { - if (symbols[n][0] != '\0') + for (n = 0; n < XALLOC_NSYMBOLS; n++) { + tsym = symbols + (XALLOC_SYMBOL_SIZE * n); + if (tsym[0] != '\0') continue; - strlcpy(symbols[n], tsym, sizeof(symbols[n])); - sym = (char *)&symbols[n]; + strlcpy(tsym, cursym, XALLOC_SYMBOL_SIZE); + sym = tsym; break; } } @@ -245,13 +264,13 @@ xfree(void *ptrptr) panic("xfree(&NULL) likely a double-free"); #ifdef MALLOC_DEBUG - for (n = 0; n < nitems(xallocs); n++) { + for (n = 0; n < XALLOC_NXALLOCS; n++) { if (xallocs[n].addr == (unsigned long)ptr) { - memset(&xallocs[n], 0, sizeof(xallocs[n])); + memset(&xallocs[n], 0, sizeof(struct xalloc)); break; } } - if (n == nitems(xallocs)) + if (n == XALLOC_NXALLOCS) panic("xfree() without xalloc, possibly a junk free", (unsigned long)ptr); #endif @@ -350,7 +369,7 @@ xalloc_print(size_t (*printer)(const char *, ...)) size_t total = 0, nallocs = 0; char chain[128]; - for (n = 0; n < nitems(xallocs); n++) { + for (n = 0; n < XALLOC_NXALLOCS; n++) { if (xallocs[n].addr == 0) continue;