malloc.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* xmalloc.c -- malloc with out of memory checking
  2. Copyright (C) 1990,91,92,93,94,95,96,97 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #ifdef HAVE_CONFIG_H
  17. #include "config.h"
  18. #endif
  19. #include <stdio.h>
  20. #ifdef HAVE_UNISTD_H
  21. #include <unistd.h>
  22. #endif
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include <sd/error.h>
  26. #if defined(__APPLE__)
  27. # include <sys/time.h>
  28. # include <crt_externs.h>
  29. # define environ (*_NSGetEnviron())
  30. #endif /* __APPLE__ */
  31. typedef void (*sd_malloc_handler_t)();
  32. static char* first_break = NULL;
  33. static sd_malloc_handler_t handler = NULL;
  34. //static void *
  35. //fixup_null_alloc (n)
  36. //size_t n;
  37. static void * fixup_null_alloc (size_t n)
  38. {
  39. void* p = 0;
  40. #ifdef HAVE_SBRK
  41. if (n == 0)
  42. p = malloc ((size_t) 1);
  43. if (p == 0) {
  44. extern char **environ;
  45. size_t allocated;
  46. if (first_break != NULL)
  47. allocated = (char *) sbrk (0) - first_break;
  48. else
  49. allocated = (char *) sbrk (0) - (char *) &environ;
  50. sd_error("\nCannot allocate %lu bytes after allocating %lu bytes\n",
  51. (unsigned long) n, (unsigned long) allocated);
  52. if (handler)
  53. handler();
  54. else {
  55. sd_error("\n\tMemory exhausted !! Aborting.\n");
  56. abort();
  57. }
  58. }
  59. #endif
  60. return p;
  61. }
  62. //sd_malloc_handler_t
  63. //sd_malloc_set_handler(a_handler)
  64. //sd_malloc_handler_t a_handler;
  65. sd_malloc_handler_t sd_malloc_set_handler(sd_malloc_handler_t a_handler)
  66. {
  67. sd_malloc_handler_t previous = handler;
  68. handler = a_handler;
  69. return previous;
  70. }
  71. /* Allocate N bytes of memory dynamically, with error checking. */
  72. //void *
  73. //sd_malloc (n)
  74. //size_t n;
  75. void *sd_malloc (size_t n)
  76. {
  77. void *p;
  78. p = malloc (n);
  79. if (p == 0)
  80. p = fixup_null_alloc (n);
  81. return p;
  82. }
  83. /* Allocate memory for N elements of S bytes, with error checking. */
  84. //void *
  85. //sd_calloc (n, s)
  86. //size_t n, s;
  87. void *sd_calloc (size_t n, size_t s)
  88. {
  89. void *p;
  90. p = calloc (n, s);
  91. if (p == 0)
  92. p = fixup_null_alloc (n * s);
  93. return p;
  94. }
  95. /* Change the size of an allocated block of memory P to N bytes,
  96. with error checking.
  97. If P is NULL, run sd_malloc. */
  98. //void *
  99. //sd_realloc (p, n)
  100. //void *p;
  101. //size_t n;
  102. void *sd_realloc (void *p, size_t n)
  103. {
  104. if (p == 0)
  105. return sd_malloc (n);
  106. p = realloc (p, n);
  107. if (p == 0)
  108. p = fixup_null_alloc (n);
  109. return p;
  110. }
  111. /* Return a newly allocated copy of STRING. */
  112. //char *
  113. //sd_strdup (string)
  114. //const char *string;
  115. char *sd_strdup (const char *string)
  116. {
  117. return strcpy ((char *)sd_malloc (strlen (string) + 1), string);
  118. }