| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -276,6 +276,8 @@ void _Py_Specialize_BinaryOp(PyObject *lhs, PyObject *rhs, _Py_CODEUNIT *instr, | |||
| 276 | 276 | SpecializedCacheEntry *cache); | |
| 277 | 277 | void _Py_Specialize_CompareOp(PyObject *lhs, PyObject *rhs, _Py_CODEUNIT *instr, SpecializedCacheEntry *cache); | |
| 278 | 278 | ||
| 279 | + /* Deallocator function for static codeobjects used in deepfreeze.py */ | ||
| 280 | + void _PyStaticCode_Dealloc(PyCodeObject *co, _Py_CODEUNIT *firstinstr); | ||
| 279 | 281 | ||
| 280 | 282 | #ifdef Py_STATS | |
| 281 | 283 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -83,6 +83,7 @@ extern void _PyWarnings_Fini(PyInterpreterState *interp); | |||
| 83 | 83 | extern void _PyAST_Fini(PyInterpreterState *interp); | |
| 84 | 84 | extern void _PyAtExit_Fini(PyInterpreterState *interp); | |
| 85 | 85 | extern void _PyThread_FiniType(PyInterpreterState *interp); | |
| 86 | + extern void _Py_Deepfreeze_Fini(void); | ||
| 86 | 87 | ||
| 87 | 88 | extern PyStatus _PyGILState_Init(_PyRuntimeState *runtime); | |
| 88 | 89 | extern PyStatus _PyGILState_SetTstate(PyThreadState *tstate); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1 @@ | |||
| 1 | + Fix memory leak in code objects generated by deepfreeze. Patch by Kumar Aditya. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1906,3 +1906,18 @@ _PyCode_ConstantKey(PyObject *op) | |||
| 1906 | 1906 | } | |
| 1907 | 1907 | return key; | |
| 1908 | 1908 | } | |
| 1909 | + | ||
| 1910 | + void | ||
| 1911 | + _PyStaticCode_Dealloc(PyCodeObject *co, _Py_CODEUNIT *firstinstr) | ||
| 1912 | + { | ||
| 1913 | + PyMem_Free(co->co_quickened); | ||
| 1914 | + co->co_quickened = NULL; | ||
| 1915 | + PyMem_Free(co->co_extra); | ||
| 1916 | + co->co_extra = NULL; | ||
| 1917 | + co->co_firstinstr = firstinstr; | ||
| 1918 | + if (co->co_weakreflist != NULL) { | ||
| 1919 | + PyObject_ClearWeakRefs((PyObject *)co); | ||
| 1920 | + co->co_weakreflist = NULL; | ||
| 1921 | + } | ||
| 1922 | + co->co_warmup = QUICKENING_INITIAL_WARMUP_VALUE; | ||
| 1923 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,6 +14,12 @@ | |||
| 14 | 14 | #include "Python/frozen_modules/importlib._bootstrap_external.h" | |
| 15 | 15 | /* End includes */ | |
| 16 | 16 | ||
| 17 | + /* Empty finalizer for deepfrozen modules*/ | ||
| 18 | + void | ||
| 19 | + _Py_Deepfreeze_Fini(void) | ||
| 20 | + { | ||
| 21 | + } | ||
| 22 | + | ||
| 17 | 23 | /* Note that a negative size indicates a package. */ | |
| 18 | 24 | ||
| 19 | 25 | static const struct _frozen bootstrap_modules[] = { | |
@@ -103,3 +109,4 @@ main(int argc, char **argv) | |||
| 103 | 109 | } | |
| 104 | 110 | Py_ExitStatusException(status); | |
| 105 | 111 | } | |
| 112 | + | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -22,6 +22,12 @@ | |||
| 22 | 22 | #include <unistd.h> | |
| 23 | 23 | #endif | |
| 24 | 24 | ||
| 25 | + /* Empty finalizer for deepfrozen modules */ | ||
| 26 | + void | ||
| 27 | + _Py_Deepfreeze_Fini(void) | ||
| 28 | + { | ||
| 29 | + } | ||
| 30 | + | ||
| 25 | 31 | /* To avoid a circular dependency on frozen.o, we create our own structure | |
| 26 | 32 | of frozen modules instead, left deliberately blank so as to avoid | |
| 27 | 33 | unintentional import of a stale version of _frozen_importlib. */ | |
@@ -235,3 +241,4 @@ main(int argc, char *argv[]) | |||
| 235 | 241 | Py_Finalize(); | |
| 236 | 242 | return 1; | |
| 237 | 243 | } | |
| 244 | + | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1723,6 +1723,7 @@ finalize_interp_clear(PyThreadState *tstate) | |||
| 1723 | 1723 | _Py_HashRandomization_Fini(); | |
| 1724 | 1724 | _PyArg_Fini(); | |
| 1725 | 1725 | _Py_ClearFileSystemEncoding(); | |
| 1726 | + _Py_Deepfreeze_Fini(); | ||
| 1726 | 1727 | } | |
| 1727 | 1728 | ||
| 1728 | 1729 | finalize_interp_types(tstate->interp); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -109,6 +109,7 @@ def __init__(self, file: TextIO) -> None: | |||
| 109 | 109 | self.cache: Dict[tuple[type, object, str], str] = {} | |
| 110 | 110 | self.hits, self.misses = 0, 0 | |
| 111 | 111 | self.patchups: list[str] = [] | |
| 112 | + self.deallocs: list[str] = [] | ||
| 112 | 113 | self.write('#include "Python.h"') | |
| 113 | 114 | self.write('#include "internal/pycore_gc.h"') | |
| 114 | 115 | self.write('#include "internal/pycore_code.h"') | |
@@ -277,6 +278,7 @@ def generate_code(self, name: str, code: types.CodeType) -> str: | |||
| 277 | 278 | self.write(f".co_varnames = {co_varnames},") | |
| 278 | 279 | self.write(f".co_cellvars = {co_cellvars},") | |
| 279 | 280 | self.write(f".co_freevars = {co_freevars},") | |
| 281 | + self.deallocs.append(f"_PyStaticCode_Dealloc(&{name}, (_Py_CODEUNIT *) {removesuffix(co_code, '.ob_base.ob_base')}.ob_sval);") | ||
| 280 | 282 | return f"& {name}.ob_base" | |
| 281 | 283 | ||
| 282 | 284 | def generate_tuple(self, name: str, t: Tuple[object, ...]) -> str: | |
@@ -440,6 +442,9 @@ def generate(args: list[str], output: TextIO) -> None: | |||
| 440 | 442 | else: | |
| 441 | 443 | code = compile(fd.read(), f"<frozen {modname}>", "exec") | |
| 442 | 444 | printer.generate_file(modname, code) | |
| 445 | + with printer.block(f"void\n_Py_Deepfreeze_Fini(void)"): | ||
| 446 | + for p in printer.deallocs: | ||
| 447 | + printer.write(p) | ||
| 443 | 448 | if verbose: | |
| 444 | 449 | print(f"Cache hits: {printer.hits}, misses: {printer.misses}") | |
| 445 | 450 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments