You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

util.c 950 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /* $Id: util.c,v 1.2 1999/11/23 05:39:27 tausq Exp $ */
  2. #include "util.h"
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include "slangui.h"
  6. #include "macros.h"
  7. #ifdef DEBUG
  8. static int _num_mallocs = 0;
  9. char *safe_strdup(const char *s)
  10. {
  11. char *p;
  12. if (s != NULL) {
  13. p = strdup(s);
  14. _num_mallocs++;
  15. if (p == NULL) DIE("Cannot allocate memory for strdup");
  16. return p;
  17. } else {
  18. return NULL;
  19. }
  20. }
  21. void *safe_malloc(int size)
  22. {
  23. void *p;
  24. if (size == 0) {
  25. DPRINTF("Attempting to allocate 0 bytes!");
  26. return NULL;
  27. }
  28. p = malloc(size);
  29. _num_mallocs++;
  30. if (p == NULL) DIE("Cannot allocate %d bytes of memory", size);
  31. return p;
  32. }
  33. void safe_free(void **p)
  34. {
  35. if (p == NULL || *p == NULL) {
  36. DPRINTF("Attempting to dereference NULL pointer");
  37. } else {
  38. free(*p);
  39. }
  40. if (p) *p = NULL;
  41. _num_mallocs--;
  42. }
  43. void memleak_check(void)
  44. {
  45. DPRINTF("Outstanding mallocs : %d\n", _num_mallocs);
  46. }
  47. #endif