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.
 
 
 

68 lines
1.1 KiB

  1. /* $Id: util.c,v 1.4 2001/11/22 17:53:48 tausq Rel $ */
  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_realloc(void *x, size_t size)
  34. {
  35. void *p;
  36. p = realloc(x, size);
  37. _num_mallocs++;
  38. if (p == NULL) DIE(_("Cannot reallocate %d bytes of memory"), size);
  39. return p;
  40. }
  41. void safe_free(void **p)
  42. {
  43. if (p == NULL || *p == NULL) {
  44. DPRINTF("Attempting to dereference NULL pointer");
  45. } else {
  46. free(*p);
  47. }
  48. if (p) *p = NULL;
  49. _num_mallocs--;
  50. }
  51. void memleak_check(void)
  52. {
  53. DPRINTF("Outstanding mallocs : %d\n", _num_mallocs);
  54. }
  55. #endif