xalloc.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * memory wrappers
  3. *
  4. * Copyright (c) Artem Bityutskiy, 2007, 2008
  5. * Copyright 2001, 2002 Red Hat, Inc.
  6. * 2001 David A. Schleef <ds@lineo.com>
  7. * 2002 Axis Communications AB
  8. * 2001, 2002 Erik Andersen <andersen@codepoet.org>
  9. * 2004 University of Szeged, Hungary
  10. * 2006 KaiGai Kohei <kaigai@ak.jp.nec.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  20. * the GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. */
  26. #ifndef __MTD_UTILS_XALLOC_H__
  27. #define __MTD_UTILS_XALLOC_H__
  28. #include <stdarg.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. /*
  32. * Mark these functions as unused so that gcc does not emit warnings
  33. * when people include this header but don't use every function.
  34. */
  35. __attribute__((unused))
  36. static void *xmalloc(size_t size)
  37. {
  38. void *ptr = malloc(size);
  39. if (ptr == NULL && size != 0)
  40. sys_errmsg_die("out of memory");
  41. return ptr;
  42. }
  43. __attribute__((unused))
  44. static void *xcalloc(size_t nmemb, size_t size)
  45. {
  46. void *ptr = calloc(nmemb, size);
  47. if (ptr == NULL && nmemb != 0 && size != 0)
  48. sys_errmsg_die("out of memory");
  49. return ptr;
  50. }
  51. __attribute__((unused))
  52. static void *xzalloc(size_t size)
  53. {
  54. return xcalloc(1, size);
  55. }
  56. __attribute__((unused))
  57. static void *xrealloc(void *ptr, size_t size)
  58. {
  59. ptr = realloc(ptr, size);
  60. if (ptr == NULL && size != 0)
  61. sys_errmsg_die("out of memory");
  62. return ptr;
  63. }
  64. __attribute__((unused))
  65. static char *xstrdup(const char *s)
  66. {
  67. char *t;
  68. if (s == NULL)
  69. return NULL;
  70. t = strdup(s);
  71. if (t == NULL)
  72. sys_errmsg_die("out of memory");
  73. return t;
  74. }
  75. #ifdef _GNU_SOURCE
  76. __attribute__((unused))
  77. static int xasprintf(char **strp, const char *fmt, ...)
  78. {
  79. int cnt;
  80. va_list ap;
  81. va_start(ap, fmt);
  82. cnt = vasprintf(strp, fmt, ap);
  83. va_end(ap);
  84. if (cnt == -1)
  85. sys_errmsg_die("out of memory");
  86. return cnt;
  87. }
  88. #endif
  89. #endif /* !__MTD_UTILS_XALLOC_H__ */