From 38a8916134adaa0222c0e8d904c2b2b840cfb79b Mon Sep 17 00:00:00 2001 From: Neil Schemenauer Date: Wed, 27 Mar 2002 15:18:21 +0000 Subject: [PATCH] Remove weakref free list. This has the side effect of fixing a memory management bug. Also, move some duplicated code into the new_weakref fucntion. --- Objects/weakrefobject.c | 37 +++++++++++-------------------------- 1 file changed, 11 insertions(+), 26 deletions(-) diff --git a/Objects/weakrefobject.c b/Objects/weakrefobject.c index 2e02cf27cab..021670a9936 100644 --- a/Objects/weakrefobject.c +++ b/Objects/weakrefobject.c @@ -5,9 +5,6 @@ #define GET_WEAKREFS_LISTPTR(o) \ ((PyWeakReference **) PyObject_GET_WEAKREFS_LISTPTR(o)) -static PyWeakReference * -free_list = NULL; - long _PyWeakref_GetWeakrefCount(PyWeakReference *head) @@ -23,21 +20,18 @@ _PyWeakref_GetWeakrefCount(PyWeakReference *head) static PyWeakReference * -new_weakref(void) +new_weakref(PyObject *ob, PyObject *callback) { PyWeakReference *result; - if (free_list != NULL) { - result = free_list; - free_list = result->wr_next; - result->ob_type = &_PyWeakref_RefType; - _Py_NewReference((PyObject *)result); - } - else { - result = PyObject_GC_New(PyWeakReference, &_PyWeakref_RefType); - } - if (result) + result = PyObject_GC_New(PyWeakReference, &_PyWeakref_RefType); + if (result) { result->hash = -1; + result->wr_object = ob; + Py_XINCREF(callback); + result->wr_callback = callback; + PyObject_GC_Track(result); + } return result; } @@ -76,8 +70,7 @@ weakref_dealloc(PyWeakReference *self) { PyObject_GC_UnTrack((PyObject *)self); clear_weakref(self); - self->wr_next = free_list; - free_list = self; + PyObject_GC_Del(self); } @@ -580,11 +573,8 @@ PyWeakref_NewRef(PyObject *ob, PyObject *callback) if (result != NULL) Py_XINCREF(result); else { - result = new_weakref(); + result = new_weakref(ob, callback); if (result != NULL) { - Py_XINCREF(callback); - result->wr_callback = callback; - result->wr_object = ob; if (callback == NULL) { insert_head(result, list); } @@ -596,7 +586,6 @@ PyWeakref_NewRef(PyObject *ob, PyObject *callback) else insert_after(result, prev); } - PyObject_GC_Track(result); } } return (PyObject *) result; @@ -624,7 +613,7 @@ PyWeakref_NewProxy(PyObject *ob, PyObject *callback) if (result != NULL) Py_XINCREF(result); else { - result = new_weakref(); + result = new_weakref(ob, callback); if (result != NULL) { PyWeakReference *prev; @@ -632,9 +621,6 @@ PyWeakref_NewProxy(PyObject *ob, PyObject *callback) result->ob_type = &_PyWeakref_CallableProxyType; else result->ob_type = &_PyWeakref_ProxyType; - result->wr_object = ob; - Py_XINCREF(callback); - result->wr_callback = callback; if (callback == NULL) prev = ref; else @@ -644,7 +630,6 @@ PyWeakref_NewProxy(PyObject *ob, PyObject *callback) insert_head(result, list); else insert_after(result, prev); - PyObject_GC_Track(result); } } return (PyObject *) result;