1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-07-15 18:17:32 -05:00

Move the malloc helpers out of misc.c.

Now they live in their own file memory.c. The advantage of this is
that you can link them into a binary without also pulling in the rest
of misc.c with its various dependencies on other parts of the code,
such as conf.c.
This commit is contained in:
Simon Tatham
2018-11-27 19:20:32 +00:00
parent 1586a41656
commit abec9e1c7e
4 changed files with 123 additions and 110 deletions

102
misc.c
View File

@ -850,12 +850,6 @@ void sanitise_term_data(bufchain *out, const void *vdata, int len)
* one.
*/
#ifdef MINEFIELD
void *minefield_c_malloc(size_t size);
void minefield_c_free(void *p);
void *minefield_c_realloc(void *p, size_t size);
#endif
#ifdef MALLOC_LOG
static FILE *fp = NULL;
@ -875,102 +869,6 @@ void mlog(char *file, int line)
}
#endif
void *safemalloc(size_t n, size_t size)
{
void *p;
if (n > INT_MAX / size) {
p = NULL;
} else {
size *= n;
if (size == 0) size = 1;
#ifdef MINEFIELD
p = minefield_c_malloc(size);
#else
p = malloc(size);
#endif
}
if (!p) {
char str[200];
#ifdef MALLOC_LOG
sprintf(str, "Out of memory! (%s:%d, size=%d)",
mlog_file, mlog_line, size);
fprintf(fp, "*** %s\n", str);
fclose(fp);
#else
strcpy(str, "Out of memory!");
#endif
modalfatalbox("%s", str);
}
#ifdef MALLOC_LOG
if (fp)
fprintf(fp, "malloc(%d) returns %p\n", size, p);
#endif
return p;
}
void *saferealloc(void *ptr, size_t n, size_t size)
{
void *p;
if (n > INT_MAX / size) {
p = NULL;
} else {
size *= n;
if (!ptr) {
#ifdef MINEFIELD
p = minefield_c_malloc(size);
#else
p = malloc(size);
#endif
} else {
#ifdef MINEFIELD
p = minefield_c_realloc(ptr, size);
#else
p = realloc(ptr, size);
#endif
}
}
if (!p) {
char str[200];
#ifdef MALLOC_LOG
sprintf(str, "Out of memory! (%s:%d, size=%d)",
mlog_file, mlog_line, size);
fprintf(fp, "*** %s\n", str);
fclose(fp);
#else
strcpy(str, "Out of memory!");
#endif
modalfatalbox("%s", str);
}
#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
#ifdef MINEFIELD
minefield_c_free(ptr);
#else
free(ptr);
#endif
}
#ifdef MALLOC_LOG
else if (fp)
fprintf(fp, "freeing null pointer - no action taken\n");
#endif
}
/* ----------------------------------------------------------------------
* Debugging routines.
*/