gc-malloc.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /* Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
  2. * 2004, 2006, 2008, 2009, 2010, 2011, 2012, 2013 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. #ifdef GUILE_DEBUG_MALLOC
  48. #include "libguile/debug-malloc.h"
  49. #endif
  50. #ifdef HAVE_UNISTD_H
  51. #include <unistd.h>
  52. #endif
  53. /*
  54. INIT_MALLOC_LIMIT is the initial amount of malloc usage which will
  55. trigger a GC.
  56. After startup (at the guile> prompt), we have approximately 100k of
  57. alloced memory, which won't go away on GC. Let's set the init such
  58. that we get a nice yield on the next allocation:
  59. */
  60. #define SCM_DEFAULT_INIT_MALLOC_LIMIT 200*1024
  61. #define SCM_DEFAULT_MALLOC_MINYIELD 40
  62. /* #define DEBUGINFO */
  63. static void*
  64. do_realloc (void *from, size_t new_size)
  65. {
  66. scm_gc_register_allocation (new_size);
  67. return realloc (from, new_size);
  68. }
  69. static void*
  70. do_calloc (size_t n, size_t size)
  71. {
  72. scm_gc_register_allocation (size);
  73. return calloc (n, size);
  74. }
  75. static void*
  76. do_gc_malloc (size_t size, const char *what)
  77. {
  78. /* Ensure nonzero size to be compatible with always-nonzero return of
  79. glibc malloc. */
  80. return GC_MALLOC (size ? size : sizeof (void *));
  81. }
  82. static void*
  83. do_gc_malloc_atomic (size_t size, const char *what)
  84. {
  85. return GC_MALLOC_ATOMIC (size ? size : sizeof (void *));
  86. }
  87. static void*
  88. do_gc_realloc (void *from, size_t size, const char *what)
  89. {
  90. return GC_REALLOC (from, size ? size : sizeof (void *));
  91. }
  92. static void
  93. do_gc_free (void *ptr)
  94. {
  95. GC_FREE (ptr);
  96. }
  97. /* Function for non-cell memory management.
  98. */
  99. void *
  100. scm_realloc (void *mem, size_t size)
  101. {
  102. void *ptr;
  103. ptr = do_realloc (mem, size);
  104. if (ptr || size == 0)
  105. return ptr;
  106. /* Time is hard: trigger a full, ``stop-the-world'' GC, and try again. */
  107. GC_gcollect_and_unmap ();
  108. ptr = do_realloc (mem, size);
  109. if (ptr)
  110. return ptr;
  111. scm_memory_error ("realloc");
  112. }
  113. void *
  114. scm_malloc (size_t sz)
  115. {
  116. return scm_realloc (NULL, sz);
  117. }
  118. /*
  119. Hmm. Should we use the C convention for arguments (i.e. N_ELTS,
  120. SIZEOF_ELT)? --hwn
  121. */
  122. void *
  123. scm_calloc (size_t sz)
  124. {
  125. void * ptr;
  126. /*
  127. By default, try to use calloc, as it is likely more efficient than
  128. calling memset by hand.
  129. */
  130. ptr = do_calloc (sz, 1);
  131. if (ptr || sz == 0)
  132. return ptr;
  133. ptr = scm_realloc (NULL, sz);
  134. memset (ptr, 0x0, sz);
  135. return ptr;
  136. }
  137. char *
  138. scm_strndup (const char *str, size_t n)
  139. {
  140. char *dst = scm_malloc (n + 1);
  141. memcpy (dst, str, n);
  142. dst[n] = 0;
  143. return dst;
  144. }
  145. char *
  146. scm_strdup (const char *str)
  147. {
  148. return scm_strndup (str, strlen (str));
  149. }
  150. void
  151. scm_gc_register_collectable_memory (void *mem, size_t size, const char *what)
  152. {
  153. scm_gc_register_allocation (size);
  154. #ifdef GUILE_DEBUG_MALLOC
  155. if (mem)
  156. scm_malloc_register (mem, what);
  157. #endif
  158. }
  159. void
  160. scm_gc_unregister_collectable_memory (void *mem, size_t size, const char *what)
  161. {
  162. /* Nothing to do. */
  163. #ifdef GUILE_DEBUG_MALLOC
  164. if (mem)
  165. scm_malloc_unregister (mem);
  166. #endif
  167. }
  168. /* Allocate SIZE bytes of memory whose contents should not be scanned
  169. for pointers (useful, e.g., for strings). Note though that this
  170. memory is *not* cleared; be sure to initialize it to prevent
  171. information leaks. */
  172. void *
  173. scm_gc_malloc_pointerless (size_t size, const char *what)
  174. {
  175. return do_gc_malloc_atomic (size, what);
  176. }
  177. void *
  178. scm_gc_malloc (size_t size, const char *what)
  179. {
  180. return do_gc_malloc (size, what);
  181. }
  182. void *
  183. scm_gc_calloc (size_t size, const char *what)
  184. {
  185. /* `GC_MALLOC ()' always returns a zeroed buffer. */
  186. return do_gc_malloc (size, what);
  187. }
  188. void *
  189. scm_gc_realloc (void *mem, size_t old_size, size_t new_size, const char *what)
  190. {
  191. return do_gc_realloc (mem, new_size, what);
  192. }
  193. void
  194. scm_gc_free (void *mem, size_t size, const char *what)
  195. {
  196. do_gc_free (mem);
  197. }
  198. char *
  199. scm_gc_strndup (const char *str, size_t n, const char *what)
  200. {
  201. char *dst = do_gc_malloc_atomic (n + 1, what);
  202. memcpy (dst, str, n);
  203. dst[n] = 0;
  204. return dst;
  205. }
  206. char *
  207. scm_gc_strdup (const char *str, const char *what)
  208. {
  209. return scm_gc_strndup (str, strlen (str), what);
  210. }