AmendHub

Download:

jcs

/

subtext

/

amendments

/

590

util: Use __option() to enforce options needed for malloc debugging


jcs made amendment 590 9 months ago
--- util.c Thu Feb 15 15:18:31 2024 +++ util.c Thu Feb 15 17:02:43 2024 @@ -108,13 +108,23 @@ util_init(void) #define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4)) #ifdef MALLOC_DEBUG +/* + * malloc tracking, storing a stack trace of each xmalloc call and its + * size, which can be dumped with xalloc_print(). xfree also gains the + * ability to detect junk frees. + * + * Requires project options 'Always generate stack frames', + * 'Macsbug Names', and 'Long format' to be enabled. + */ +#if !__option(force_frame) || !__option(long_macsbug_names) +#error +#endif -#define SYMBOL_SIZE 32 -static char symbols[64][SYMBOL_SIZE]; - -#define MAX_STACK_DEPTH 10 +#define XALLOC_SYMBOL_SIZE 32 +#define XALLOC_MAX_STACK_DEPTH 10 +static char symbols[64][XALLOC_SYMBOL_SIZE]; struct xalloc { - char *stack[MAX_STACK_DEPTH]; + char *stack[XALLOC_MAX_STACK_DEPTH]; unsigned long addr; unsigned long size; }; @@ -126,7 +136,7 @@ xmalloc(size_t size) { void *ptr; #ifdef MALLOC_DEBUG - char tsym[SYMBOL_SIZE], *sym; + static char tsym[XALLOC_SYMBOL_SIZE], *sym; struct xalloc *xa; unsigned long _a6, ret; unsigned char *code; @@ -164,7 +174,7 @@ xmalloc(size_t size) * to and do it again to find the previous function symbol, repeating * until we hit "main" or "uthread_begin". */ - for (nframe = 0; nframe < MAX_STACK_DEPTH; nframe++) { + for (nframe = 0; nframe < XALLOC_MAX_STACK_DEPTH; nframe++) { ret = *(unsigned long *)(_a6 + 4); code = (unsigned char *)ret; @@ -349,7 +359,7 @@ xalloc_print(size_t (*printer)(const char *, ...)) chain[0] = '\0'; len = 0; - for (j = MAX_STACK_DEPTH - 1; j >= 0; j--) { + for (j = XALLOC_MAX_STACK_DEPTH - 1; j >= 0; j--) { if (xallocs[n].stack[j] == 0) continue;