gc-malloc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002, 2003, 2004, 2006, 2008 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/unif.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. static int scm_i_minyield_malloc;
  67. void
  68. scm_gc_init_malloc (void)
  69. {
  70. scm_mtrigger = scm_getenv_int ("GUILE_INIT_MALLOC_LIMIT",
  71. SCM_DEFAULT_INIT_MALLOC_LIMIT);
  72. scm_i_minyield_malloc = scm_getenv_int ("GUILE_MIN_YIELD_MALLOC",
  73. SCM_DEFAULT_MALLOC_MINYIELD);
  74. if (scm_i_minyield_malloc >= 100)
  75. scm_i_minyield_malloc = 99;
  76. if (scm_i_minyield_malloc < 1)
  77. scm_i_minyield_malloc = 1;
  78. if (scm_mtrigger < 0)
  79. scm_mtrigger = SCM_DEFAULT_INIT_MALLOC_LIMIT;
  80. }
  81. /* Function for non-cell memory management.
  82. */
  83. void *
  84. scm_realloc (void *mem, size_t size)
  85. {
  86. void *ptr;
  87. SCM_SYSCALL (ptr = realloc (mem, size));
  88. if (ptr)
  89. return ptr;
  90. scm_i_scm_pthread_mutex_lock (&scm_i_sweep_mutex);
  91. scm_gc_running_p = 1;
  92. scm_i_gc ("realloc");
  93. /*
  94. We don't want these sweep statistics to influence results for
  95. cell GC, so we don't collect statistics.
  96. realloc () failed, so we're really desparate to free memory. Run a
  97. full sweep.
  98. */
  99. scm_i_sweep_all_segments ("realloc", NULL);
  100. scm_gc_running_p = 0;
  101. scm_i_pthread_mutex_unlock (&scm_i_sweep_mutex);
  102. SCM_SYSCALL (ptr = realloc (mem, size));
  103. if (ptr)
  104. return ptr;
  105. scm_memory_error ("realloc");
  106. }
  107. void *
  108. scm_malloc (size_t sz)
  109. {
  110. return scm_realloc (NULL, sz);
  111. }
  112. /*
  113. Hmm. Should we use the C convention for arguments (i.e. N_ELTS,
  114. SIZEOF_ELT)? --hwn
  115. */
  116. void *
  117. scm_calloc (size_t sz)
  118. {
  119. void * ptr;
  120. /*
  121. By default, try to use calloc, as it is likely more efficient than
  122. calling memset by hand.
  123. */
  124. SCM_SYSCALL (ptr = calloc (sz, 1));
  125. if (ptr)
  126. return ptr;
  127. ptr = scm_realloc (NULL, sz);
  128. memset (ptr, 0x0, sz);
  129. return ptr;
  130. }
  131. char *
  132. scm_strndup (const char *str, size_t n)
  133. {
  134. char *dst = scm_malloc (n + 1);
  135. memcpy (dst, str, n);
  136. dst[n] = 0;
  137. return dst;
  138. }
  139. char *
  140. scm_strdup (const char *str)
  141. {
  142. return scm_strndup (str, strlen (str));
  143. }
  144. static void
  145. decrease_mtrigger (size_t size, const char * what)
  146. {
  147. scm_i_pthread_mutex_lock (&scm_i_gc_admin_mutex);
  148. if (size > scm_mallocated)
  149. {
  150. fprintf (stderr, "`scm_mallocated' underflow. This means that more "
  151. "memory was unregistered\n"
  152. "via `scm_gc_unregister_collectable_memory ()' than "
  153. "registered.\n");
  154. abort ();
  155. }
  156. scm_mallocated -= size;
  157. scm_gc_malloc_collected += size;
  158. scm_i_pthread_mutex_unlock (&scm_i_gc_admin_mutex);
  159. }
  160. static void
  161. increase_mtrigger (size_t size, const char *what)
  162. {
  163. size_t mallocated = 0;
  164. int overflow = 0, triggered = 0;
  165. scm_i_pthread_mutex_lock (&scm_i_gc_admin_mutex);
  166. if (ULONG_MAX - size < scm_mallocated)
  167. overflow = 1;
  168. else
  169. {
  170. scm_mallocated += size;
  171. mallocated = scm_mallocated;
  172. if (scm_mallocated > scm_mtrigger)
  173. triggered = 1;
  174. }
  175. scm_i_pthread_mutex_unlock (&scm_i_gc_admin_mutex);
  176. if (overflow)
  177. scm_memory_error ("Overflow of scm_mallocated: too much memory in use.");
  178. /*
  179. A program that uses a lot of malloced collectable memory (vectors,
  180. strings), will use a lot of memory off the cell-heap; it needs to
  181. do GC more often (before cells are exhausted), otherwise swapping
  182. and malloc management will tie it down.
  183. */
  184. if (triggered)
  185. {
  186. unsigned long prev_alloced;
  187. float yield;
  188. scm_i_scm_pthread_mutex_lock (&scm_i_sweep_mutex);
  189. scm_gc_running_p = 1;
  190. prev_alloced = mallocated;
  191. /* The GC will finish the pending sweep. For that reason, we
  192. don't execute a complete sweep after GC, although that might
  193. free some more memory.
  194. */
  195. scm_i_gc (what);
  196. yield = (((float) prev_alloced - (float) scm_mallocated)
  197. / (float) prev_alloced);
  198. scm_gc_malloc_yield_percentage = (int) (100 * yield);
  199. #ifdef DEBUGINFO
  200. fprintf (stderr, "prev %lud , now %lud, yield %4.2lf, want %d",
  201. prev_alloced,
  202. scm_mallocated,
  203. 100.0 * yield,
  204. scm_i_minyield_malloc);
  205. #endif
  206. if (yield < scm_i_minyield_malloc / 100.0)
  207. {
  208. /*
  209. We make the trigger a little larger, even; If you have a
  210. program that builds up a lot of data in strings, then the
  211. desired yield will never be satisfied.
  212. Instead of getting bogged down, we let the mtrigger grow
  213. strongly with it.
  214. */
  215. float no_overflow_trigger = scm_mallocated * 110.0;
  216. no_overflow_trigger /= (float) (100.0 - scm_i_minyield_malloc);
  217. if (no_overflow_trigger >= (float) ULONG_MAX)
  218. scm_mtrigger = ULONG_MAX;
  219. else
  220. scm_mtrigger = (unsigned long) no_overflow_trigger;
  221. #ifdef DEBUGINFO
  222. fprintf (stderr, "Mtrigger sweep: ineffective. New trigger %d\n",
  223. scm_mtrigger);
  224. #endif
  225. }
  226. scm_gc_running_p = 0;
  227. scm_i_pthread_mutex_unlock (&scm_i_sweep_mutex);
  228. }
  229. }
  230. void
  231. scm_gc_register_collectable_memory (void *mem, size_t size, const char *what)
  232. {
  233. increase_mtrigger (size, what);
  234. #ifdef GUILE_DEBUG_MALLOC
  235. if (mem)
  236. scm_malloc_register (mem, what);
  237. #endif
  238. }
  239. void
  240. scm_gc_unregister_collectable_memory (void *mem, size_t size, const char *what)
  241. {
  242. decrease_mtrigger (size, what);
  243. #ifdef GUILE_DEBUG_MALLOC
  244. if (mem)
  245. scm_malloc_unregister (mem);
  246. #endif
  247. }
  248. void *
  249. scm_gc_malloc (size_t size, const char *what)
  250. {
  251. /*
  252. The straightforward implementation below has the problem
  253. that it might call the GC twice, once in scm_malloc and then
  254. again in scm_gc_register_collectable_memory. We don't really
  255. want the second GC since it will not find new garbage.
  256. Note: this is a theoretical peeve. In reality, malloc () never
  257. returns NULL. Usually, memory is overcommitted, and when you try
  258. to write it the program is killed with signal 11. --hwn
  259. */
  260. void *ptr = size ? scm_malloc (size) : NULL;
  261. scm_gc_register_collectable_memory (ptr, size, what);
  262. return ptr;
  263. }
  264. void *
  265. scm_gc_calloc (size_t size, const char *what)
  266. {
  267. void *ptr = scm_gc_malloc (size, what);
  268. memset (ptr, 0x0, size);
  269. return ptr;
  270. }
  271. void *
  272. scm_gc_realloc (void *mem, size_t old_size, size_t new_size, const char *what)
  273. {
  274. void *ptr;
  275. /* XXX - see scm_gc_malloc. */
  276. /*
  277. scm_realloc () may invalidate the block pointed to by WHERE, eg. by
  278. unmapping it from memory or altering the contents. Since
  279. increase_mtrigger () might trigger a GC that would scan
  280. MEM, it is crucial that this call precedes realloc ().
  281. */
  282. decrease_mtrigger (old_size, what);
  283. increase_mtrigger (new_size, what);
  284. ptr = scm_realloc (mem, new_size);
  285. #ifdef GUILE_DEBUG_MALLOC
  286. if (mem)
  287. scm_malloc_reregister (mem, ptr, what);
  288. #endif
  289. return ptr;
  290. }
  291. void
  292. scm_gc_free (void *mem, size_t size, const char *what)
  293. {
  294. scm_gc_unregister_collectable_memory (mem, size, what);
  295. if (mem)
  296. free (mem);
  297. }
  298. char *
  299. scm_gc_strndup (const char *str, size_t n, const char *what)
  300. {
  301. char *dst = scm_gc_malloc (n+1, what);
  302. memcpy (dst, str, n);
  303. dst[n] = 0;
  304. return dst;
  305. }
  306. char *
  307. scm_gc_strdup (const char *str, const char *what)
  308. {
  309. return scm_gc_strndup (str, strlen (str), what);
  310. }
  311. #if SCM_ENABLE_DEPRECATED == 1
  312. /* {Deprecated front end to malloc}
  313. *
  314. * scm_must_malloc, scm_must_realloc, scm_must_free, scm_done_malloc,
  315. * scm_done_free
  316. *
  317. * These functions provide services comparable to malloc, realloc, and
  318. * free. They should be used when allocating memory that will be under
  319. * control of the garbage collector, i.e., if the memory may be freed
  320. * during garbage collection.
  321. *
  322. * They are deprecated because they weren't really used the way
  323. * outlined above, and making sure to return the right amount from
  324. * smob free routines was sometimes difficult when dealing with nested
  325. * data structures. We basically want everybody to review their code
  326. * and use the more symmetrical scm_gc_malloc/scm_gc_free functions
  327. * instead. In some cases, where scm_must_malloc has been used
  328. * incorrectly (i.e. for non-GC-able memory), use scm_malloc/free.
  329. */
  330. void *
  331. scm_must_malloc (size_t size, const char *what)
  332. {
  333. scm_c_issue_deprecation_warning
  334. ("scm_must_malloc is deprecated. "
  335. "Use scm_gc_malloc and scm_gc_free instead.");
  336. return scm_gc_malloc (size, what);
  337. }
  338. void *
  339. scm_must_realloc (void *where,
  340. size_t old_size,
  341. size_t size,
  342. const char *what)
  343. {
  344. scm_c_issue_deprecation_warning
  345. ("scm_must_realloc is deprecated. "
  346. "Use scm_gc_realloc and scm_gc_free instead.");
  347. return scm_gc_realloc (where, old_size, size, what);
  348. }
  349. char *
  350. scm_must_strndup (const char *str, size_t length)
  351. {
  352. scm_c_issue_deprecation_warning
  353. ("scm_must_strndup is deprecated. "
  354. "Use scm_gc_strndup and scm_gc_free instead.");
  355. return scm_gc_strndup (str, length, "string");
  356. }
  357. char *
  358. scm_must_strdup (const char *str)
  359. {
  360. scm_c_issue_deprecation_warning
  361. ("scm_must_strdup is deprecated. "
  362. "Use scm_gc_strdup and scm_gc_free instead.");
  363. return scm_gc_strdup (str, "string");
  364. }
  365. void
  366. scm_must_free (void *obj)
  367. #define FUNC_NAME "scm_must_free"
  368. {
  369. scm_c_issue_deprecation_warning
  370. ("scm_must_free is deprecated. "
  371. "Use scm_gc_malloc and scm_gc_free instead.");
  372. #ifdef GUILE_DEBUG_MALLOC
  373. scm_malloc_unregister (obj);
  374. #endif
  375. if (obj)
  376. free (obj);
  377. else
  378. {
  379. fprintf (stderr,"freeing NULL pointer");
  380. abort ();
  381. }
  382. }
  383. #undef FUNC_NAME
  384. void
  385. scm_done_malloc (long size)
  386. {
  387. scm_c_issue_deprecation_warning
  388. ("scm_done_malloc is deprecated. "
  389. "Use scm_gc_register_collectable_memory instead.");
  390. if (size >= 0)
  391. scm_gc_register_collectable_memory (NULL, size, "foreign mallocs");
  392. else
  393. scm_gc_unregister_collectable_memory (NULL, -size, "foreign mallocs");
  394. }
  395. void
  396. scm_done_free (long size)
  397. {
  398. scm_c_issue_deprecation_warning
  399. ("scm_done_free is deprecated. "
  400. "Use scm_gc_unregister_collectable_memory instead.");
  401. if (size >= 0)
  402. scm_gc_unregister_collectable_memory (NULL, size, "foreign mallocs");
  403. else
  404. scm_gc_register_collectable_memory (NULL, -size, "foreign mallocs");
  405. }
  406. #endif /* SCM_ENABLE_DEPRECATED == 1 */