6 /* My own versions of malloc, realloc and free. Because I want malloc and
7 * realloc to bomb out and exit the program if they run out of memory,
8 * realloc to reliably call malloc if passed a NULL pointer, and free
9 * to reliably do nothing if passed a NULL pointer. Of course we can also
10 * put trace printouts in, if we need to. */
13 static FILE *fp
= NULL
;
15 void mlog(char *file
, int line
) {
17 fp
= fopen("putty_mem.log", "w");
18 setvbuf(fp
, NULL
, _IONBF
, BUFSIZ
);
21 fprintf (fp
, "%s:%d: ", file
, line
);
25 void *safemalloc(size_t size
) {
26 void *p
= malloc (size
);
28 MessageBox(NULL
, "Out of memory!", "PuTTY Fatal Error",
29 MB_SYSTEMMODAL
| MB_ICONERROR
| MB_OK
);
34 fprintf(fp
, "malloc(%d) returns %p\n", size
, p
);
39 void *saferealloc(void *ptr
, size_t size
) {
44 p
= realloc (ptr
, size
);
46 MessageBox(NULL
, "Out of memory!", "PuTTY Fatal Error",
47 MB_SYSTEMMODAL
| MB_ICONERROR
| MB_OK
);
52 fprintf(fp
, "realloc(%p,%d) returns %p\n", ptr
, size
, p
);
57 void safefree(void *ptr
) {
61 fprintf(fp
, "free(%p)\n", ptr
);
67 fprintf(fp
, "freeing null pointer - no action taken\n");