Add _Py_Deepfreeze_Fini() and _PyStaticCode_Dealloc() functions.
SpecializedCacheEntry *cache);
void _Py_Specialize_CompareOp(PyObject *lhs, PyObject *rhs, _Py_CODEUNIT *instr, SpecializedCacheEntry *cache);
+/* Deallocator function for static codeobjects used in deepfreeze.py */
+void _PyStaticCode_Dealloc(PyCodeObject *co, _Py_CODEUNIT *firstinstr);
#ifdef Py_STATS
extern void _PyAST_Fini(PyInterpreterState *interp);
extern void _PyAtExit_Fini(PyInterpreterState *interp);
extern void _PyThread_FiniType(PyInterpreterState *interp);
+extern void _Py_Deepfreeze_Fini(void);
extern PyStatus _PyGILState_Init(_PyRuntimeState *runtime);
extern PyStatus _PyGILState_SetTstate(PyThreadState *tstate);
--- /dev/null
+Fix memory leak in code objects generated by deepfreeze. Patch by Kumar Aditya.
\ No newline at end of file
}
return key;
}
+
+void
+_PyStaticCode_Dealloc(PyCodeObject *co, _Py_CODEUNIT *firstinstr)
+{
+ PyMem_Free(co->co_quickened);
+ co->co_quickened = NULL;
+ PyMem_Free(co->co_extra);
+ co->co_extra = NULL;
+ co->co_firstinstr = firstinstr;
+ if (co->co_weakreflist != NULL) {
+ PyObject_ClearWeakRefs((PyObject *)co);
+ co->co_weakreflist = NULL;
+ }
+ co->co_warmup = QUICKENING_INITIAL_WARMUP_VALUE;
+}
#include "Python/frozen_modules/importlib._bootstrap_external.h"
/* End includes */
+/* Empty finalizer for deepfrozen modules*/
+void
+_Py_Deepfreeze_Fini(void)
+{
+}
+
/* Note that a negative size indicates a package. */
static const struct _frozen bootstrap_modules[] = {
}
Py_ExitStatusException(status);
}
+
#include <unistd.h>
#endif
+/* Empty finalizer for deepfrozen modules */
+void
+_Py_Deepfreeze_Fini(void)
+{
+}
+
/* To avoid a circular dependency on frozen.o, we create our own structure
of frozen modules instead, left deliberately blank so as to avoid
unintentional import of a stale version of _frozen_importlib. */
Py_Finalize();
return 1;
}
+
_Py_HashRandomization_Fini();
_PyArg_Fini();
_Py_ClearFileSystemEncoding();
+ _Py_Deepfreeze_Fini();
}
finalize_interp_types(tstate->interp);
self.cache: Dict[tuple[type, object, str], str] = {}
self.hits, self.misses = 0, 0
self.patchups: list[str] = []
+ self.deallocs: list[str] = []
self.write('#include "Python.h"')
self.write('#include "internal/pycore_gc.h"')
self.write('#include "internal/pycore_code.h"')
self.write(f".co_varnames = {co_varnames},")
self.write(f".co_cellvars = {co_cellvars},")
self.write(f".co_freevars = {co_freevars},")
+ self.deallocs.append(f"_PyStaticCode_Dealloc(&{name}, (_Py_CODEUNIT *) {removesuffix(co_code, '.ob_base.ob_base')}.ob_sval);")
return f"& {name}.ob_base"
def generate_tuple(self, name: str, t: Tuple[object, ...]) -> str:
else:
code = compile(fd.read(), f"<frozen {modname}>", "exec")
printer.generate_file(modname, code)
+ with printer.block(f"void\n_Py_Deepfreeze_Fini(void)"):
+ for p in printer.deallocs:
+ printer.write(p)
if verbose:
print(f"Cache hits: {printer.hits}, misses: {printer.misses}")