| 1 | /* |
| 2 | * PuTTY memory-handling header. |
| 3 | */ |
| 4 | |
| 5 | #ifndef PUTTY_PUTTYMEM_H |
| 6 | #define PUTTY_PUTTYMEM_H |
| 7 | |
| 8 | #include <stddef.h> /* for size_t */ |
| 9 | #include <string.h> /* for memcpy() */ |
| 10 | |
| 11 | |
| 12 | /* #define MALLOC_LOG do this if you suspect putty of leaking memory */ |
| 13 | #ifdef MALLOC_LOG |
| 14 | #define smalloc(z) (mlog(__FILE__,__LINE__), safemalloc(z)) |
| 15 | #define srealloc(y,z) (mlog(__FILE__,__LINE__), saferealloc(y,z)) |
| 16 | #define sfree(z) (mlog(__FILE__,__LINE__), safefree(z)) |
| 17 | void mlog(char *, int); |
| 18 | #else |
| 19 | #define smalloc safemalloc |
| 20 | #define srealloc saferealloc |
| 21 | #define sfree safefree |
| 22 | #endif |
| 23 | |
| 24 | void *safemalloc(size_t); |
| 25 | void *saferealloc(void *, size_t); |
| 26 | void safefree(void *); |
| 27 | |
| 28 | /* |
| 29 | * Direct use of smalloc within the code should be avoided where |
| 30 | * possible, in favour of these type-casting macros which ensure |
| 31 | * you don't mistakenly allocate enough space for one sort of |
| 32 | * structure and assign it to a different sort of pointer. |
| 33 | */ |
| 34 | #define snew(type) ((type *)smalloc(sizeof(type))) |
| 35 | #define snewn(n, type) ((type *)smalloc((n)*sizeof(type))) |
| 36 | #define sresize(ptr, n, type) ((type *)srealloc(ptr, (n)*sizeof(type))) |
| 37 | |
| 38 | #endif |