PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/src/wml_backend/p4_gm4/lib/xmalloc.c

https://bitbucket.org/shlomif/website-meta-language
C | 129 lines | 84 code | 21 blank | 24 comment | 15 complexity | 2db9005dc2634fa128f4da6fe3a83243 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. #if __STDC__
  18. # define VOID void
  19. #else
  20. # define VOID char
  21. #endif
  22. #include <sys/types.h>
  23. #if defined(STDC_HEADERS)
  24. # include <stdlib.h>
  25. #else
  26. VOID *calloc ();
  27. VOID *malloc ();
  28. VOID *realloc ();
  29. void free ();
  30. #endif
  31. #if ENABLE_NLS
  32. # include <libintl.h>
  33. # define _(Text) gettext (Text)
  34. #else
  35. # define textdomain(Domain)
  36. # define _(Text) Text
  37. #endif
  38. #include "error.h"
  39. #ifndef EXIT_FAILURE
  40. # define EXIT_FAILURE 1
  41. #endif
  42. /* Prototypes for functions defined here. */
  43. #if defined (__STDC__) && __STDC__
  44. static VOID *fixup_null_alloc (size_t n);
  45. VOID *xmalloc (size_t n);
  46. VOID *xcalloc (size_t n, size_t s);
  47. VOID *xrealloc (VOID *p, size_t n);
  48. #endif
  49. /* Exit value when the requested amount of memory is not available.
  50. The caller may set it to some other value. */
  51. int xmalloc_exit_failure = EXIT_FAILURE;
  52. #if defined(__STDC__) && (defined(HAVE_VPRINTF) || defined(HAVE_DOPRNT))
  53. void error (int, int, const char *, ...);
  54. #else
  55. void error ();
  56. #endif
  57. static VOID *
  58. fixup_null_alloc (n)
  59. size_t n;
  60. {
  61. VOID *p;
  62. p = 0;
  63. if (n == 0)
  64. p = malloc ((size_t) 1);
  65. if (p == 0)
  66. error (xmalloc_exit_failure, 0, _("Memory exhausted"));
  67. return p;
  68. }
  69. /* Allocate N bytes of memory dynamically, with error checking. */
  70. VOID *
  71. xmalloc (n)
  72. size_t n;
  73. {
  74. VOID *p;
  75. p = malloc (n);
  76. if (p == 0)
  77. p = fixup_null_alloc (n);
  78. return p;
  79. }
  80. /* Allocate memory for N elements of S bytes, with error checking. */
  81. VOID *
  82. xcalloc (n, s)
  83. size_t n, s;
  84. {
  85. VOID *p;
  86. p = calloc (n, s);
  87. if (p == 0)
  88. p = fixup_null_alloc (n);
  89. return p;
  90. }
  91. /* Change the size of an allocated block of memory P to N bytes,
  92. with error checking.
  93. If P is NULL, run xmalloc. */
  94. VOID *
  95. xrealloc (p, n)
  96. VOID *p;
  97. size_t n;
  98. {
  99. if (p == 0)
  100. return xmalloc (n);
  101. p = realloc (p, n);
  102. if (p == 0)
  103. p = fixup_null_alloc (n);
  104. return p;
  105. }