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.
This commit is contained in:
Neil Schemenauer 2002-03-27 15:18:21 +00:00
parent 9e38c100ab
commit 38a8916134
1 changed files with 11 additions and 26 deletions

View File

@ -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;