gc-malloc.c 5.4 KB

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