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");
72 static FILE *debug_fp
= NULL
;
73 static int debug_got_console
= 0;
75 void dprintf(char *fmt
, ...) {
80 if (!debug_got_console
) {
82 debug_got_console
= 1;
85 debug_fp
= fopen("debug.log", "w");
89 vsprintf(buf
, fmt
, ap
);
90 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE
), buf
, strlen(buf
), &dw
, NULL
);