malloc.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 <tchar.h>
  20. #include <stdio.h>
  21. #ifdef HAVE_UNISTD_H
  22. #include <unistd.h>
  23. #endif
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include "sd/error.h"
  27. #if defined(__APPLE__)
  28. # include <sys/time.h>
  29. # include <crt_externs.h>
  30. # define environ (*_NSGetEnviron())
  31. #endif /* __APPLE__ */
  32. typedef void (*sd_malloc_handler_t)();
  33. static TCHAR* first_break = NULL;
  34. static sd_malloc_handler_t handler = NULL;
  35. //static void *
  36. //fixup_null_alloc (n)
  37. //size_t n;
  38. static void * fixup_null_alloc(size_t n)
  39. {
  40. void* p = 0;
  41. #ifdef HAVE_SBRK
  42. if (n == 0)
  43. p = malloc ((size_t) 1);
  44. if (p == 0) {
  45. extern char **environ;
  46. size_t allocated;
  47. if (first_break != NULL)
  48. allocated = (char *) sbrk (0) - first_break;
  49. else
  50. allocated = (char *) sbrk (0) - (char *) &environ;
  51. sd_error("\nCannot allocate %lu bytes after allocating %lu bytes\n",
  52. (unsigned long) n, (unsigned long) allocated);
  53. if (handler)
  54. handler();
  55. else {
  56. sd_error("\n\tMemory exhausted !! Aborting.\n");
  57. abort();
  58. }
  59. }
  60. #endif
  61. return p;
  62. }
  63. //sd_malloc_handler_t
  64. //sd_malloc_set_handler(a_handler)
  65. //sd_malloc_handler_t a_handler;
  66. sd_malloc_handler_t sd_malloc_set_handler(sd_malloc_handler_t a_handler)
  67. {
  68. sd_malloc_handler_t previous = handler;
  69. handler = a_handler;
  70. return previous;
  71. }
  72. /* Allocate N bytes of memory dynamically, with error checking. */
  73. //void *
  74. //sd_malloc (n)
  75. //size_t n;
  76. void *sd_malloc(size_t n)
  77. {
  78. void *p = 0;
  79. p = malloc(n);
  80. if (p == 0)
  81. p = fixup_null_alloc(n);
  82. return p;
  83. }
  84. /* Allocate memory for N elements of S bytes, with error checking. */
  85. //void *
  86. //sd_calloc (n, s)
  87. //size_t n, s;
  88. void *sd_calloc (size_t n, size_t s)
  89. {
  90. void *p = 0;
  91. // 分配n个长度为s的连续空间;
  92. p = calloc (n, s);
  93. if (p == 0)
  94. p = fixup_null_alloc (n * s);
  95. return p;
  96. }
  97. /* Change the size of an allocated block of memory P to N bytes,
  98. with error checking.
  99. If P is NULL, run sd_malloc. */
  100. //void *
  101. //sd_realloc (p, n)
  102. //void *p;
  103. //size_t n;
  104. void *sd_realloc (void *p, size_t n)
  105. {
  106. if (p == 0)
  107. return sd_malloc(n);
  108. p = realloc (p, n);
  109. if (p == 0)
  110. p = fixup_null_alloc (n);
  111. return p;
  112. }
  113. /* Return a newly allocated copy of STRING. */
  114. //char *
  115. //sd_strdup (string)
  116. //const char *string;
  117. TCHAR *sd_strdup(const TCHAR *string)
  118. {
  119. TCHAR *p = (TCHAR *)sd_malloc(_tcslen(string)*sizeof(TCHAR) + 1);
  120. _tcscpy_s(p, _tcslen(string)*sizeof(TCHAR) + 1, string);
  121. return p;
  122. }