puttymem.h 1.3 KB

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