gc-malloc.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /* Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
  2. * 2004, 2006, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public License
  6. * as published by the Free Software Foundation; either version 3 of
  7. * the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301 USA
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. # include <config.h>
  21. #endif
  22. #include <stdio.h>
  23. #include <errno.h>
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #ifdef __ia64__
  27. #include <ucontext.h>
  28. extern unsigned long * __libc_ia64_register_backing_store_base;
  29. #endif
  30. #include "libguile/_scm.h"
  31. #include "libguile/eval.h"
  32. #include "libguile/stime.h"
  33. #include "libguile/stackchk.h"
  34. #include "libguile/struct.h"
  35. #include "libguile/smob.h"
  36. #include "libguile/arrays.h"
  37. #include "libguile/async.h"
  38. #include "libguile/ports.h"
  39. #include "libguile/root.h"
  40. #include "libguile/strings.h"
  41. #include "libguile/vectors.h"
  42. #include "libguile/hashtab.h"
  43. #include "libguile/tags.h"
  44. #include "libguile/validate.h"
  45. #include "libguile/deprecation.h"
  46. #include "libguile/gc.h"
  47. #include "libguile/private-gc.h"
  48. #ifdef GUILE_DEBUG_MALLOC
  49. #include "libguile/debug-malloc.h"
  50. #endif
  51. #ifdef HAVE_UNISTD_H
  52. #include <unistd.h>
  53. #endif
  54. /*
  55. INIT_MALLOC_LIMIT is the initial amount of malloc usage which will
  56. trigger a GC.
  57. After startup (at the guile> prompt), we have approximately 100k of
  58. alloced memory, which won't go away on GC. Let's set the init such
  59. that we get a nice yield on the next allocation:
  60. */
  61. #define SCM_DEFAULT_INIT_MALLOC_LIMIT 200*1024
  62. #define SCM_DEFAULT_MALLOC_MINYIELD 40
  63. /* #define DEBUGINFO */
  64. static void*
  65. do_realloc (void *from, size_t new_size)
  66. {
  67. scm_gc_register_allocation (new_size);
  68. return realloc (from, new_size);
  69. }
  70. static void*
  71. do_calloc (size_t n, size_t size)
  72. {
  73. scm_gc_register_allocation (size);
  74. return calloc (n, size);
  75. }
  76. static void*
  77. do_gc_malloc (size_t size, const char *what)
  78. {
  79. /* Ensure nonzero size to be compatible with always-nonzero return of
  80. glibc malloc. */
  81. return GC_MALLOC (size ? size : sizeof (void *));
  82. }
  83. static void*
  84. do_gc_malloc_atomic (size_t size, const char *what)
  85. {
  86. return GC_MALLOC_ATOMIC (size ? size : sizeof (void *));
  87. }
  88. static void*
  89. do_gc_realloc (void *from, size_t size, const char *what)
  90. {
  91. return GC_REALLOC (from, size ? size : sizeof (void *));
  92. }
  93. static void
  94. do_gc_free (void *ptr)
  95. {
  96. GC_FREE (ptr);
  97. }
  98. /* Function for non-cell memory management.
  99. */
  100. void *
  101. scm_realloc (void *mem, size_t size)
  102. {
  103. void *ptr;
  104. ptr = do_realloc (mem, size);
  105. if (ptr || size == 0)
  106. return ptr;
  107. /* Time is hard: trigger a full, ``stop-the-world'' GC, and try again. */
  108. #ifdef HAVE_GC_GCOLLECT_AND_UNMAP
  109. GC_gcollect_and_unmap ();
  110. #else
  111. GC_gcollect ();
  112. #endif
  113. ptr = do_realloc (mem, size);
  114. if (ptr)
  115. return ptr;
  116. scm_memory_error ("realloc");
  117. }
  118. void *
  119. scm_malloc (size_t sz)
  120. {
  121. return scm_realloc (NULL, sz);
  122. }
  123. /*
  124. Hmm. Should we use the C convention for arguments (i.e. N_ELTS,
  125. SIZEOF_ELT)? --hwn
  126. */
  127. void *
  128. scm_calloc (size_t sz)
  129. {
  130. void * ptr;
  131. /*
  132. By default, try to use calloc, as it is likely more efficient than
  133. calling memset by hand.
  134. */
  135. ptr = do_calloc (sz, 1);
  136. if (ptr || sz == 0)
  137. return ptr;
  138. ptr = scm_realloc (NULL, sz);
  139. memset (ptr, 0x0, sz);
  140. return ptr;
  141. }
  142. char *
  143. scm_strndup (const char *str, size_t n)
  144. {
  145. char *dst = scm_malloc (n + 1);
  146. memcpy (dst, str, n);
  147. dst[n] = 0;
  148. return dst;
  149. }
  150. char *
  151. scm_strdup (const char *str)
  152. {
  153. return scm_strndup (str, strlen (str));
  154. }
  155. void
  156. scm_gc_register_collectable_memory (void *mem, size_t size, const char *what)
  157. {
  158. scm_gc_register_allocation (size);
  159. #ifdef GUILE_DEBUG_MALLOC
  160. if (mem)
  161. scm_malloc_register (mem, what);
  162. #endif
  163. }
  164. void
  165. scm_gc_unregister_collectable_memory (void *mem, size_t size, const char *what)
  166. {
  167. /* Nothing to do. */
  168. #ifdef GUILE_DEBUG_MALLOC
  169. if (mem)
  170. scm_malloc_unregister (mem);
  171. #endif
  172. }
  173. /* Allocate SIZE bytes of memory whose contents should not be scanned
  174. for pointers (useful, e.g., for strings). Note though that this
  175. memory is *not* cleared; be sure to initialize it to prevent
  176. information leaks. */
  177. void *
  178. scm_gc_malloc_pointerless (size_t size, const char *what)
  179. {
  180. return do_gc_malloc_atomic (size, what);
  181. }
  182. void *
  183. scm_gc_malloc (size_t size, const char *what)
  184. {
  185. return do_gc_malloc (size, what);
  186. }
  187. void *
  188. scm_gc_calloc (size_t size, const char *what)
  189. {
  190. /* `GC_MALLOC ()' always returns a zeroed buffer. */
  191. return do_gc_malloc (size, what);
  192. }
  193. void *
  194. scm_gc_realloc (void *mem, size_t old_size, size_t new_size, const char *what)
  195. {
  196. return do_gc_realloc (mem, new_size, what);
  197. }
  198. void
  199. scm_gc_free (void *mem, size_t size, const char *what)
  200. {
  201. do_gc_free (mem);
  202. }
  203. char *
  204. scm_gc_strndup (const char *str, size_t n, const char *what)
  205. {
  206. char *dst = do_gc_malloc_atomic (n + 1, what);
  207. memcpy (dst, str, n);
  208. dst[n] = 0;
  209. return dst;
  210. }
  211. char *
  212. scm_gc_strdup (const char *str, const char *what)
  213. {
  214. return scm_gc_strndup (str, strlen (str), what);
  215. }