/Modules/gcmodule.c

http://unladen-swallow.googlecode.com/ · C · 1486 lines · 935 code · 155 blank · 396 comment · 198 complexity · 09fbe349ede1540e20837273c6736752 MD5 · raw file

  1. /*
  2. Reference Cycle Garbage Collection
  3. ==================================
  4. Neil Schemenauer <nas@arctrix.com>
  5. Based on a post on the python-dev list. Ideas from Guido van Rossum,
  6. Eric Tiedemann, and various others.
  7. http://www.arctrix.com/nas/python/gc/
  8. http://www.python.org/pipermail/python-dev/2000-March/003869.html
  9. http://www.python.org/pipermail/python-dev/2000-March/004010.html
  10. http://www.python.org/pipermail/python-dev/2000-March/004022.html
  11. For a highlevel view of the collection process, read the collect
  12. function.
  13. */
  14. #include "Python.h"
  15. #include "frameobject.h" /* for PyFrame_ClearFreeList */
  16. /* Get an object's GC head */
  17. #define AS_GC(o) ((PyGC_Head *)(o)-1)
  18. /* Get the object given the GC head */
  19. #define FROM_GC(g) ((PyObject *)(((PyGC_Head *)g)+1))
  20. /*** Global GC state ***/
  21. struct gc_generation {
  22. PyGC_Head head;
  23. int threshold; /* collection threshold */
  24. int count; /* count of allocations or collections of younger
  25. generations */
  26. };
  27. #define NUM_GENERATIONS 3
  28. #define GEN_HEAD(n) (&generations[n].head)
  29. /* linked lists of container objects */
  30. static struct gc_generation generations[NUM_GENERATIONS] = {
  31. /* PyGC_Head, threshold, count */
  32. {{{GEN_HEAD(0), GEN_HEAD(0), 0}}, 700, 0},
  33. {{{GEN_HEAD(1), GEN_HEAD(1), 0}}, 10, 0},
  34. {{{GEN_HEAD(2), GEN_HEAD(2), 0}}, 10, 0},
  35. };
  36. PyGC_Head *_PyGC_generation0 = GEN_HEAD(0);
  37. static int enabled = 1; /* automatic collection enabled? */
  38. /* true if we are currently running the collector */
  39. static int collecting = 0;
  40. /* list of uncollectable objects */
  41. static PyObject *garbage = NULL;
  42. /* Python string to use if unhandled exception occurs */
  43. static PyObject *gc_str = NULL;
  44. /* Python string used to look for __del__ attribute. */
  45. static PyObject *delstr = NULL;
  46. /* This is the number of objects who survived the last full collection. It
  47. approximates the number of long lived objects tracked by the GC.
  48. (by "full collection", we mean a collection of the oldest generation).
  49. */
  50. static Py_ssize_t long_lived_total = 0;
  51. /* This is the number of objects who survived all "non-full" collections,
  52. and are awaiting to undergo a full collection for the first time.
  53. */
  54. static Py_ssize_t long_lived_pending = 0;
  55. /*
  56. NOTE: about the counting of long-lived objects.
  57. To limit the cost of garbage collection, there are two strategies;
  58. - make each collection faster, e.g. by scanning fewer objects
  59. - do less collections
  60. This heuristic is about the latter strategy.
  61. In addition to the various configurable thresholds, we only trigger a
  62. full collection if the ratio
  63. long_lived_pending / long_lived_total
  64. is above a given value (hardwired to 25%).
  65. The reason is that, while "non-full" collections (i.e., collections of
  66. the young and middle generations) will always examine roughly the same
  67. number of objects -- determined by the aforementioned thresholds --,
  68. the cost of a full collection is proportional to the total number of
  69. long-lived objects, which is virtually unbounded.
  70. Indeed, it has been remarked that doing a full collection every
  71. <constant number> of object creations entails a dramatic performance
  72. degradation in workloads which consist in creating and storing lots of
  73. long-lived objects (e.g. building a large list of GC-tracked objects would
  74. show quadratic performance, instead of linear as expected: see issue #4074).
  75. Using the above ratio, instead, yields amortized linear performance in
  76. the total number of objects (the effect of which can be summarized
  77. thusly: "each full garbage collection is more and more costly as the
  78. number of objects grows, but we do fewer and fewer of them").
  79. This heuristic was suggested by Martin von Lรถwis on python-dev in
  80. June 2008. His original analysis and proposal can be found at:
  81. http://mail.python.org/pipermail/python-dev/2008-June/080579.html
  82. */
  83. /* set for debugging information */
  84. #define DEBUG_STATS (1<<0) /* print collection statistics */
  85. #define DEBUG_COLLECTABLE (1<<1) /* print collectable objects */
  86. #define DEBUG_UNCOLLECTABLE (1<<2) /* print uncollectable objects */
  87. #define DEBUG_INSTANCES (1<<3) /* print instances */
  88. #define DEBUG_OBJECTS (1<<4) /* print other objects */
  89. #define DEBUG_SAVEALL (1<<5) /* save all garbage in gc.garbage */
  90. #define DEBUG_LEAK DEBUG_COLLECTABLE | \
  91. DEBUG_UNCOLLECTABLE | \
  92. DEBUG_INSTANCES | \
  93. DEBUG_OBJECTS | \
  94. DEBUG_SAVEALL
  95. static int debug;
  96. static PyObject *tmod = NULL;
  97. /*--------------------------------------------------------------------------
  98. gc_refs values.
  99. Between collections, every gc'ed object has one of two gc_refs values:
  100. GC_UNTRACKED
  101. The initial state; objects returned by PyObject_GC_Malloc are in this
  102. state. The object doesn't live in any generation list, and its
  103. tp_traverse slot must not be called.
  104. GC_REACHABLE
  105. The object lives in some generation list, and its tp_traverse is safe to
  106. call. An object transitions to GC_REACHABLE when PyObject_GC_Track
  107. is called.
  108. During a collection, gc_refs can temporarily take on other states:
  109. >= 0
  110. At the start of a collection, update_refs() copies the true refcount
  111. to gc_refs, for each object in the generation being collected.
  112. subtract_refs() then adjusts gc_refs so that it equals the number of
  113. times an object is referenced directly from outside the generation
  114. being collected.
  115. gc_refs remains >= 0 throughout these steps.
  116. GC_TENTATIVELY_UNREACHABLE
  117. move_unreachable() then moves objects not reachable (whether directly or
  118. indirectly) from outside the generation into an "unreachable" set.
  119. Objects that are found to be reachable have gc_refs set to GC_REACHABLE
  120. again. Objects that are found to be unreachable have gc_refs set to
  121. GC_TENTATIVELY_UNREACHABLE. It's "tentatively" because the pass doing
  122. this can't be sure until it ends, and GC_TENTATIVELY_UNREACHABLE may
  123. transition back to GC_REACHABLE.
  124. Only objects with GC_TENTATIVELY_UNREACHABLE still set are candidates
  125. for collection. If it's decided not to collect such an object (e.g.,
  126. it has a __del__ method), its gc_refs is restored to GC_REACHABLE again.
  127. ----------------------------------------------------------------------------
  128. */
  129. #define GC_UNTRACKED _PyGC_REFS_UNTRACKED
  130. #define GC_REACHABLE _PyGC_REFS_REACHABLE
  131. #define GC_TENTATIVELY_UNREACHABLE _PyGC_REFS_TENTATIVELY_UNREACHABLE
  132. #define IS_TRACKED(o) ((AS_GC(o))->gc.gc_refs != GC_UNTRACKED)
  133. #define IS_REACHABLE(o) ((AS_GC(o))->gc.gc_refs == GC_REACHABLE)
  134. #define IS_TENTATIVELY_UNREACHABLE(o) ( \
  135. (AS_GC(o))->gc.gc_refs == GC_TENTATIVELY_UNREACHABLE)
  136. /*** list functions ***/
  137. static void
  138. gc_list_init(PyGC_Head *list)
  139. {
  140. list->gc.gc_prev = list;
  141. list->gc.gc_next = list;
  142. }
  143. static int
  144. gc_list_is_empty(PyGC_Head *list)
  145. {
  146. return (list->gc.gc_next == list);
  147. }
  148. #if 0
  149. /* This became unused after gc_list_move() was introduced. */
  150. /* Append `node` to `list`. */
  151. static void
  152. gc_list_append(PyGC_Head *node, PyGC_Head *list)
  153. {
  154. node->gc.gc_next = list;
  155. node->gc.gc_prev = list->gc.gc_prev;
  156. node->gc.gc_prev->gc.gc_next = node;
  157. list->gc.gc_prev = node;
  158. }
  159. #endif
  160. /* Remove `node` from the gc list it's currently in. */
  161. static void
  162. gc_list_remove(PyGC_Head *node)
  163. {
  164. node->gc.gc_prev->gc.gc_next = node->gc.gc_next;
  165. node->gc.gc_next->gc.gc_prev = node->gc.gc_prev;
  166. node->gc.gc_next = NULL; /* object is not currently tracked */
  167. }
  168. /* Move `node` from the gc list it's currently in (which is not explicitly
  169. * named here) to the end of `list`. This is semantically the same as
  170. * gc_list_remove(node) followed by gc_list_append(node, list).
  171. */
  172. static void
  173. gc_list_move(PyGC_Head *node, PyGC_Head *list)
  174. {
  175. PyGC_Head *new_prev;
  176. PyGC_Head *current_prev = node->gc.gc_prev;
  177. PyGC_Head *current_next = node->gc.gc_next;
  178. /* Unlink from current list. */
  179. current_prev->gc.gc_next = current_next;
  180. current_next->gc.gc_prev = current_prev;
  181. /* Relink at end of new list. */
  182. new_prev = node->gc.gc_prev = list->gc.gc_prev;
  183. new_prev->gc.gc_next = list->gc.gc_prev = node;
  184. node->gc.gc_next = list;
  185. }
  186. /* append list `from` onto list `to`; `from` becomes an empty list */
  187. static void
  188. gc_list_merge(PyGC_Head *from, PyGC_Head *to)
  189. {
  190. PyGC_Head *tail;
  191. assert(from != to);
  192. if (!gc_list_is_empty(from)) {
  193. tail = to->gc.gc_prev;
  194. tail->gc.gc_next = from->gc.gc_next;
  195. tail->gc.gc_next->gc.gc_prev = tail;
  196. to->gc.gc_prev = from->gc.gc_prev;
  197. to->gc.gc_prev->gc.gc_next = to;
  198. }
  199. gc_list_init(from);
  200. }
  201. static Py_ssize_t
  202. gc_list_size(PyGC_Head *list)
  203. {
  204. PyGC_Head *gc;
  205. Py_ssize_t n = 0;
  206. for (gc = list->gc.gc_next; gc != list; gc = gc->gc.gc_next) {
  207. n++;
  208. }
  209. return n;
  210. }
  211. /* Append objects in a GC list to a Python list.
  212. * Return 0 if all OK, < 0 if error (out of memory for list).
  213. */
  214. static int
  215. append_objects(PyObject *py_list, PyGC_Head *gc_list)
  216. {
  217. PyGC_Head *gc;
  218. for (gc = gc_list->gc.gc_next; gc != gc_list; gc = gc->gc.gc_next) {
  219. PyObject *op = FROM_GC(gc);
  220. if (op != py_list) {
  221. if (PyList_Append(py_list, op)) {
  222. return -1; /* exception */
  223. }
  224. }
  225. }
  226. return 0;
  227. }
  228. /*** end of list stuff ***/
  229. /* Set all gc_refs = ob_refcnt. After this, gc_refs is > 0 for all objects
  230. * in containers, and is GC_REACHABLE for all tracked gc objects not in
  231. * containers.
  232. */
  233. static void
  234. update_refs(PyGC_Head *containers)
  235. {
  236. PyGC_Head *gc = containers->gc.gc_next;
  237. for (; gc != containers; gc = gc->gc.gc_next) {
  238. assert(gc->gc.gc_refs == GC_REACHABLE);
  239. gc->gc.gc_refs = Py_REFCNT(FROM_GC(gc));
  240. /* Python's cyclic gc should never see an incoming refcount
  241. * of 0: if something decref'ed to 0, it should have been
  242. * deallocated immediately at that time.
  243. * Possible cause (if the assert triggers): a tp_dealloc
  244. * routine left a gc-aware object tracked during its teardown
  245. * phase, and did something-- or allowed something to happen --
  246. * that called back into Python. gc can trigger then, and may
  247. * see the still-tracked dying object. Before this assert
  248. * was added, such mistakes went on to allow gc to try to
  249. * delete the object again. In a debug build, that caused
  250. * a mysterious segfault, when _Py_ForgetReference tried
  251. * to remove the object from the doubly-linked list of all
  252. * objects a second time. In a release build, an actual
  253. * double deallocation occurred, which leads to corruption
  254. * of the allocator's internal bookkeeping pointers. That's
  255. * so serious that maybe this should be a release-build
  256. * check instead of an assert?
  257. */
  258. assert(gc->gc.gc_refs != 0);
  259. }
  260. }
  261. /* A traversal callback for subtract_refs. */
  262. static int
  263. visit_decref(PyObject *op, void *data)
  264. {
  265. assert(op != NULL);
  266. if (PyObject_IS_GC(op)) {
  267. PyGC_Head *gc = AS_GC(op);
  268. /* We're only interested in gc_refs for objects in the
  269. * generation being collected, which can be recognized
  270. * because only they have positive gc_refs.
  271. */
  272. assert(gc->gc.gc_refs != 0); /* else refcount was too small */
  273. if (gc->gc.gc_refs > 0)
  274. gc->gc.gc_refs--;
  275. }
  276. return 0;
  277. }
  278. /* Subtract internal references from gc_refs. After this, gc_refs is >= 0
  279. * for all objects in containers, and is GC_REACHABLE for all tracked gc
  280. * objects not in containers. The ones with gc_refs > 0 are directly
  281. * reachable from outside containers, and so can't be collected.
  282. */
  283. static void
  284. subtract_refs(PyGC_Head *containers)
  285. {
  286. traverseproc traverse;
  287. PyGC_Head *gc = containers->gc.gc_next;
  288. for (; gc != containers; gc=gc->gc.gc_next) {
  289. traverse = Py_TYPE(FROM_GC(gc))->tp_traverse;
  290. (void) traverse(FROM_GC(gc),
  291. (visitproc)visit_decref,
  292. NULL);
  293. }
  294. }
  295. /* A traversal callback for move_unreachable. */
  296. static int
  297. visit_reachable(PyObject *op, PyGC_Head *reachable)
  298. {
  299. if (PyObject_IS_GC(op)) {
  300. PyGC_Head *gc = AS_GC(op);
  301. const Py_ssize_t gc_refs = gc->gc.gc_refs;
  302. if (gc_refs == 0) {
  303. /* This is in move_unreachable's 'young' list, but
  304. * the traversal hasn't yet gotten to it. All
  305. * we need to do is tell move_unreachable that it's
  306. * reachable.
  307. */
  308. gc->gc.gc_refs = 1;
  309. }
  310. else if (gc_refs == GC_TENTATIVELY_UNREACHABLE) {
  311. /* This had gc_refs = 0 when move_unreachable got
  312. * to it, but turns out it's reachable after all.
  313. * Move it back to move_unreachable's 'young' list,
  314. * and move_unreachable will eventually get to it
  315. * again.
  316. */
  317. gc_list_move(gc, reachable);
  318. gc->gc.gc_refs = 1;
  319. }
  320. /* Else there's nothing to do.
  321. * If gc_refs > 0, it must be in move_unreachable's 'young'
  322. * list, and move_unreachable will eventually get to it.
  323. * If gc_refs == GC_REACHABLE, it's either in some other
  324. * generation so we don't care about it, or move_unreachable
  325. * already dealt with it.
  326. * If gc_refs == GC_UNTRACKED, it must be ignored.
  327. */
  328. else {
  329. assert(gc_refs > 0
  330. || gc_refs == GC_REACHABLE
  331. || gc_refs == GC_UNTRACKED);
  332. }
  333. }
  334. return 0;
  335. }
  336. /* Move the unreachable objects from young to unreachable. After this,
  337. * all objects in young have gc_refs = GC_REACHABLE, and all objects in
  338. * unreachable have gc_refs = GC_TENTATIVELY_UNREACHABLE. All tracked
  339. * gc objects not in young or unreachable still have gc_refs = GC_REACHABLE.
  340. * All objects in young after this are directly or indirectly reachable
  341. * from outside the original young; and all objects in unreachable are
  342. * not.
  343. */
  344. static void
  345. move_unreachable(PyGC_Head *young, PyGC_Head *unreachable)
  346. {
  347. PyGC_Head *gc = young->gc.gc_next;
  348. /* Invariants: all objects "to the left" of us in young have gc_refs
  349. * = GC_REACHABLE, and are indeed reachable (directly or indirectly)
  350. * from outside the young list as it was at entry. All other objects
  351. * from the original young "to the left" of us are in unreachable now,
  352. * and have gc_refs = GC_TENTATIVELY_UNREACHABLE. All objects to the
  353. * left of us in 'young' now have been scanned, and no objects here
  354. * or to the right have been scanned yet.
  355. */
  356. while (gc != young) {
  357. PyGC_Head *next;
  358. if (gc->gc.gc_refs) {
  359. /* gc is definitely reachable from outside the
  360. * original 'young'. Mark it as such, and traverse
  361. * its pointers to find any other objects that may
  362. * be directly reachable from it. Note that the
  363. * call to tp_traverse may append objects to young,
  364. * so we have to wait until it returns to determine
  365. * the next object to visit.
  366. */
  367. PyObject *op = FROM_GC(gc);
  368. traverseproc traverse = Py_TYPE(op)->tp_traverse;
  369. assert(gc->gc.gc_refs > 0);
  370. gc->gc.gc_refs = GC_REACHABLE;
  371. (void) traverse(op,
  372. (visitproc)visit_reachable,
  373. (void *)young);
  374. next = gc->gc.gc_next;
  375. }
  376. else {
  377. /* This *may* be unreachable. To make progress,
  378. * assume it is. gc isn't directly reachable from
  379. * any object we've already traversed, but may be
  380. * reachable from an object we haven't gotten to yet.
  381. * visit_reachable will eventually move gc back into
  382. * young if that's so, and we'll see it again.
  383. */
  384. next = gc->gc.gc_next;
  385. gc_list_move(gc, unreachable);
  386. gc->gc.gc_refs = GC_TENTATIVELY_UNREACHABLE;
  387. }
  388. gc = next;
  389. }
  390. }
  391. /* Return true if object has a finalization method.
  392. * CAUTION: An instance of an old-style class has to be checked for a
  393. *__del__ method, and earlier versions of this used to call PyObject_HasAttr,
  394. * which in turn could call the class's __getattr__ hook (if any). That
  395. * could invoke arbitrary Python code, mutating the object graph in arbitrary
  396. * ways, and that was the source of some excruciatingly subtle bugs.
  397. */
  398. static int
  399. has_finalizer(PyObject *op)
  400. {
  401. if (PyInstance_Check(op)) {
  402. assert(delstr != NULL);
  403. return _PyInstance_Lookup(op, delstr) != NULL;
  404. }
  405. else if (PyType_HasFeature(op->ob_type, Py_TPFLAGS_HEAPTYPE))
  406. return op->ob_type->tp_del != NULL;
  407. else if (PyGen_CheckExact(op))
  408. return PyGen_NeedsFinalizing((PyGenObject *)op);
  409. else
  410. return 0;
  411. }
  412. /* Move the objects in unreachable with __del__ methods into `finalizers`.
  413. * Objects moved into `finalizers` have gc_refs set to GC_REACHABLE; the
  414. * objects remaining in unreachable are left at GC_TENTATIVELY_UNREACHABLE.
  415. */
  416. static void
  417. move_finalizers(PyGC_Head *unreachable, PyGC_Head *finalizers)
  418. {
  419. PyGC_Head *gc;
  420. PyGC_Head *next;
  421. /* March over unreachable. Move objects with finalizers into
  422. * `finalizers`.
  423. */
  424. for (gc = unreachable->gc.gc_next; gc != unreachable; gc = next) {
  425. PyObject *op = FROM_GC(gc);
  426. assert(IS_TENTATIVELY_UNREACHABLE(op));
  427. next = gc->gc.gc_next;
  428. if (has_finalizer(op)) {
  429. gc_list_move(gc, finalizers);
  430. gc->gc.gc_refs = GC_REACHABLE;
  431. }
  432. }
  433. }
  434. /* A traversal callback for move_finalizer_reachable. */
  435. static int
  436. visit_move(PyObject *op, PyGC_Head *tolist)
  437. {
  438. if (PyObject_IS_GC(op)) {
  439. if (IS_TENTATIVELY_UNREACHABLE(op)) {
  440. PyGC_Head *gc = AS_GC(op);
  441. gc_list_move(gc, tolist);
  442. gc->gc.gc_refs = GC_REACHABLE;
  443. }
  444. }
  445. return 0;
  446. }
  447. /* Move objects that are reachable from finalizers, from the unreachable set
  448. * into finalizers set.
  449. */
  450. static void
  451. move_finalizer_reachable(PyGC_Head *finalizers)
  452. {
  453. traverseproc traverse;
  454. PyGC_Head *gc = finalizers->gc.gc_next;
  455. for (; gc != finalizers; gc = gc->gc.gc_next) {
  456. /* Note that the finalizers list may grow during this. */
  457. traverse = Py_TYPE(FROM_GC(gc))->tp_traverse;
  458. (void) traverse(FROM_GC(gc),
  459. (visitproc)visit_move,
  460. (void *)finalizers);
  461. }
  462. }
  463. /* Clear all weakrefs to unreachable objects, and if such a weakref has a
  464. * callback, invoke it if necessary. Note that it's possible for such
  465. * weakrefs to be outside the unreachable set -- indeed, those are precisely
  466. * the weakrefs whose callbacks must be invoked. See gc_weakref.txt for
  467. * overview & some details. Some weakrefs with callbacks may be reclaimed
  468. * directly by this routine; the number reclaimed is the return value. Other
  469. * weakrefs with callbacks may be moved into the `old` generation. Objects
  470. * moved into `old` have gc_refs set to GC_REACHABLE; the objects remaining in
  471. * unreachable are left at GC_TENTATIVELY_UNREACHABLE. When this returns,
  472. * no object in `unreachable` is weakly referenced anymore.
  473. */
  474. static int
  475. handle_weakrefs(PyGC_Head *unreachable, PyGC_Head *old)
  476. {
  477. PyGC_Head *gc;
  478. PyObject *op; /* generally FROM_GC(gc) */
  479. PyWeakReference *wr; /* generally a cast of op */
  480. PyGC_Head wrcb_to_call; /* weakrefs with callbacks to call */
  481. PyGC_Head *next;
  482. int num_freed = 0;
  483. gc_list_init(&wrcb_to_call);
  484. /* Clear all weakrefs to the objects in unreachable. If such a weakref
  485. * also has a callback, move it into `wrcb_to_call` if the callback
  486. * needs to be invoked. Note that we cannot invoke any callbacks until
  487. * all weakrefs to unreachable objects are cleared, lest the callback
  488. * resurrect an unreachable object via a still-active weakref. We
  489. * make another pass over wrcb_to_call, invoking callbacks, after this
  490. * pass completes.
  491. */
  492. for (gc = unreachable->gc.gc_next; gc != unreachable; gc = next) {
  493. PyWeakReference **wrlist;
  494. op = FROM_GC(gc);
  495. assert(IS_TENTATIVELY_UNREACHABLE(op));
  496. next = gc->gc.gc_next;
  497. if (! PyType_SUPPORTS_WEAKREFS(Py_TYPE(op)))
  498. continue;
  499. /* It supports weakrefs. Does it have any? */
  500. wrlist = (PyWeakReference **)
  501. PyObject_GET_WEAKREFS_LISTPTR(op);
  502. /* `op` may have some weakrefs. March over the list, clear
  503. * all the weakrefs, and move the weakrefs with callbacks
  504. * that must be called into wrcb_to_call.
  505. */
  506. for (wr = *wrlist; wr != NULL; wr = *wrlist) {
  507. PyGC_Head *wrasgc; /* AS_GC(wr) */
  508. /* _PyWeakref_ClearRef clears the weakref but leaves
  509. * the callback pointer intact. Obscure: it also
  510. * changes *wrlist.
  511. */
  512. assert(wr->wr_object == op);
  513. _PyWeakref_ClearRef(wr);
  514. assert(wr->wr_object == Py_None);
  515. if (wr->wr_callback == NULL)
  516. continue; /* no callback */
  517. /* Headache time. `op` is going away, and is weakly referenced by
  518. * `wr`, which has a callback. Should the callback be invoked? If wr
  519. * is also trash, no:
  520. *
  521. * 1. There's no need to call it. The object and the weakref are
  522. * both going away, so it's legitimate to pretend the weakref is
  523. * going away first. The user has to ensure a weakref outlives its
  524. * referent if they want a guarantee that the wr callback will get
  525. * invoked.
  526. *
  527. * 2. It may be catastrophic to call it. If the callback is also in
  528. * cyclic trash (CT), then although the CT is unreachable from
  529. * outside the current generation, CT may be reachable from the
  530. * callback. Then the callback could resurrect insane objects.
  531. *
  532. * Since the callback is never needed and may be unsafe in this case,
  533. * wr is simply left in the unreachable set. Note that because we
  534. * already called _PyWeakref_ClearRef(wr), its callback will never
  535. * trigger.
  536. *
  537. * OTOH, if wr isn't part of CT, we should invoke the callback: the
  538. * weakref outlived the trash. Note that since wr isn't CT in this
  539. * case, its callback can't be CT either -- wr acted as an external
  540. * root to this generation, and therefore its callback did too. So
  541. * nothing in CT is reachable from the callback either, so it's hard
  542. * to imagine how calling it later could create a problem for us. wr
  543. * is moved to wrcb_to_call in this case.
  544. */
  545. if (IS_TENTATIVELY_UNREACHABLE(wr))
  546. continue;
  547. assert(IS_REACHABLE(wr));
  548. /* Create a new reference so that wr can't go away
  549. * before we can process it again.
  550. */
  551. Py_INCREF(wr);
  552. /* Move wr to wrcb_to_call, for the next pass. */
  553. wrasgc = AS_GC(wr);
  554. assert(wrasgc != next); /* wrasgc is reachable, but
  555. next isn't, so they can't
  556. be the same */
  557. gc_list_move(wrasgc, &wrcb_to_call);
  558. }
  559. }
  560. /* Invoke the callbacks we decided to honor. It's safe to invoke them
  561. * because they can't reference unreachable objects.
  562. */
  563. while (! gc_list_is_empty(&wrcb_to_call)) {
  564. PyObject *temp;
  565. PyObject *callback;
  566. gc = wrcb_to_call.gc.gc_next;
  567. op = FROM_GC(gc);
  568. assert(IS_REACHABLE(op));
  569. assert(PyWeakref_Check(op));
  570. wr = (PyWeakReference *)op;
  571. callback = wr->wr_callback;
  572. assert(callback != NULL);
  573. /* copy-paste of weakrefobject.c's handle_callback() */
  574. temp = PyObject_CallFunctionObjArgs(callback, wr, NULL);
  575. if (temp == NULL)
  576. PyErr_WriteUnraisable(callback);
  577. else
  578. Py_DECREF(temp);
  579. /* Give up the reference we created in the first pass. When
  580. * op's refcount hits 0 (which it may or may not do right now),
  581. * op's tp_dealloc will decref op->wr_callback too. Note
  582. * that the refcount probably will hit 0 now, and because this
  583. * weakref was reachable to begin with, gc didn't already
  584. * add it to its count of freed objects. Example: a reachable
  585. * weak value dict maps some key to this reachable weakref.
  586. * The callback removes this key->weakref mapping from the
  587. * dict, leaving no other references to the weakref (excepting
  588. * ours).
  589. */
  590. Py_DECREF(op);
  591. if (wrcb_to_call.gc.gc_next == gc) {
  592. /* object is still alive -- move it */
  593. gc_list_move(gc, old);
  594. }
  595. else
  596. ++num_freed;
  597. }
  598. return num_freed;
  599. }
  600. static void
  601. debug_instance(char *msg, PyInstanceObject *inst)
  602. {
  603. char *cname;
  604. /* simple version of instance_repr */
  605. PyObject *classname = inst->in_class->cl_name;
  606. if (classname != NULL && PyString_Check(classname))
  607. cname = PyString_AsString(classname);
  608. else
  609. cname = "?";
  610. PySys_WriteStderr("gc: %.100s <%.100s instance at %p>\n",
  611. msg, cname, inst);
  612. }
  613. static void
  614. debug_cycle(char *msg, PyObject *op)
  615. {
  616. if ((debug & DEBUG_INSTANCES) && PyInstance_Check(op)) {
  617. debug_instance(msg, (PyInstanceObject *)op);
  618. }
  619. else if (debug & DEBUG_OBJECTS) {
  620. PySys_WriteStderr("gc: %.100s <%.100s %p>\n",
  621. msg, Py_TYPE(op)->tp_name, op);
  622. }
  623. }
  624. /* Handle uncollectable garbage (cycles with finalizers, and stuff reachable
  625. * only from such cycles).
  626. * If DEBUG_SAVEALL, all objects in finalizers are appended to the module
  627. * garbage list (a Python list), else only the objects in finalizers with
  628. * __del__ methods are appended to garbage. All objects in finalizers are
  629. * merged into the old list regardless.
  630. * Returns 0 if all OK, <0 on error (out of memory to grow the garbage list).
  631. * The finalizers list is made empty on a successful return.
  632. */
  633. static int
  634. handle_finalizers(PyGC_Head *finalizers, PyGC_Head *old)
  635. {
  636. PyGC_Head *gc = finalizers->gc.gc_next;
  637. if (garbage == NULL) {
  638. garbage = PyList_New(0);
  639. if (garbage == NULL)
  640. Py_FatalError("gc couldn't create gc.garbage list");
  641. }
  642. for (; gc != finalizers; gc = gc->gc.gc_next) {
  643. PyObject *op = FROM_GC(gc);
  644. if ((debug & DEBUG_SAVEALL) || has_finalizer(op)) {
  645. if (PyList_Append(garbage, op) < 0)
  646. return -1;
  647. }
  648. }
  649. gc_list_merge(finalizers, old);
  650. return 0;
  651. }
  652. /* Break reference cycles by clearing the containers involved. This is
  653. * tricky business as the lists can be changing and we don't know which
  654. * objects may be freed. It is possible I screwed something up here.
  655. */
  656. static void
  657. delete_garbage(PyGC_Head *collectable, PyGC_Head *old)
  658. {
  659. inquiry clear;
  660. while (!gc_list_is_empty(collectable)) {
  661. PyGC_Head *gc = collectable->gc.gc_next;
  662. PyObject *op = FROM_GC(gc);
  663. assert(IS_TENTATIVELY_UNREACHABLE(op));
  664. if (debug & DEBUG_SAVEALL) {
  665. PyList_Append(garbage, op);
  666. }
  667. else {
  668. if ((clear = Py_TYPE(op)->tp_clear) != NULL) {
  669. Py_INCREF(op);
  670. clear(op);
  671. Py_DECREF(op);
  672. }
  673. }
  674. if (collectable->gc.gc_next == gc) {
  675. /* object is still alive, move it, it may die later */
  676. gc_list_move(gc, old);
  677. gc->gc.gc_refs = GC_REACHABLE;
  678. }
  679. }
  680. }
  681. /* Clear all free lists
  682. * All free lists are cleared during the collection of the highest generation.
  683. * Allocated items in the free list may keep a pymalloc arena occupied.
  684. * Clearing the free lists may give back memory to the OS earlier.
  685. */
  686. static void
  687. clear_freelists(void)
  688. {
  689. (void)PyMethod_ClearFreeList();
  690. (void)PyFrame_ClearFreeList();
  691. (void)PyCFunction_ClearFreeList();
  692. (void)PyTuple_ClearFreeList();
  693. (void)PyUnicode_ClearFreeList();
  694. (void)PyInt_ClearFreeList();
  695. (void)PyFloat_ClearFreeList();
  696. }
  697. static double
  698. get_time(void)
  699. {
  700. double result = 0;
  701. if (tmod != NULL) {
  702. PyObject *f = PyObject_CallMethod(tmod, "time", NULL);
  703. if (f == NULL) {
  704. PyErr_Clear();
  705. }
  706. else {
  707. if (PyFloat_Check(f))
  708. result = PyFloat_AsDouble(f);
  709. Py_DECREF(f);
  710. }
  711. }
  712. return result;
  713. }
  714. /* This is the main function. Read this to understand how the
  715. * collection process works. */
  716. static Py_ssize_t
  717. collect(int generation)
  718. {
  719. int i;
  720. Py_ssize_t m = 0; /* # objects collected */
  721. Py_ssize_t n = 0; /* # unreachable objects that couldn't be collected */
  722. PyGC_Head *young; /* the generation we are examining */
  723. PyGC_Head *old; /* next older generation */
  724. PyGC_Head unreachable; /* non-problematic unreachable trash */
  725. PyGC_Head finalizers; /* objects with, & reachable from, __del__ */
  726. PyGC_Head *gc;
  727. double t1 = 0.0;
  728. if (delstr == NULL) {
  729. delstr = PyString_InternFromString("__del__");
  730. if (delstr == NULL)
  731. Py_FatalError("gc couldn't allocate \"__del__\"");
  732. }
  733. if (debug & DEBUG_STATS) {
  734. t1 = get_time();
  735. PySys_WriteStderr("gc: collecting generation %d...\n",
  736. generation);
  737. PySys_WriteStderr("gc: objects in each generation:");
  738. for (i = 0; i < NUM_GENERATIONS; i++)
  739. PySys_WriteStderr(" %" PY_FORMAT_SIZE_T "d",
  740. gc_list_size(GEN_HEAD(i)));
  741. PySys_WriteStderr("\n");
  742. }
  743. /* update collection and allocation counters */
  744. if (generation+1 < NUM_GENERATIONS)
  745. generations[generation+1].count += 1;
  746. for (i = 0; i <= generation; i++)
  747. generations[i].count = 0;
  748. /* merge younger generations with one we are currently collecting */
  749. for (i = 0; i < generation; i++) {
  750. gc_list_merge(GEN_HEAD(i), GEN_HEAD(generation));
  751. }
  752. /* handy references */
  753. young = GEN_HEAD(generation);
  754. if (generation < NUM_GENERATIONS-1)
  755. old = GEN_HEAD(generation+1);
  756. else
  757. old = young;
  758. /* Using ob_refcnt and gc_refs, calculate which objects in the
  759. * container set are reachable from outside the set (i.e., have a
  760. * refcount greater than 0 when all the references within the
  761. * set are taken into account).
  762. */
  763. update_refs(young);
  764. subtract_refs(young);
  765. /* Leave everything reachable from outside young in young, and move
  766. * everything else (in young) to unreachable.
  767. * NOTE: This used to move the reachable objects into a reachable
  768. * set instead. But most things usually turn out to be reachable,
  769. * so it's more efficient to move the unreachable things.
  770. */
  771. gc_list_init(&unreachable);
  772. move_unreachable(young, &unreachable);
  773. /* Move reachable objects to next generation. */
  774. if (young != old) {
  775. if (generation == NUM_GENERATIONS - 2) {
  776. long_lived_pending += gc_list_size(young);
  777. }
  778. gc_list_merge(young, old);
  779. }
  780. else {
  781. long_lived_pending = 0;
  782. long_lived_total = gc_list_size(young);
  783. }
  784. /* All objects in unreachable are trash, but objects reachable from
  785. * finalizers can't safely be deleted. Python programmers should take
  786. * care not to create such things. For Python, finalizers means
  787. * instance objects with __del__ methods. Weakrefs with callbacks
  788. * can also call arbitrary Python code but they will be dealt with by
  789. * handle_weakrefs().
  790. */
  791. gc_list_init(&finalizers);
  792. move_finalizers(&unreachable, &finalizers);
  793. /* finalizers contains the unreachable objects with a finalizer;
  794. * unreachable objects reachable *from* those are also uncollectable,
  795. * and we move those into the finalizers list too.
  796. */
  797. move_finalizer_reachable(&finalizers);
  798. /* Collect statistics on collectable objects found and print
  799. * debugging information.
  800. */
  801. for (gc = unreachable.gc.gc_next; gc != &unreachable;
  802. gc = gc->gc.gc_next) {
  803. m++;
  804. if (debug & DEBUG_COLLECTABLE) {
  805. debug_cycle("collectable", FROM_GC(gc));
  806. }
  807. }
  808. /* Clear weakrefs and invoke callbacks as necessary. */
  809. m += handle_weakrefs(&unreachable, old);
  810. /* Call tp_clear on objects in the unreachable set. This will cause
  811. * the reference cycles to be broken. It may also cause some objects
  812. * in finalizers to be freed.
  813. */
  814. delete_garbage(&unreachable, old);
  815. /* Collect statistics on uncollectable objects found and print
  816. * debugging information. */
  817. for (gc = finalizers.gc.gc_next;
  818. gc != &finalizers;
  819. gc = gc->gc.gc_next) {
  820. n++;
  821. if (debug & DEBUG_UNCOLLECTABLE)
  822. debug_cycle("uncollectable", FROM_GC(gc));
  823. }
  824. if (debug & DEBUG_STATS) {
  825. double t2 = get_time();
  826. if (m == 0 && n == 0)
  827. PySys_WriteStderr("gc: done");
  828. else
  829. PySys_WriteStderr(
  830. "gc: done, "
  831. "%" PY_FORMAT_SIZE_T "d unreachable, "
  832. "%" PY_FORMAT_SIZE_T "d uncollectable",
  833. n+m, n);
  834. if (t1 && t2) {
  835. PySys_WriteStderr(", %.4fs elapsed", t2-t1);
  836. }
  837. PySys_WriteStderr(".\n");
  838. }
  839. /* Append instances in the uncollectable set to a Python
  840. * reachable list of garbage. The programmer has to deal with
  841. * this if they insist on creating this type of structure.
  842. */
  843. (void)handle_finalizers(&finalizers, old);
  844. /* Clear free list only during the collection of the higest
  845. * generation */
  846. if (generation == NUM_GENERATIONS-1) {
  847. clear_freelists();
  848. }
  849. if (PyErr_Occurred()) {
  850. if (gc_str == NULL)
  851. gc_str = PyString_FromString("garbage collection");
  852. PyErr_WriteUnraisable(gc_str);
  853. Py_FatalError("unexpected exception during garbage collection");
  854. }
  855. return n+m;
  856. }
  857. static Py_ssize_t
  858. collect_generations(void)
  859. {
  860. int i;
  861. Py_ssize_t n = 0;
  862. /* Find the oldest generation (higest numbered) where the count
  863. * exceeds the threshold. Objects in the that generation and
  864. * generations younger than it will be collected. */
  865. for (i = NUM_GENERATIONS-1; i >= 0; i--) {
  866. if (generations[i].count > generations[i].threshold) {
  867. /* Avoid quadratic performance degradation in number
  868. of tracked objects. See comments at the beginning
  869. of this file, and issue #4074.
  870. */
  871. if (i == NUM_GENERATIONS - 1
  872. && long_lived_pending < long_lived_total / 4)
  873. continue;
  874. n = collect(i);
  875. break;
  876. }
  877. }
  878. return n;
  879. }
  880. PyDoc_STRVAR(gc_enable__doc__,
  881. "enable() -> None\n"
  882. "\n"
  883. "Enable automatic garbage collection.\n");
  884. static PyObject *
  885. gc_enable(PyObject *self, PyObject *noargs)
  886. {
  887. enabled = 1;
  888. Py_INCREF(Py_None);
  889. return Py_None;
  890. }
  891. PyDoc_STRVAR(gc_disable__doc__,
  892. "disable() -> None\n"
  893. "\n"
  894. "Disable automatic garbage collection.\n");
  895. static PyObject *
  896. gc_disable(PyObject *self, PyObject *noargs)
  897. {
  898. enabled = 0;
  899. Py_INCREF(Py_None);
  900. return Py_None;
  901. }
  902. PyDoc_STRVAR(gc_isenabled__doc__,
  903. "isenabled() -> status\n"
  904. "\n"
  905. "Returns true if automatic garbage collection is enabled.\n");
  906. static PyObject *
  907. gc_isenabled(PyObject *self, PyObject *noargs)
  908. {
  909. return PyBool_FromLong((long)enabled);
  910. }
  911. PyDoc_STRVAR(gc_collect__doc__,
  912. "collect([generation]) -> n\n"
  913. "\n"
  914. "With no arguments, run a full collection. The optional argument\n"
  915. "may be an integer specifying which generation to collect. A ValueError\n"
  916. "is raised if the generation number is invalid.\n\n"
  917. "The number of unreachable objects is returned.\n");
  918. static PyObject *
  919. gc_collect(PyObject *self, PyObject *args, PyObject *kws)
  920. {
  921. static char *keywords[] = {"generation", NULL};
  922. int genarg = NUM_GENERATIONS - 1;
  923. Py_ssize_t n;
  924. if (!PyArg_ParseTupleAndKeywords(args, kws, "|i", keywords, &genarg))
  925. return NULL;
  926. else if (genarg < 0 || genarg >= NUM_GENERATIONS) {
  927. PyErr_SetString(PyExc_ValueError, "invalid generation");
  928. return NULL;
  929. }
  930. if (collecting)
  931. n = 0; /* already collecting, don't do anything */
  932. else {
  933. collecting = 1;
  934. n = collect(genarg);
  935. collecting = 0;
  936. }
  937. return PyInt_FromSsize_t(n);
  938. }
  939. PyDoc_STRVAR(gc_set_debug__doc__,
  940. "set_debug(flags) -> None\n"
  941. "\n"
  942. "Set the garbage collection debugging flags. Debugging information is\n"
  943. "written to sys.stderr.\n"
  944. "\n"
  945. "flags is an integer and can have the following bits turned on:\n"
  946. "\n"
  947. " DEBUG_STATS - Print statistics during collection.\n"
  948. " DEBUG_COLLECTABLE - Print collectable objects found.\n"
  949. " DEBUG_UNCOLLECTABLE - Print unreachable but uncollectable objects found.\n"
  950. " DEBUG_INSTANCES - Print instance objects.\n"
  951. " DEBUG_OBJECTS - Print objects other than instances.\n"
  952. " DEBUG_SAVEALL - Save objects to gc.garbage rather than freeing them.\n"
  953. " DEBUG_LEAK - Debug leaking programs (everything but STATS).\n");
  954. static PyObject *
  955. gc_set_debug(PyObject *self, PyObject *args)
  956. {
  957. if (!PyArg_ParseTuple(args, "i:set_debug", &debug))
  958. return NULL;
  959. Py_INCREF(Py_None);
  960. return Py_None;
  961. }
  962. PyDoc_STRVAR(gc_get_debug__doc__,
  963. "get_debug() -> flags\n"
  964. "\n"
  965. "Get the garbage collection debugging flags.\n");
  966. static PyObject *
  967. gc_get_debug(PyObject *self, PyObject *noargs)
  968. {
  969. return Py_BuildValue("i", debug);
  970. }
  971. PyDoc_STRVAR(gc_set_thresh__doc__,
  972. "set_threshold(threshold0, [threshold1, threshold2]) -> None\n"
  973. "\n"
  974. "Sets the collection thresholds. Setting threshold0 to zero disables\n"
  975. "collection.\n");
  976. static PyObject *
  977. gc_set_thresh(PyObject *self, PyObject *args)
  978. {
  979. int i;
  980. if (!PyArg_ParseTuple(args, "i|ii:set_threshold",
  981. &generations[0].threshold,
  982. &generations[1].threshold,
  983. &generations[2].threshold))
  984. return NULL;
  985. for (i = 2; i < NUM_GENERATIONS; i++) {
  986. /* generations higher than 2 get the same threshold */
  987. generations[i].threshold = generations[2].threshold;
  988. }
  989. Py_INCREF(Py_None);
  990. return Py_None;
  991. }
  992. PyDoc_STRVAR(gc_get_thresh__doc__,
  993. "get_threshold() -> (threshold0, threshold1, threshold2)\n"
  994. "\n"
  995. "Return the current collection thresholds\n");
  996. static PyObject *
  997. gc_get_thresh(PyObject *self, PyObject *noargs)
  998. {
  999. return Py_BuildValue("(iii)",
  1000. generations[0].threshold,
  1001. generations[1].threshold,
  1002. generations[2].threshold);
  1003. }
  1004. PyDoc_STRVAR(gc_get_count__doc__,
  1005. "get_count() -> (count0, count1, count2)\n"
  1006. "\n"
  1007. "Return the current collection counts\n");
  1008. static PyObject *
  1009. gc_get_count(PyObject *self, PyObject *noargs)
  1010. {
  1011. return Py_BuildValue("(iii)",
  1012. generations[0].count,
  1013. generations[1].count,
  1014. generations[2].count);
  1015. }
  1016. static int
  1017. referrersvisit(PyObject* obj, PyObject *objs)
  1018. {
  1019. Py_ssize_t i;
  1020. for (i = 0; i < PyTuple_GET_SIZE(objs); i++)
  1021. if (PyTuple_GET_ITEM(objs, i) == obj)
  1022. return 1;
  1023. return 0;
  1024. }
  1025. static int
  1026. gc_referrers_for(PyObject *objs, PyGC_Head *list, PyObject *resultlist)
  1027. {
  1028. PyGC_Head *gc;
  1029. PyObject *obj;
  1030. traverseproc traverse;
  1031. for (gc = list->gc.gc_next; gc != list; gc = gc->gc.gc_next) {
  1032. obj = FROM_GC(gc);
  1033. traverse = Py_TYPE(obj)->tp_traverse;
  1034. if (obj == objs || obj == resultlist)
  1035. continue;
  1036. if (traverse(obj, (visitproc)referrersvisit, objs)) {
  1037. if (PyList_Append(resultlist, obj) < 0)
  1038. return 0; /* error */
  1039. }
  1040. }
  1041. return 1; /* no error */
  1042. }
  1043. PyDoc_STRVAR(gc_get_referrers__doc__,
  1044. "get_referrers(*objs) -> list\n\
  1045. Return the list of objects that directly refer to any of objs.");
  1046. static PyObject *
  1047. gc_get_referrers(PyObject *self, PyObject *args)
  1048. {
  1049. int i;
  1050. PyObject *result = PyList_New(0);
  1051. if (!result) return NULL;
  1052. for (i = 0; i < NUM_GENERATIONS; i++) {
  1053. if (!(gc_referrers_for(args, GEN_HEAD(i), result))) {
  1054. Py_DECREF(result);
  1055. return NULL;
  1056. }
  1057. }
  1058. return result;
  1059. }
  1060. /* Append obj to list; return true if error (out of memory), false if OK. */
  1061. static int
  1062. referentsvisit(PyObject *obj, PyObject *list)
  1063. {
  1064. return PyList_Append(list, obj) < 0;
  1065. }
  1066. PyDoc_STRVAR(gc_get_referents__doc__,
  1067. "get_referents(*objs) -> list\n\
  1068. Return the list of objects that are directly referred to by objs.");
  1069. static PyObject *
  1070. gc_get_referents(PyObject *self, PyObject *args)
  1071. {
  1072. Py_ssize_t i;
  1073. PyObject *result = PyList_New(0);
  1074. if (result == NULL)
  1075. return NULL;
  1076. for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
  1077. traverseproc traverse;
  1078. PyObject *obj = PyTuple_GET_ITEM(args, i);
  1079. if (! PyObject_IS_GC(obj))
  1080. continue;
  1081. traverse = Py_TYPE(obj)->tp_traverse;
  1082. if (! traverse)
  1083. continue;
  1084. if (traverse(obj, (visitproc)referentsvisit, result)) {
  1085. Py_DECREF(result);
  1086. return NULL;
  1087. }
  1088. }
  1089. return result;
  1090. }
  1091. PyDoc_STRVAR(gc_get_objects__doc__,
  1092. "get_objects() -> [...]\n"
  1093. "\n"
  1094. "Return a list of objects tracked by the collector (excluding the list\n"
  1095. "returned).\n");
  1096. static PyObject *
  1097. gc_get_objects(PyObject *self, PyObject *noargs)
  1098. {
  1099. int i;
  1100. PyObject* result;
  1101. result = PyList_New(0);
  1102. if (result == NULL)
  1103. return NULL;
  1104. for (i = 0; i < NUM_GENERATIONS; i++) {
  1105. if (append_objects(result, GEN_HEAD(i))) {
  1106. Py_DECREF(result);
  1107. return NULL;
  1108. }
  1109. }
  1110. return result;
  1111. }
  1112. PyDoc_STRVAR(gc__doc__,
  1113. "This module provides access to the garbage collector for reference cycles.\n"
  1114. "\n"
  1115. "enable() -- Enable automatic garbage collection.\n"
  1116. "disable() -- Disable automatic garbage collection.\n"
  1117. "isenabled() -- Returns true if automatic collection is enabled.\n"
  1118. "collect() -- Do a full collection right now.\n"
  1119. "get_count() -- Return the current collection counts.\n"
  1120. "set_debug() -- Set debugging flags.\n"
  1121. "get_debug() -- Get debugging flags.\n"
  1122. "set_threshold() -- Set the collection thresholds.\n"
  1123. "get_threshold() -- Return the current the collection thresholds.\n"
  1124. "get_objects() -- Return a list of all objects tracked by the collector.\n"
  1125. "get_referrers() -- Return the list of objects that refer to an object.\n"
  1126. "get_referents() -- Return the list of objects that an object refers to.\n");
  1127. static PyMethodDef GcMethods[] = {
  1128. {"enable", gc_enable, METH_NOARGS, gc_enable__doc__},
  1129. {"disable", gc_disable, METH_NOARGS, gc_disable__doc__},
  1130. {"isenabled", gc_isenabled, METH_NOARGS, gc_isenabled__doc__},
  1131. {"set_debug", gc_set_debug, METH_VARARGS, gc_set_debug__doc__},
  1132. {"get_debug", gc_get_debug, METH_NOARGS, gc_get_debug__doc__},
  1133. {"get_count", gc_get_count, METH_NOARGS, gc_get_count__doc__},
  1134. {"set_threshold", gc_set_thresh, METH_VARARGS, gc_set_thresh__doc__},
  1135. {"get_threshold", gc_get_thresh, METH_NOARGS, gc_get_thresh__doc__},
  1136. {"collect", (PyCFunction)gc_collect,
  1137. METH_VARARGS | METH_KEYWORDS, gc_collect__doc__},
  1138. {"get_objects", gc_get_objects,METH_NOARGS, gc_get_objects__doc__},
  1139. {"get_referrers", gc_get_referrers, METH_VARARGS,
  1140. gc_get_referrers__doc__},
  1141. {"get_referents", gc_get_referents, METH_VARARGS,
  1142. gc_get_referents__doc__},
  1143. {NULL, NULL} /* Sentinel */
  1144. };
  1145. PyMODINIT_FUNC
  1146. initgc(void)
  1147. {
  1148. PyObject *m;
  1149. m = Py_InitModule4("gc",
  1150. GcMethods,
  1151. gc__doc__,
  1152. NULL,
  1153. PYTHON_API_VERSION);
  1154. if (m == NULL)
  1155. return;
  1156. if (garbage == NULL) {
  1157. garbage = PyList_New(0);
  1158. if (garbage == NULL)
  1159. return;
  1160. }
  1161. Py_INCREF(garbage);
  1162. if (PyModule_AddObject(m, "garbage", garbage) < 0)
  1163. return;
  1164. /* Importing can't be done in collect() because collect()
  1165. * can be called via PyGC_Collect() in Py_Finalize().
  1166. * This wouldn't be a problem, except that <initialized> is
  1167. * reset to 0 before calling collect which trips up
  1168. * the import and triggers an assertion.
  1169. */
  1170. if (tmod == NULL) {
  1171. tmod = PyImport_ImportModuleNoBlock("time");
  1172. if (tmod == NULL)
  1173. PyErr_Clear();
  1174. }
  1175. #define ADD_INT(NAME) if (PyModule_AddIntConstant(m, #NAME, NAME) < 0) return
  1176. ADD_INT(DEBUG_STATS);
  1177. ADD_INT(DEBUG_COLLECTABLE);
  1178. ADD_INT(DEBUG_UNCOLLECTABLE);
  1179. ADD_INT(DEBUG_INSTANCES);
  1180. ADD_INT(DEBUG_OBJECTS);
  1181. ADD_INT(DEBUG_SAVEALL);
  1182. ADD_INT(DEBUG_LEAK);
  1183. #undef ADD_INT
  1184. }
  1185. /* API to invoke gc.collect() from C */
  1186. Py_ssize_t
  1187. PyGC_Collect(void)
  1188. {
  1189. Py_ssize_t n;
  1190. if (collecting)
  1191. n = 0; /* already collecting, don't do anything */
  1192. else {
  1193. collecting = 1;
  1194. n = collect(NUM_GENERATIONS - 1);
  1195. collecting = 0;
  1196. }
  1197. return n;
  1198. }
  1199. /* for debugging */
  1200. void
  1201. _PyGC_Dump(PyGC_Head *g)
  1202. {
  1203. _PyObject_Dump(FROM_GC(g));
  1204. }
  1205. /* extension modules might be compiled with GC support so these
  1206. functions must always be available */
  1207. #undef PyObject_GC_Track
  1208. #undef PyObject_GC_UnTrack
  1209. #undef PyObject_GC_Del
  1210. #undef _PyObject_GC_Malloc
  1211. void
  1212. PyObject_GC_Track(void *op)
  1213. {
  1214. _PyObject_GC_TRACK(op);
  1215. }
  1216. /* for binary compatibility with 2.2 */
  1217. void
  1218. _PyObject_GC_Track(PyObject *op)
  1219. {
  1220. PyObject_GC_Track(op);
  1221. }
  1222. void
  1223. PyObject_GC_UnTrack(void *op)
  1224. {
  1225. /* Obscure: the Py_TRASHCAN mechanism requires that we be able to
  1226. * call PyObject_GC_UnTrack twice on an object.
  1227. */
  1228. if (IS_TRACKED(op))
  1229. _PyObject_GC_UNTRACK(op);
  1230. }
  1231. /* for binary compatibility with 2.2 */
  1232. void
  1233. _PyObject_GC_UnTrack(PyObject *op)
  1234. {
  1235. PyObject_GC_UnTrack(op);
  1236. }
  1237. PyObject *
  1238. _PyObject_GC_Malloc(size_t basicsize)
  1239. {
  1240. PyObject *op;
  1241. PyGC_Head *g;
  1242. if (basicsize > PY_SSIZE_T_MAX - sizeof(PyGC_Head))
  1243. return PyErr_NoMemory();
  1244. g = (PyGC_Head *)PyObject_MALLOC(
  1245. sizeof(PyGC_Head) + basicsize);
  1246. if (g == NULL)
  1247. return PyErr_NoMemory();
  1248. g->gc.gc_refs = GC_UNTRACKED;
  1249. generations[0].count++; /* number of allocated GC objects */
  1250. if (generations[0].count > generations[0].threshold &&
  1251. enabled &&
  1252. generations[0].threshold &&
  1253. !collecting &&
  1254. !PyErr_Occurred()) {
  1255. collecting = 1;
  1256. collect_generations();
  1257. collecting = 0;
  1258. }
  1259. op = FROM_GC(g);
  1260. return op;
  1261. }
  1262. PyObject *
  1263. _PyObject_GC_New(PyTypeObject *tp)
  1264. {
  1265. PyObject *op = _PyObject_GC_Malloc(_PyObject_SIZE(tp));
  1266. if (op != NULL)
  1267. op = PyObject_INIT(op, tp);
  1268. return op;
  1269. }
  1270. PyVarObject *
  1271. _PyObject_GC_NewVar(PyTypeObject *tp, Py_ssize_t nitems)
  1272. {
  1273. const size_t size = _PyObject_VAR_SIZE(tp, nitems);
  1274. PyVarObject *op = (PyVarObject *) _PyObject_GC_Malloc(size);
  1275. if (op != NULL)
  1276. op = PyObject_INIT_VAR(op, tp, nitems);
  1277. return op;
  1278. }
  1279. PyVarObject *
  1280. _PyObject_GC_Resize(PyVarObject *op, Py_ssize_t nitems)
  1281. {
  1282. const size_t basicsize = _PyObject_VAR_SIZE(Py_TYPE(op), nitems);
  1283. PyGC_Head *g = AS_GC(op);
  1284. if (basicsize > PY_SSIZE_T_MAX - sizeof(PyGC_Head))
  1285. return (PyVarObject *)PyErr_NoMemory();
  1286. g = (PyGC_Head *)PyObject_REALLOC(g, sizeof(PyGC_Head) + basicsize);
  1287. if (g == NULL)
  1288. return (PyVarObject *)PyErr_NoMemory();
  1289. op = (PyVarObject *) FROM_GC(g);
  1290. Py_SIZE(op) = nitems;
  1291. return op;
  1292. }
  1293. void
  1294. PyObject_GC_Del(void *op)
  1295. {
  1296. PyGC_Head *g = AS_GC(op);
  1297. if (IS_TRACKED(op))
  1298. gc_list_remove(g);
  1299. if (generations[0].count > 0) {
  1300. generations[0].count--;
  1301. }
  1302. PyObject_FREE(g);
  1303. }
  1304. /* for binary compatibility with 2.2 */
  1305. #undef _PyObject_GC_Del
  1306. void
  1307. _PyObject_GC_Del(PyObject *op)
  1308. {
  1309. PyObject_GC_Del(op);
  1310. }