A mail formatter.
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.
 
 
 

79 lines
1.8 KiB

  1. /*
  2. * xmem - Incenp.org Notch Library: die-on-error memory functions
  3. * Copyright (C) 2011 Damien Goutte-Gattat
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. #include <config.h>
  20. #endif
  21. #include <xmem.h>
  22. #include <string.h>
  23. #include <err.h>
  24. void (*xmem_error)(const char *, unsigned int) = NULL;
  25. #define mem_error(f, l) \
  26. do { \
  27. if ( xmem_error ) \
  28. (*xmem_error)((f), (l)); \
  29. err(EXIT_FAILURE, "%s, %d", (f), (l)); \
  30. } while ( 0 )
  31. void *
  32. do_malloc(size_t s, const char *file, unsigned int line)
  33. {
  34. void *p;
  35. if ( ! (p = malloc(s)) && s )
  36. mem_error(file, line);
  37. return p;
  38. }
  39. void *
  40. do_calloc(size_t n, size_t s, const char *file, unsigned int line)
  41. {
  42. void *p;
  43. if ( ! (p = calloc(n, s)) && n && s )
  44. mem_error(file, line);
  45. return p;
  46. }
  47. void *
  48. do_realloc(void *p, size_t s, const char *file, unsigned int line)
  49. {
  50. void *np;
  51. if ( ! (np = realloc(p, s)) && s )
  52. mem_error(file, line);
  53. return np;
  54. }
  55. char *
  56. do_strdup(const char *s, const char *file, unsigned int line)
  57. {
  58. char *dup;
  59. if ( ! (dup = strdup(s)) && s )
  60. mem_error(file, line);
  61. return dup;
  62. }