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.
 
 
 

115 lines
2.5 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 <stdio.h>
  23. #include <string.h>
  24. #include <err.h>
  25. void (*xmem_error)(const char *, unsigned int) = NULL;
  26. #define mem_error(f, l) \
  27. do { \
  28. if ( xmem_error ) \
  29. (*xmem_error)((f), (l)); \
  30. err(EXIT_FAILURE, "%s, %d", (f), (l)); \
  31. } while ( 0 )
  32. void *
  33. do_malloc(size_t s, const char *file, unsigned int line)
  34. {
  35. void *p;
  36. if ( ! (p = malloc(s)) && s )
  37. mem_error(file, line);
  38. return p;
  39. }
  40. void *
  41. do_calloc(size_t n, size_t s, const char *file, unsigned int line)
  42. {
  43. void *p;
  44. if ( ! (p = calloc(n, s)) && n && s )
  45. mem_error(file, line);
  46. return p;
  47. }
  48. void *
  49. do_realloc(void *p, size_t s, const char *file, unsigned int line)
  50. {
  51. void *np;
  52. if ( ! (np = realloc(p, s)) && s )
  53. mem_error(file, line);
  54. return np;
  55. }
  56. char *
  57. do_strdup(const char *s, const char *file, unsigned int line)
  58. {
  59. char *dup;
  60. if ( ! (dup = strdup(s)) && s )
  61. mem_error(file, line);
  62. return dup;
  63. }
  64. int
  65. do_asprintf(char **s,
  66. const char *fmt,
  67. const char *file,
  68. unsigned int line,
  69. ...)
  70. {
  71. int ret;
  72. va_list ap;
  73. va_start(ap, line);
  74. ret = vasprintf(s, fmt, ap);
  75. va_end(ap);
  76. if ( ret == -1 )
  77. mem_error(file, line);
  78. return ret;
  79. }
  80. int
  81. do_vasprintf(char **s,
  82. const char *fmt,
  83. const char *file,
  84. unsigned int line,
  85. va_list ap)
  86. {
  87. int ret;
  88. if ( (ret = vasprintf(s, fmt, ap)) == -1 )
  89. mem_error(file, line);
  90. return ret;
  91. }