gc-malloc.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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/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. #include <unistd.h>
  51. /*
  52. INIT_MALLOC_LIMIT is the initial amount of malloc usage which will
  53. trigger a GC.
  54. After startup (at the guile> prompt), we have approximately 100k of
  55. alloced memory, which won't go away on GC. Let's set the init such
  56. that we get a nice yield on the next allocation:
  57. */
  58. #define SCM_DEFAULT_INIT_MALLOC_LIMIT 200*1024
  59. #define SCM_DEFAULT_MALLOC_MINYIELD 40
  60. /* #define DEBUGINFO */
  61. static void*
  62. do_realloc (void *from, size_t new_size)
  63. {
  64. scm_gc_register_allocation (new_size);
  65. return realloc (from, new_size);
  66. }
  67. static void*
  68. do_calloc (size_t n, size_t size)
  69. {
  70. scm_gc_register_allocation (size);
  71. return calloc (n, size);
  72. }
  73. static void*
  74. do_gc_malloc (size_t size, const char *what)
  75. {
  76. /* Ensure nonzero size to be compatible with always-nonzero return of
  77. glibc malloc. */
  78. return GC_MALLOC (size ? size : sizeof (void *));
  79. }
  80. static void*
  81. do_gc_malloc_atomic (size_t size, const char *what)
  82. {
  83. return GC_MALLOC_ATOMIC (size ? size : sizeof (void *));
  84. }
  85. static void*
  86. do_gc_realloc (void *from, size_t size, const char *what)
  87. {
  88. return GC_REALLOC (from, size ? size : sizeof (void *));
  89. }
  90. static void
  91. do_gc_free (void *ptr)
  92. {
  93. GC_FREE (ptr);
  94. }
  95. /* Function for non-cell memory management.
  96. */
  97. void *
  98. scm_realloc (void *mem, size_t size)
  99. {
  100. void *ptr;
  101. ptr = do_realloc (mem, size);
  102. if (ptr || size == 0)
  103. return ptr;
  104. /* Time is hard: trigger a full, ``stop-the-world'' GC, and try again. */
  105. GC_gcollect_and_unmap ();
  106. ptr = do_realloc (mem, size);
  107. if (ptr)
  108. return ptr;
  109. scm_report_out_of_memory ();
  110. /* Not reached. */
  111. return NULL;
  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. }