PageRenderTime 62ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/src/wml_backend/p2_mp4h/lib/xmalloc.c

https://bitbucket.org/shlomif/website-meta-language
C | 133 lines | 88 code | 21 blank | 24 comment | 17 complexity | 4a2e17390058adda168dbb61867e91e1 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.0
  1. /* xmalloc.c -- malloc with out of memory checking
  2. Copyright (C) 1990, 91, 92, 93, 94, 95, 96 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software Foundation,
  13. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  14. #if HAVE_CONFIG_H
  15. # include <config.h>
  16. #endif
  17. #include <sys/types.h>
  18. #if STDC_HEADERS
  19. # include <stdlib.h>
  20. #else
  21. void *calloc ();
  22. void *malloc ();
  23. void *realloc ();
  24. void free ();
  25. #endif
  26. #if ENABLE_NLS
  27. # include <libintl.h>
  28. # define _(Text) gettext (Text)
  29. #else
  30. # define textdomain(Domain)
  31. # define _(Text) Text
  32. #endif
  33. #include "error.h"
  34. #ifndef EXIT_FAILURE
  35. # define EXIT_FAILURE 1
  36. #endif
  37. /* Prototypes for functions defined here. */
  38. #if defined (__STDC__) && __STDC__
  39. static void *fixup_null_alloc (size_t n);
  40. void *xmalloc (size_t n);
  41. void *xcalloc (size_t n, size_t s);
  42. void *xrealloc (void *p, size_t n);
  43. void xfree (void *p);
  44. #endif
  45. /* Exit value when the requested amount of memory is not available.
  46. The caller may set it to some other value. */
  47. int xmalloc_exit_failure = EXIT_FAILURE;
  48. #if defined(HAVE_STDARG_H) && (defined(HAVE_VPRINTF) || defined(HAVE_DOPRNT))
  49. void error (int, int, const char *, ...);
  50. #else
  51. void error ();
  52. #endif
  53. static void *
  54. fixup_null_alloc (n)
  55. size_t n;
  56. {
  57. void *p;
  58. p = 0;
  59. if (n == 0)
  60. p = malloc ((size_t) 1);
  61. if (p == 0)
  62. error (xmalloc_exit_failure, 0, _("Memory exhausted"));
  63. return p;
  64. }
  65. /* Allocate N bytes of memory dynamically, with error checking. */
  66. void *
  67. xmalloc (n)
  68. size_t n;
  69. {
  70. void *p;
  71. p = malloc (n);
  72. if (p == 0)
  73. p = fixup_null_alloc (n);
  74. return p;
  75. }
  76. /* Allocate memory for N elements of S bytes, with error checking. */
  77. void *
  78. xcalloc (n, s)
  79. size_t n, s;
  80. {
  81. void *p;
  82. p = calloc (n, s);
  83. if (p == 0)
  84. p = fixup_null_alloc (n);
  85. return p;
  86. }
  87. /* Change the size of an allocated block of memory P to N bytes,
  88. with error checking.
  89. If P is NULL, run xmalloc. */
  90. void *
  91. xrealloc (p, n)
  92. void *p;
  93. size_t n;
  94. {
  95. if (p == 0)
  96. return xmalloc (n);
  97. p = realloc (p, n);
  98. if (p == 0)
  99. p = fixup_null_alloc (n);
  100. return p;
  101. }
  102. void
  103. xfree (p)
  104. void *p;
  105. {
  106. if (p != 0)
  107. free(p);
  108. p = 0;
  109. }