gc-malloc.c 4.9 KB

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