AmendHub

Download:

jcs

/

wallops

/

amendments

/

71

main: Add Debug menu when MALLOC_DEBUG is on


jcs made amendment 71 2 months ago
--- main.c Thu Sep 5 11:03:53 2024 +++ main.c Mon Sep 9 08:44:37 2024 @@ -15,6 +15,7 @@ */ #include <string.h> +#include <stdarg.h> #include "chatter.h" #include "irc.h" #include "settings.h" @@ -25,6 +26,13 @@ NMRec notification = { 0 }; struct settings settings; MenuHandle apple_menu, file_menu, edit_menu, view_menu; +#ifdef MALLOC_DEBUG +MenuHandle debug_menu; +#define DEBUG_MENU_DUMP_ID 999 +static short xalloc_frefnum = 0; +size_t xalloc_printf(const char *format, ...); +#endif + void update_menu(void); void handle_exit(void); bool handle_menu(long menu_id); @@ -72,6 +80,11 @@ main(void) if (!(view_menu = GetMHandle(VIEW_MENU_ID))) panic("no view menu"); update_menu(); +#ifdef MALLOC_DEBUG + debug_menu = NewMenu(DEBUG_MENU_DUMP_ID, "\pDebug"); + AppendMenu(debug_menu, "\pDump Allocations"); + InsertMenu(debug_menu, 0); +#endif DrawMenuBar(); show_connect_dialog(); @@ -287,6 +300,40 @@ handle_menu(long menu_id) } } break; +#ifdef MALLOC_DEBUG + case DEBUG_MENU_DUMP_ID: + switch (LoWord(menu_id)) { + case 1: { + Str255 vname; + short error, vrefnum; + + error = GetVol(&vname, &vrefnum); + if (error) + panic("Failed to get volume: %d", error); + PtoCstr(vname); + strlcat((char *)vname, ":", sizeof(vname)); + strlcat((char *)vname, PROGRAM_NAME, sizeof(vname)); + strlcat((char *)vname, "_xalloc.txt", sizeof(vname)); + CtoPstr(vname); + + error = Create(vname, vrefnum, 'TEXT', 'TEXT'); + if (error && error != dupFNErr) + panic("Failed to create file: %d", error); + error = FSOpen(vname, vrefnum, &xalloc_frefnum); + if (error) + panic("Failed to open file: %d", error); + error = SetEOF(xalloc_frefnum, 0); + if (error) + panic("Failed to truncate file: %d", error); + + xalloc_print(xalloc_printf); + FSClose(xalloc_frefnum); + break; + } + } + ret = true; + break; +#endif default: if (nfocusables && focusables[0]->visible && focusables[0]->menu) ret = focusables[0]->menu(focusables[0], HiWord(menu_id), @@ -296,6 +343,32 @@ handle_menu(long menu_id) HiliteMenu(0); return ret; } + +#ifdef MALLOC_DEBUG +size_t +xalloc_printf(const char *format, ...) +{ + static char buf[256]; + short error; + va_list va; + size_t len; + + va_start(va, format); + len = vsnprintf(buf, sizeof(buf), format, va); + va_end(va); + + error = FSWrite(xalloc_frefnum, &len, buf); + if (error) + panic("Failed to write: %d", error); + + len = 1; + error = FSWrite(xalloc_frefnum, &len, "\r"); + if (error) + panic("Failed to write: %d", error); + + return len; +} +#endif void update_menu(void)