1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-10 09:58:01 +00:00
putty-source/misc.c
Simon Tatham bbbda4110b Created a shiny new abstraction for the socket handling. Has many
advantages:
 - protocol modules can call sk_write() without having to worry
   about writes blocking, because blocking writes are handled in the
   abstraction layer and retried later.
 - `Lost connection while sending' is a thing of the past.
 - <winsock.h> is no longer needed in most modules, because
   "putty.h" doesn't have to declare `SOCKET' variables any more,
   only the abstracted `Socket' type.
 - select()-equivalent between multiple sockets will now be handled
   sensibly, which opens the way for things like SSH port
   forwarding.

[originally from svn r744]
2000-10-23 10:32:37 +00:00

70 lines
1.5 KiB
C

#include <windows.h>
#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) {
MessageBox(NULL, "Out of memory!", "PuTTY Fatal Error",
MB_SYSTEMMODAL | MB_ICONERROR | MB_OK);
exit(1);
}
#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) {
MessageBox(NULL, "Out of memory!", "PuTTY Fatal Error",
MB_SYSTEMMODAL | MB_ICONERROR | MB_OK);
exit(1);
}
#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
}