gc-malloc.c 4.9 KB

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