jcs
/amend
/amendments
/136
*: Remove malloc notes, add debug menu when MALLOC_DEBUG defined
jcs made amendment 136 over 2 years ago
--- bile.c Tue Sep 19 20:45:17 2023
+++ bile.c Fri Feb 16 10:05:26 2024
@@ -71,7 +71,7 @@ bile_create(const Str255 filename, short vrefnum, cons
return NULL;
SetFPos(fh, fsFromStart, 0);
- bile = xmalloczero(sizeof(struct bile), "bile_create");
+ bile = xmalloczero(sizeof(struct bile));
memcpy(bile->magic, BILE_MAGIC, sizeof(bile->magic));
bile->vrefnum = vrefnum;
bile->frefnum = fh;
@@ -80,7 +80,7 @@ bile_create(const Str255 filename, short vrefnum, cons
/* write magic */
len = BILE_MAGIC_LEN;
- tmp = xstrdup(BILE_MAGIC, "bile_create magic");
+ tmp = xstrdup(BILE_MAGIC);
_bile_error = FSWrite(bile->frefnum, &len, tmp);
xfree(&tmp);
if (_bile_error)
@@ -100,7 +100,7 @@ bile_create(const Str255 filename, short vrefnum, cons
/* padding */
len = BILE_HEADER_LEN - BILE_MAGIC_LEN - BILE_OBJECT_SIZE -
BILE_OBJECT_SIZE;
- tmp = xmalloczero(len, "bile_create padding");
+ tmp = xmalloczero(len);
_bile_error = FSWrite(bile->frefnum, &len, tmp);
if (_bile_error)
goto create_bail;
@@ -143,7 +143,7 @@ bile_open(const Str255 filename, short vrefnum)
GetFPos(fh, &file_size);
SetFPos(fh, fsFromStart, 0);
- bile = xmalloczero(sizeof(struct bile), "bile_open");
+ bile = xmalloczero(sizeof(struct bile));
memcpy(bile->magic, BILE_MAGIC, sizeof(bile->magic));
bile->vrefnum = vrefnum;
bile->frefnum = fh;
@@ -268,7 +268,6 @@ struct bile_object *
bile_find(struct bile *bile, const OSType type, const unsigned long id)
{
struct bile_object *o, *ocopy;
- char note[MALLOC_NOTE_SIZE];
bile_check_sanity(bile);
@@ -276,9 +275,7 @@ bile_find(struct bile *bile, const OSType type, const
if (o == NULL)
return NULL;
- snprintf(note, sizeof(note), "bile_find %s %lu", OSTypeToString(type),
- id);
- ocopy = xmalloc(BILE_OBJECT_SIZE, note);
+ ocopy = xmalloc(BILE_OBJECT_SIZE);
memcpy(ocopy, o, BILE_OBJECT_SIZE);
return ocopy;
@@ -346,7 +343,6 @@ bile_get_nth_of_type(struct bile *bile, const unsigned
{
struct bile_object *o, *ocopy;
size_t n, count = 0;
- char note[MALLOC_NOTE_SIZE];
bile_check_sanity(bile);
@@ -358,9 +354,7 @@ bile_get_nth_of_type(struct bile *bile, const unsigned
continue;
if (count == index) {
- snprintf(note, sizeof(note), "bile_get_nth %s %lu",
- OSTypeToString(type), index);
- ocopy = xmalloc(BILE_OBJECT_SIZE, note);
+ ocopy = xmalloc(BILE_OBJECT_SIZE);
memcpy(ocopy, o, BILE_OBJECT_SIZE);
return ocopy;
}
@@ -573,7 +567,6 @@ bile_read_alloc(struct bile *bile, const OSType type,
struct bile_object *o;
size_t ret;
char **data;
- char note[MALLOC_NOTE_SIZE];
bile_check_sanity(bile);
@@ -590,9 +583,7 @@ bile_read_alloc(struct bile *bile, const OSType type,
return 0;
}
- snprintf(note, sizeof(note), "bile_read_alloc %s %ld",
- OSTypeToString(type), id);
- *data = xmalloc(o->size, note);
+ *data = xmalloc(o->size);
ret = bile_read_object(bile, o, *data, o->size);
return ret;
@@ -694,7 +685,7 @@ iterate_fields:
}
if (!write) {
- data = xmalloc(size, note);
+ data = xmalloc(size);
write = true;
size = 0;
goto iterate_fields;
@@ -736,7 +727,7 @@ bile_unmarshall_object(struct bile *bile,
memset(ptr, 0, sizeof(dptr));
continue;
}
- dptr = xmalloc(fsize, note);
+ dptr = xmalloc(fsize);
memcpy(ptr, &dptr, sizeof(dptr));
ptr = dptr;
}
@@ -905,7 +896,7 @@ bile_read_map(struct bile *bile, struct bile_object *m
/* read entire map */
size = map_obj.size;
- map = xmalloczero(size, "bile_read_map");
+ map = xmalloczero(size);
_bile_error = FSRead(bile->frefnum, &size, map);
if (_bile_error) {
xfree(&map);
@@ -943,7 +934,7 @@ bile_write_map(struct bile *bile)
new_map_size = BILE_OBJECT_SIZE * new_nobjects;
new_map_obj = bile_alloc(bile, BILE_TYPE_MAP, new_map_id,
new_map_size);
- new_map = xcalloc(BILE_OBJECT_SIZE, new_nobjects, "bile_write_map");
+ new_map = xcalloc(BILE_OBJECT_SIZE, new_nobjects);
for (n = 0, new_nobjects = 0; n < bile->nobjects; n++) {
obj = &bile->map[n];
--- browser.c Mon Dec 11 16:13:32 2023
+++ browser.c Fri Feb 16 09:13:03 2024
@@ -118,7 +118,7 @@ browser_init(struct repo *repo)
Cell cell = { 0 };
short width, height;
- browser = xmalloczero(sizeof(struct browser), "browser");
+ browser = xmalloczero(sizeof(struct browser));
browser->state = BROWSER_STATE_IDLE;
browser->repo = repo;
@@ -215,7 +215,7 @@ browser_init(struct repo *repo)
UpdateScrollbarForTE(browser->win, browser->diff_scroller,
browser->diff_te, true);
- focusable = xmalloczero(sizeof(struct focusable), "focusable");
+ focusable = xmalloczero(sizeof(struct focusable));
focusable->cookie = browser;
focusable->win = browser->win;
focusable->idle = browser_idle;
@@ -325,8 +325,7 @@ browser_selected_file_ids(struct browser *browser, sho
return 0;
}
- *selected_files = xcalloc(browser->repo->nfiles, sizeof(short),
- "selected_files");
+ *selected_files = xcalloc(browser->repo->nfiles, sizeof(short));
if (browser_is_all_files_selected(browser)) {
nselected_files = browser->repo->nfiles;
--- committer.c Thu Jan 11 13:56:58 2024
+++ committer.c Fri Feb 16 09:15:01 2024
@@ -67,7 +67,7 @@ committer_init(struct browser *browser)
TextStyle style;
short fh;
- committer = xmalloczero(sizeof(struct committer), "committer_init");
+ committer = xmalloczero(sizeof(struct committer));
committer->browser = browser;
browser->committer = committer;
@@ -149,7 +149,7 @@ committer_init(struct browser *browser)
committer->last_te = committer->log_te;
- focusable = xmalloczero(sizeof(struct focusable), "committer focusable");
+ focusable = xmalloczero(sizeof(struct focusable));
focusable->cookie = committer;
focusable->win = committer->win;
focusable->modal = true;
@@ -442,7 +442,7 @@ committer_generate_diff(struct committer *committer)
committer->allow_commit = false;
committer->canceled = false;
committer->diffed_files = xcalloc(sizeof(struct diffed_file),
- nselected_files, "committer diffed_files");
+ nselected_files);
committer->diff_too_big = false;
HLock(committer->diff_te);
--- diffreg.c Thu Jan 11 14:28:35 2024
+++ diffreg.c Fri Feb 16 09:40:54 2024
@@ -357,10 +357,10 @@ diffreg(char *file1, char *file2, int flags)
unsort(sfile[0], slen[0], class);
class = xreallocarray(class, slen[0] + 2, sizeof(*class));
- klist = xcalloc(slen[0] + 2, sizeof(*klist), "diffreg klist");
+ klist = xcalloc(slen[0] + 2, sizeof(*klist));
clen = 0;
clistlen = 100;
- clist = xcalloc(clistlen, sizeof(*clist), "diffreg clist");
+ clist = xcalloc(clistlen, sizeof(*clist));
i = stone(class, slen[0], member, klist, flags);
xfree(&member);
xfree(&class);
@@ -444,7 +444,7 @@ prepare(int i, FILE *fd, off_t filesize, int flags)
if (sz < 100)
sz = 100;
- p = xcalloc(sz + 3, sizeof(*p), "diff prepare");
+ p = xcalloc(sz + 3, sizeof(*p));
for (j = 0; (h = readhash(fd, flags));) {
if (j == sz) {
sz = sz * 3 / 2;
@@ -772,7 +772,7 @@ unsort(struct line *f, int l, int *b)
{
int *a, i;
- a = xcalloc(l + 1, sizeof(*a), "diff unsort");
+ a = xcalloc(l + 1, sizeof(*a));
for (i = 1; i <= l; i++)
a[f[i].serial] = f[i].value;
for (i = 1; i <= l; i++)
@@ -870,7 +870,7 @@ preadline(int fd, size_t rlen, off_t off)
ssize_t nr;
off_t pos;
- line = xmalloc(rlen + 1, "diff preadline");
+ line = xmalloc(rlen + 1);
pos = lseek(fd, 0, SEEK_CUR);
lseek(fd, off, SEEK_SET);
if ((nr = read(fd, line, rlen)) == -1)
--- editor.c Tue Oct 24 12:01:57 2023
+++ editor.c Fri Feb 16 09:08:04 2024
@@ -57,7 +57,7 @@ editor_init(struct browser *browser, struct repo_amend
short fh, off;
struct tm *ttm = NULL;
- editor = xmalloczero(sizeof(struct editor), "editor");
+ editor = xmalloczero(sizeof(struct editor));
editor->browser = browser;
editor->amendment = amendment;
@@ -154,7 +154,7 @@ editor_init(struct browser *browser, struct repo_amend
editor->last_te = editor->author_te;
- focusable = xmalloczero(sizeof(struct focusable), "editor focusable");
+ focusable = xmalloczero(sizeof(struct focusable));
focusable->cookie = editor;
focusable->win = editor->win;
focusable->modal = true;
@@ -443,7 +443,7 @@ editor_save(struct editor *editor)
return;
}
- date = xmalloc(len + 1, "editor_save");
+ date = xmalloc(len + 1);
memcpy(date, *(*(editor->date_te))->hText, len);
date[len] = '\0';
--- focusable.c Sat Mar 4 22:39:16 2023
+++ focusable.c Fri Feb 16 09:32:50 2024
@@ -158,7 +158,7 @@ focusables_quit(void)
* nfocusables and focusables array will probably be
* modified as each focusable quits
*/
- tfocusables = xcalloc(sizeof(Ptr), tnfocusables, "tfocusables");
+ tfocusables = xcalloc(sizeof(Ptr), tnfocusables);
memcpy(tfocusables, focusables, sizeof(Ptr) * tnfocusables);
for (n = 0; n < tnfocusables; n++) {
--- main.c Wed Mar 29 11:56:25 2023
+++ main.c Fri Feb 16 13:42:20 2024
@@ -16,6 +16,7 @@
#include <stdio.h>
#include <string.h>
+#include <stdarg.h>
#include "amend.h"
#include "browser.h"
@@ -28,6 +29,13 @@
MenuHandle apple_menu, file_menu, edit_menu, repo_menu, amendment_menu;
bool quitting = false;
+#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
+
bool handle_menu(long menu_id);
int
@@ -66,6 +74,11 @@ main(void)
repo_menu = GetMHandle(REPO_MENU_ID);
amendment_menu = GetMHandle(AMENDMENT_MENU_ID);
menu_defaults();
+#ifdef MALLOC_DEBUG
+ debug_menu = NewMenu(DEBUG_MENU_DUMP_ID, "\pDebug");
+ AppendMenu(debug_menu, "\pDump Allocations");
+ InsertMenu(debug_menu, 0);
+#endif
DrawMenuBar();
/* see if we were started by double-clicking a .repo file */
@@ -236,6 +249,38 @@ handle_menu(long menu_id)
break;
}
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, ":amend_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:
ret = false;
}
@@ -244,6 +289,32 @@ handled:
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
menu_defaults(void)
--- repo.c Thu Jan 11 14:23:38 2024
+++ repo.c Fri Feb 16 09:38:05 2024
@@ -105,7 +105,7 @@ repo_init(struct bile *bile, bool is_new)
unsigned long *ids;
bool canceled = false;
- repo = xmalloczero(sizeof(struct repo), "repo");
+ repo = xmalloczero(sizeof(struct repo));
repo->bile = bile;
repo->next_file_id = 1;
repo->next_amendment_id = 1;
@@ -120,15 +120,14 @@ repo_init(struct bile *bile, bool is_new)
/* fill in file info */
repo->nfiles = bile_sorted_ids_by_type(bile, REPO_FILE_RTYPE, &ids);
if (repo->nfiles) {
- repo->files = xcalloc(repo->nfiles, sizeof(Ptr), "repo files");
+ repo->files = xcalloc(repo->nfiles, sizeof(Ptr));
for (i = 0; i < repo->nfiles; i++) {
if (i == 0 || i == repo->nfiles - 1 || ((i + 1) % 10) == 0)
progress("Loading file %d/%d...", i + 1, repo->nfiles);
size = bile_read_alloc(bile, REPO_FILE_RTYPE, ids[i], &data);
if (size == 0)
panic("failed fetching file %ld", ids[i]);
- repo->files[i] = xmalloc(sizeof(struct repo_file),
- "repo file");
+ repo->files[i] = xmalloc(sizeof(struct repo_file));
repo->files[i] = repo_parse_file(ids[i], (unsigned char *)data,
size);
if (repo->files[i]->id >= repo->next_file_id)
@@ -188,8 +187,7 @@ repo_load_amendments(struct repo *repo, bool fill_in)
return true;
if (!fill_in) {
- repo->amendments = xcalloc(repo->namendments, sizeof(Ptr),
- "repo amendments");
+ repo->amendments = xcalloc(repo->namendments, sizeof(Ptr));
repo->unloaded_amendments = (settings.max_amendments > 0 &&
repo->namendments > settings.max_amendments);
@@ -273,7 +271,7 @@ repo_parse_file(unsigned long id, unsigned char *data,
datapos = 0;
- file = xmalloczero(sizeof(struct repo_file), "repo_parse_file");
+ file = xmalloczero(sizeof(struct repo_file));
file->id = id;
/* filename, pstr */
@@ -319,8 +317,7 @@ repo_parse_amendment(unsigned long id, unsigned char *
struct repo_amendment *amendment;
unsigned short len, i;
- amendment = xmalloc(sizeof(struct repo_amendment),
- "repo_parse_amendment");
+ amendment = xmalloc(sizeof(struct repo_amendment));
amendment->id = id;
/* date */
@@ -343,8 +340,7 @@ repo_parse_amendment(unsigned long id, unsigned char *
data += 2;
if (amendment->nfiles) {
- amendment->file_ids = xcalloc(amendment->nfiles, sizeof(short),
- "amendment file_ids");
+ amendment->file_ids = xcalloc(amendment->nfiles, sizeof(short));
for (i = 0; i < amendment->nfiles; i++) {
amendment->file_ids[i] = (data[0] << 8) | data[1];
data += 2;
@@ -395,7 +391,7 @@ repo_diff_header(struct repo *repo, struct repo_amendm
unsigned short header_len;
short i;
- *ret = xmalloc(128 + amendment->log_len, "repo_diff_header");
+ *ret = xmalloc(128 + amendment->log_len);
ttm = localtime(&amendment->date);
header_len = sprintf(*ret,
"Author: %s\r"
@@ -456,7 +452,7 @@ repo_show_diff_text(struct repo *repo, struct repo_ame
trunc = 1;
}
- dtext = xmalloc(all_len, "repo_show_diff_text");
+ dtext = xmalloc(all_len);
memcpy(dtext, buf, header_len);
xfree(&buf);
@@ -582,7 +578,7 @@ repo_add_file(struct repo *repo)
repo->nfiles++;
repo->files = xrealloc(repo->files, repo->nfiles * sizeof(Ptr));
file = repo->files[repo->nfiles - 1] =
- xmalloczero(sizeof(struct repo_file), "repo_add_file");
+ xmalloczero(sizeof(struct repo_file));
file->id = repo->next_file_id;
repo->next_file_id++;
@@ -631,7 +627,7 @@ repo_file_update(struct repo *repo, struct repo_file *
/* filename len, filename, type, creator, ctime, mtime, flags */
len = 1 + filename[0] + 4 + 4 + 4 + 4 + 1;
- data = xmalloczero(len, "repo_file_update");
+ data = xmalloczero(len);
datapos = 0;
/* copy filename as pstr */
@@ -725,7 +721,7 @@ repo_checkout_file(struct repo *repo, struct repo_file
panic("Failed to truncate file %s: %d", PtoCstr(filename), error);
/* TODO: add offset to bile_read to read in chunks */
- text = xmalloc(textob->size, "repo_checkout_file");
+ text = xmalloc(textob->size);
size = bile_read_object(repo->bile, textob, text, textob->size);
if (size != textob->size)
panic("Failed to read text object %ld: %d", textob->id,
@@ -968,7 +964,7 @@ repo_export_amendment(struct repo *repo, struct repo_a
error);
xfree(&buf);
- buf = xmalloc(bob->size, "repo_export_amendment");
+ buf = xmalloc(bob->size);
size = bile_read_object(repo->bile, bob, buf, bob->size);
error = FSWrite(frefnum, &size, buf);
if (error)
@@ -993,15 +989,13 @@ repo_amend(struct repo *repo, struct diffed_file *diff
size_t size;
short i, error, frefnum;
- amendment = xmalloczero(sizeof(struct repo_amendment),
- "repo_amend amendment");
+ amendment = xmalloczero(sizeof(struct repo_amendment));
amendment->id = repo->next_amendment_id;
amendment->date = Time;
/* find files with actual data changes */
amendment->nfiles = 0;
- amendment->file_ids = xcalloc(sizeof(short), nfiles,
- "repo_amend file_ids");
+ amendment->file_ids = xcalloc(sizeof(short), nfiles);
for (i = 0; i < nfiles; i++) {
if (diffed_files[i].flags & DIFFED_FILE_TEXT) {
amendment->file_ids[amendment->nfiles] =
@@ -1072,7 +1066,7 @@ repo_amend(struct repo *repo, struct diffed_file *diff
panic("Failed to get size of file %s: %d",
PtoCstr(tfilename), error);
- tdata = xmalloc(fsize, "repo_amend data");
+ tdata = xmalloc(fsize);
error = FSRead(frefnum, &fsize, tdata);
if (error)
panic("Failed to read %ul of file %s: %d", fsize,
@@ -1132,7 +1126,7 @@ repo_marshall_amendment(struct repo_amendment *amendme
/* log (wstr) */
len += sizeof(short) + amendment->log_len;
- *retdata = xmalloc(len, "repo_marshall_amendment");
+ *retdata = xmalloc(len);
data = *retdata;
data[pos++] = (amendment->date >> 24) & 0xff;