mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-01-09 17:38:00 +00:00
24c7f9b058
[originally from svn r40]
65 lines
1.4 KiB
C
65 lines
1.4 KiB
C
/* $Id: misc.c,v 1.2.2.1 1999/02/19 15:24:15 ben Exp $ */
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include "putty.h"
|
|
|
|
/* My own versions of malloc, realloc and free. Because I want malloc and
|
|
* realloc to bomb out and exit the program if they run out of memory,
|
|
* realloc to reliably call malloc if passed a NULL pointer, and free
|
|
* to reliably do nothing if passed a NULL pointer. Of course we can also
|
|
* put trace printouts in, if we need to. */
|
|
|
|
#ifdef MALLOC_LOG
|
|
static FILE *fp = NULL;
|
|
|
|
void mlog(char *file, int line) {
|
|
if (!fp) {
|
|
fp = fopen("putty_mem.log", "w");
|
|
setvbuf(fp, NULL, _IONBF, BUFSIZ);
|
|
}
|
|
if (fp)
|
|
fprintf (fp, "%s:%d: ", file, line);
|
|
}
|
|
#endif
|
|
|
|
void *safemalloc(size_t size) {
|
|
void *p = malloc (size);
|
|
if (!p)
|
|
fatalbox("%s", "Out of memory!");
|
|
#ifdef MALLOC_LOG
|
|
if (fp)
|
|
fprintf(fp, "malloc(%d) returns %p\n", size, p);
|
|
#endif
|
|
return p;
|
|
}
|
|
|
|
void *saferealloc(void *ptr, size_t size) {
|
|
void *p;
|
|
if (!ptr)
|
|
p = malloc (size);
|
|
else
|
|
p = realloc (ptr, size);
|
|
if (!p)
|
|
fatalbox("%s", "Out of memory!");
|
|
#ifdef MALLOC_LOG
|
|
if (fp)
|
|
fprintf(fp, "realloc(%p,%d) returns %p\n", ptr, size, p);
|
|
#endif
|
|
return p;
|
|
}
|
|
|
|
void safefree(void *ptr) {
|
|
if (ptr) {
|
|
#ifdef MALLOC_LOG
|
|
if (fp)
|
|
fprintf(fp, "free(%p)\n", ptr);
|
|
#endif
|
|
free (ptr);
|
|
}
|
|
#ifdef MALLOC_LOG
|
|
else if (fp)
|
|
fprintf(fp, "freeing null pointer - no action taken\n");
|
|
#endif
|
|
}
|