gcmodule.c.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*[clinic input]
  2. preserve
  3. [clinic start generated code]*/
  4. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  5. # include "pycore_gc.h" // PyGC_Head
  6. # include "pycore_runtime.h" // _Py_ID()
  7. #endif
  8. PyDoc_STRVAR(gc_enable__doc__,
  9. "enable($module, /)\n"
  10. "--\n"
  11. "\n"
  12. "Enable automatic garbage collection.");
  13. #define GC_ENABLE_METHODDEF \
  14. {"enable", (PyCFunction)gc_enable, METH_NOARGS, gc_enable__doc__},
  15. static PyObject *
  16. gc_enable_impl(PyObject *module);
  17. static PyObject *
  18. gc_enable(PyObject *module, PyObject *Py_UNUSED(ignored))
  19. {
  20. return gc_enable_impl(module);
  21. }
  22. PyDoc_STRVAR(gc_disable__doc__,
  23. "disable($module, /)\n"
  24. "--\n"
  25. "\n"
  26. "Disable automatic garbage collection.");
  27. #define GC_DISABLE_METHODDEF \
  28. {"disable", (PyCFunction)gc_disable, METH_NOARGS, gc_disable__doc__},
  29. static PyObject *
  30. gc_disable_impl(PyObject *module);
  31. static PyObject *
  32. gc_disable(PyObject *module, PyObject *Py_UNUSED(ignored))
  33. {
  34. return gc_disable_impl(module);
  35. }
  36. PyDoc_STRVAR(gc_isenabled__doc__,
  37. "isenabled($module, /)\n"
  38. "--\n"
  39. "\n"
  40. "Returns true if automatic garbage collection is enabled.");
  41. #define GC_ISENABLED_METHODDEF \
  42. {"isenabled", (PyCFunction)gc_isenabled, METH_NOARGS, gc_isenabled__doc__},
  43. static int
  44. gc_isenabled_impl(PyObject *module);
  45. static PyObject *
  46. gc_isenabled(PyObject *module, PyObject *Py_UNUSED(ignored))
  47. {
  48. PyObject *return_value = NULL;
  49. int _return_value;
  50. _return_value = gc_isenabled_impl(module);
  51. if ((_return_value == -1) && PyErr_Occurred()) {
  52. goto exit;
  53. }
  54. return_value = PyBool_FromLong((long)_return_value);
  55. exit:
  56. return return_value;
  57. }
  58. PyDoc_STRVAR(gc_collect__doc__,
  59. "collect($module, /, generation=2)\n"
  60. "--\n"
  61. "\n"
  62. "Run the garbage collector.\n"
  63. "\n"
  64. "With no arguments, run a full collection. The optional argument\n"
  65. "may be an integer specifying which generation to collect. A ValueError\n"
  66. "is raised if the generation number is invalid.\n"
  67. "\n"
  68. "The number of unreachable objects is returned.");
  69. #define GC_COLLECT_METHODDEF \
  70. {"collect", _PyCFunction_CAST(gc_collect), METH_FASTCALL|METH_KEYWORDS, gc_collect__doc__},
  71. static Py_ssize_t
  72. gc_collect_impl(PyObject *module, int generation);
  73. static PyObject *
  74. gc_collect(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  75. {
  76. PyObject *return_value = NULL;
  77. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  78. #define NUM_KEYWORDS 1
  79. static struct {
  80. PyGC_Head _this_is_not_used;
  81. PyObject_VAR_HEAD
  82. PyObject *ob_item[NUM_KEYWORDS];
  83. } _kwtuple = {
  84. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  85. .ob_item = { &_Py_ID(generation), },
  86. };
  87. #undef NUM_KEYWORDS
  88. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  89. #else // !Py_BUILD_CORE
  90. # define KWTUPLE NULL
  91. #endif // !Py_BUILD_CORE
  92. static const char * const _keywords[] = {"generation", NULL};
  93. static _PyArg_Parser _parser = {
  94. .keywords = _keywords,
  95. .fname = "collect",
  96. .kwtuple = KWTUPLE,
  97. };
  98. #undef KWTUPLE
  99. PyObject *argsbuf[1];
  100. Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
  101. int generation = NUM_GENERATIONS - 1;
  102. Py_ssize_t _return_value;
  103. args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
  104. if (!args) {
  105. goto exit;
  106. }
  107. if (!noptargs) {
  108. goto skip_optional_pos;
  109. }
  110. generation = _PyLong_AsInt(args[0]);
  111. if (generation == -1 && PyErr_Occurred()) {
  112. goto exit;
  113. }
  114. skip_optional_pos:
  115. _return_value = gc_collect_impl(module, generation);
  116. if ((_return_value == -1) && PyErr_Occurred()) {
  117. goto exit;
  118. }
  119. return_value = PyLong_FromSsize_t(_return_value);
  120. exit:
  121. return return_value;
  122. }
  123. PyDoc_STRVAR(gc_set_debug__doc__,
  124. "set_debug($module, flags, /)\n"
  125. "--\n"
  126. "\n"
  127. "Set the garbage collection debugging flags.\n"
  128. "\n"
  129. " flags\n"
  130. " An integer that can have the following bits turned on:\n"
  131. " DEBUG_STATS - Print statistics during collection.\n"
  132. " DEBUG_COLLECTABLE - Print collectable objects found.\n"
  133. " DEBUG_UNCOLLECTABLE - Print unreachable but uncollectable objects\n"
  134. " found.\n"
  135. " DEBUG_SAVEALL - Save objects to gc.garbage rather than freeing them.\n"
  136. " DEBUG_LEAK - Debug leaking programs (everything but STATS).\n"
  137. "\n"
  138. "Debugging information is written to sys.stderr.");
  139. #define GC_SET_DEBUG_METHODDEF \
  140. {"set_debug", (PyCFunction)gc_set_debug, METH_O, gc_set_debug__doc__},
  141. static PyObject *
  142. gc_set_debug_impl(PyObject *module, int flags);
  143. static PyObject *
  144. gc_set_debug(PyObject *module, PyObject *arg)
  145. {
  146. PyObject *return_value = NULL;
  147. int flags;
  148. flags = _PyLong_AsInt(arg);
  149. if (flags == -1 && PyErr_Occurred()) {
  150. goto exit;
  151. }
  152. return_value = gc_set_debug_impl(module, flags);
  153. exit:
  154. return return_value;
  155. }
  156. PyDoc_STRVAR(gc_get_debug__doc__,
  157. "get_debug($module, /)\n"
  158. "--\n"
  159. "\n"
  160. "Get the garbage collection debugging flags.");
  161. #define GC_GET_DEBUG_METHODDEF \
  162. {"get_debug", (PyCFunction)gc_get_debug, METH_NOARGS, gc_get_debug__doc__},
  163. static int
  164. gc_get_debug_impl(PyObject *module);
  165. static PyObject *
  166. gc_get_debug(PyObject *module, PyObject *Py_UNUSED(ignored))
  167. {
  168. PyObject *return_value = NULL;
  169. int _return_value;
  170. _return_value = gc_get_debug_impl(module);
  171. if ((_return_value == -1) && PyErr_Occurred()) {
  172. goto exit;
  173. }
  174. return_value = PyLong_FromLong((long)_return_value);
  175. exit:
  176. return return_value;
  177. }
  178. PyDoc_STRVAR(gc_get_threshold__doc__,
  179. "get_threshold($module, /)\n"
  180. "--\n"
  181. "\n"
  182. "Return the current collection thresholds.");
  183. #define GC_GET_THRESHOLD_METHODDEF \
  184. {"get_threshold", (PyCFunction)gc_get_threshold, METH_NOARGS, gc_get_threshold__doc__},
  185. static PyObject *
  186. gc_get_threshold_impl(PyObject *module);
  187. static PyObject *
  188. gc_get_threshold(PyObject *module, PyObject *Py_UNUSED(ignored))
  189. {
  190. return gc_get_threshold_impl(module);
  191. }
  192. PyDoc_STRVAR(gc_get_count__doc__,
  193. "get_count($module, /)\n"
  194. "--\n"
  195. "\n"
  196. "Return a three-tuple of the current collection counts.");
  197. #define GC_GET_COUNT_METHODDEF \
  198. {"get_count", (PyCFunction)gc_get_count, METH_NOARGS, gc_get_count__doc__},
  199. static PyObject *
  200. gc_get_count_impl(PyObject *module);
  201. static PyObject *
  202. gc_get_count(PyObject *module, PyObject *Py_UNUSED(ignored))
  203. {
  204. return gc_get_count_impl(module);
  205. }
  206. PyDoc_STRVAR(gc_get_objects__doc__,
  207. "get_objects($module, /, generation=None)\n"
  208. "--\n"
  209. "\n"
  210. "Return a list of objects tracked by the collector (excluding the list returned).\n"
  211. "\n"
  212. " generation\n"
  213. " Generation to extract the objects from.\n"
  214. "\n"
  215. "If generation is not None, return only the objects tracked by the collector\n"
  216. "that are in that generation.");
  217. #define GC_GET_OBJECTS_METHODDEF \
  218. {"get_objects", _PyCFunction_CAST(gc_get_objects), METH_FASTCALL|METH_KEYWORDS, gc_get_objects__doc__},
  219. static PyObject *
  220. gc_get_objects_impl(PyObject *module, Py_ssize_t generation);
  221. static PyObject *
  222. gc_get_objects(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  223. {
  224. PyObject *return_value = NULL;
  225. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  226. #define NUM_KEYWORDS 1
  227. static struct {
  228. PyGC_Head _this_is_not_used;
  229. PyObject_VAR_HEAD
  230. PyObject *ob_item[NUM_KEYWORDS];
  231. } _kwtuple = {
  232. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  233. .ob_item = { &_Py_ID(generation), },
  234. };
  235. #undef NUM_KEYWORDS
  236. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  237. #else // !Py_BUILD_CORE
  238. # define KWTUPLE NULL
  239. #endif // !Py_BUILD_CORE
  240. static const char * const _keywords[] = {"generation", NULL};
  241. static _PyArg_Parser _parser = {
  242. .keywords = _keywords,
  243. .fname = "get_objects",
  244. .kwtuple = KWTUPLE,
  245. };
  246. #undef KWTUPLE
  247. PyObject *argsbuf[1];
  248. Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
  249. Py_ssize_t generation = -1;
  250. args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
  251. if (!args) {
  252. goto exit;
  253. }
  254. if (!noptargs) {
  255. goto skip_optional_pos;
  256. }
  257. if (!_Py_convert_optional_to_ssize_t(args[0], &generation)) {
  258. goto exit;
  259. }
  260. skip_optional_pos:
  261. return_value = gc_get_objects_impl(module, generation);
  262. exit:
  263. return return_value;
  264. }
  265. PyDoc_STRVAR(gc_get_stats__doc__,
  266. "get_stats($module, /)\n"
  267. "--\n"
  268. "\n"
  269. "Return a list of dictionaries containing per-generation statistics.");
  270. #define GC_GET_STATS_METHODDEF \
  271. {"get_stats", (PyCFunction)gc_get_stats, METH_NOARGS, gc_get_stats__doc__},
  272. static PyObject *
  273. gc_get_stats_impl(PyObject *module);
  274. static PyObject *
  275. gc_get_stats(PyObject *module, PyObject *Py_UNUSED(ignored))
  276. {
  277. return gc_get_stats_impl(module);
  278. }
  279. PyDoc_STRVAR(gc_is_tracked__doc__,
  280. "is_tracked($module, obj, /)\n"
  281. "--\n"
  282. "\n"
  283. "Returns true if the object is tracked by the garbage collector.\n"
  284. "\n"
  285. "Simple atomic objects will return false.");
  286. #define GC_IS_TRACKED_METHODDEF \
  287. {"is_tracked", (PyCFunction)gc_is_tracked, METH_O, gc_is_tracked__doc__},
  288. PyDoc_STRVAR(gc_is_finalized__doc__,
  289. "is_finalized($module, obj, /)\n"
  290. "--\n"
  291. "\n"
  292. "Returns true if the object has been already finalized by the GC.");
  293. #define GC_IS_FINALIZED_METHODDEF \
  294. {"is_finalized", (PyCFunction)gc_is_finalized, METH_O, gc_is_finalized__doc__},
  295. PyDoc_STRVAR(gc_freeze__doc__,
  296. "freeze($module, /)\n"
  297. "--\n"
  298. "\n"
  299. "Freeze all current tracked objects and ignore them for future collections.\n"
  300. "\n"
  301. "This can be used before a POSIX fork() call to make the gc copy-on-write friendly.\n"
  302. "Note: collection before a POSIX fork() call may free pages for future allocation\n"
  303. "which can cause copy-on-write.");
  304. #define GC_FREEZE_METHODDEF \
  305. {"freeze", (PyCFunction)gc_freeze, METH_NOARGS, gc_freeze__doc__},
  306. static PyObject *
  307. gc_freeze_impl(PyObject *module);
  308. static PyObject *
  309. gc_freeze(PyObject *module, PyObject *Py_UNUSED(ignored))
  310. {
  311. return gc_freeze_impl(module);
  312. }
  313. PyDoc_STRVAR(gc_unfreeze__doc__,
  314. "unfreeze($module, /)\n"
  315. "--\n"
  316. "\n"
  317. "Unfreeze all objects in the permanent generation.\n"
  318. "\n"
  319. "Put all objects in the permanent generation back into oldest generation.");
  320. #define GC_UNFREEZE_METHODDEF \
  321. {"unfreeze", (PyCFunction)gc_unfreeze, METH_NOARGS, gc_unfreeze__doc__},
  322. static PyObject *
  323. gc_unfreeze_impl(PyObject *module);
  324. static PyObject *
  325. gc_unfreeze(PyObject *module, PyObject *Py_UNUSED(ignored))
  326. {
  327. return gc_unfreeze_impl(module);
  328. }
  329. PyDoc_STRVAR(gc_get_freeze_count__doc__,
  330. "get_freeze_count($module, /)\n"
  331. "--\n"
  332. "\n"
  333. "Return the number of objects in the permanent generation.");
  334. #define GC_GET_FREEZE_COUNT_METHODDEF \
  335. {"get_freeze_count", (PyCFunction)gc_get_freeze_count, METH_NOARGS, gc_get_freeze_count__doc__},
  336. static Py_ssize_t
  337. gc_get_freeze_count_impl(PyObject *module);
  338. static PyObject *
  339. gc_get_freeze_count(PyObject *module, PyObject *Py_UNUSED(ignored))
  340. {
  341. PyObject *return_value = NULL;
  342. Py_ssize_t _return_value;
  343. _return_value = gc_get_freeze_count_impl(module);
  344. if ((_return_value == -1) && PyErr_Occurred()) {
  345. goto exit;
  346. }
  347. return_value = PyLong_FromSsize_t(_return_value);
  348. exit:
  349. return return_value;
  350. }
  351. /*[clinic end generated code: output=66432ac0e17fd04f input=a9049054013a1b77]*/