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.
 
 
 

58 lines
929 B

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