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");
19 fprintf (fp, "%s:%d: ", file, line);
23 void *safemalloc(size_t size) {
24 void *p = malloc (size);
26 MessageBox(NULL, "Out of memory!", "PuTTY Fatal Error",
27 MB_SYSTEMMODAL | MB_ICONERROR | MB_OK);
32 fprintf(fp, "malloc(%d) returns %p\n", size, p);
37 void *saferealloc(void *ptr, size_t size) {
42 p = realloc (ptr, size);
44 MessageBox(NULL, "Out of memory!", "PuTTY Fatal Error",
45 MB_SYSTEMMODAL | MB_ICONERROR | MB_OK);
50 fprintf(fp, "realloc(%p,%d) returns %p\n", ptr, size, p);
55 void safefree(void *ptr) {
59 fprintf(fp, "free(%p)\n", ptr);
65 fprintf(fp, "freeing null pointer - no action taken\n");