Skip to content

Commit 581f695

Browse files
committed
assume remaning manual refcounting is not needed, because we use smart references
1 parent 9a9ed3b commit 581f695

File tree

7 files changed

+16
-45
lines changed

7 files changed

+16
-45
lines changed

src/runtime/classmanager.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,6 @@ private static void InitClassBase(Type type, ClassBase impl, PyType pyType)
303303
impl.dotNetMembers.Add(name);
304304
Runtime.PyDict_SetItemString(dict, name, item.ObjectReference);
305305
// Decref the item now that it's been used.
306-
item.DecrRefCount();
307306
if (ClassBase.CilToPyOpMap.TryGetValue(name, out var pyOp)) {
308307
impl.richcompare.Add(pyOp, (MethodObject)item);
309308
}
@@ -336,7 +335,6 @@ private static void InitClassBase(Type type, ClassBase impl, PyType pyType)
336335
// TODO: deprecate __overloads__ soon...
337336
Runtime.PyDict_SetItem(dict, PyIdentifier.__overloads__, ctors.ObjectReference);
338337
Runtime.PyDict_SetItem(dict, PyIdentifier.Overloads, ctors.ObjectReference);
339-
ctors.DecrRefCount();
340338
}
341339

342340
// don't generate the docstring if one was already set from a DocStringAttribute.
@@ -567,7 +565,6 @@ private static ClassInfo GetClassInfo(Type type)
567565
}
568566
Debug.Assert(ob.pyHandle is not null);
569567
// GetClass returns a Borrowed ref. ci.members owns the reference.
570-
ob.IncrRefCount();
571568
ci.members[mi.Name] = ob;
572569
continue;
573570
}

src/runtime/extensiontype.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,11 @@ protected virtual void Dealloc()
5858
{
5959
var type = Runtime.PyObject_TYPE(this.ObjectReference);
6060
Runtime.PyObject_GC_Del(this.pyHandle);
61-
// Not necessary for decref of `tpHandle` - it is borrowed
6261

6362
this.FreeGCHandle();
6463

6564
// we must decref our type: https://docs.python.org/3/c-api/typeobj.html#c.PyTypeObject.tp_dealloc
66-
Runtime.XDecref(type.DangerousGetAddress());
65+
Runtime.XDecref(StolenReference.DangerousFromPointer(type.DangerousGetAddress()));
6766
}
6867

6968
/// <summary>DecRefs and nulls any fields pointing back to Python</summary>

src/runtime/managedtype.cs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,6 @@ internal BorrowedReference TypeReference
5050

5151
private static readonly Dictionary<ManagedType, TrackTypes> _managedObjs = new Dictionary<ManagedType, TrackTypes>();
5252

53-
[Obsolete]
54-
internal void IncrRefCount()
55-
{
56-
Runtime.XIncref(pyHandle);
57-
}
58-
59-
[Obsolete]
60-
internal void DecrRefCount()
61-
{
62-
Runtime.XDecref(pyHandle);
63-
}
64-
6553
internal long RefCount
6654
{
6755
get
@@ -268,12 +256,12 @@ protected static BorrowedReference GetObjectDict(BorrowedReference ob)
268256
return Util.ReadRef(ob, instanceDictOffset);
269257
}
270258

271-
protected static void SetObjectDict(BorrowedReference ob, in StolenReference value)
259+
protected static void SetObjectDict(BorrowedReference ob, StolenReference value)
272260
{
273261
if (value.Pointer == IntPtr.Zero) throw new ArgumentNullException(nameof(value));
274262
SetObjectDictNullable(ob, value);
275263
}
276-
protected static void SetObjectDictNullable(BorrowedReference ob, in StolenReference value)
264+
protected static void SetObjectDictNullable(BorrowedReference ob, StolenReference value)
277265
{
278266
BorrowedReference type = Runtime.PyObject_TYPE(ob);
279267
int instanceDictOffset = Util.ReadInt32(type, TypeOffset.tp_dictoffset);

src/runtime/metatype.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,10 @@ public static void tp_dealloc(NewReference tp)
300300
#endif
301301
}
302302

303-
BorrowedReference op = Util.ReadRef(tp.Borrow(), TypeOffset.ob_type);
304-
Runtime.XDecref(op);
303+
var op = Util.ReadIntPtr(tp.Borrow(), TypeOffset.ob_type);
304+
// We must decref our type.
305+
// type_dealloc from PyType will use it to get tp_free so we must keep the value
306+
Runtime.XDecref(StolenReference.DangerousFromPointer(op));
305307

306308
// Delegate the rest of finalization the Python metatype. Note
307309
// that the PyType_Type implementation of tp_dealloc will call

src/runtime/moduleobject.cs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ internal class ModuleObject : ExtensionType
2222

2323
// Attributes to be set on the module according to PEP302 and 451
2424
// by the import machinery.
25-
static readonly HashSet<string> settableAttributes =
26-
new HashSet<string> {"__spec__", "__file__", "__name__", "__path__", "__loader__", "__package__"};
25+
static readonly HashSet<string?> settableAttributes =
26+
new () {"__spec__", "__file__", "__name__", "__path__", "__loader__", "__package__"};
2727

2828
public ModuleObject(string name)
2929
{
@@ -102,7 +102,6 @@ public ModuleObject(string name)
102102
{
103103
m = new ModuleObject(qname);
104104
StoreAttribute(name, m);
105-
m.DecrRefCount();
106105
return m;
107106
}
108107

@@ -156,7 +155,6 @@ private void StoreAttribute(string name, ManagedType ob)
156155
{
157156
throw PythonException.ThrowLastAsClrException();
158157
}
159-
ob.IncrRefCount();
160158
cache[name] = ob;
161159
}
162160

@@ -221,7 +219,6 @@ internal void InitializeModuleMembers()
221219
mi[0] = method;
222220
var m = new ModuleFunctionObject(type, name, mi, allow_threads);
223221
StoreAttribute(name, m);
224-
m.DecrRefCount();
225222
}
226223
}
227224

@@ -234,7 +231,6 @@ internal void InitializeModuleMembers()
234231
string name = property.Name;
235232
var p = new ModulePropertyObject(property);
236233
StoreAttribute(name, p);
237-
p.DecrRefCount();
238234
}
239235
}
240236
type = type.BaseType;
@@ -325,10 +321,6 @@ protected override void Clear()
325321
{
326322
this.dict.Dispose();
327323
ClearObjectDict(this.ObjectReference);
328-
foreach (var attr in this.cache.Values)
329-
{
330-
Runtime.XDecref(attr.pyHandle);
331-
}
332324
this.cache.Clear();
333325
base.Clear();
334326
}
@@ -356,13 +348,6 @@ protected override void OnSave(InterDomainContext context)
356348
{
357349
base.OnSave(context);
358350
System.Diagnostics.Debug.Assert(dict == GetObjectDict(ObjectReference));
359-
foreach (var attr in cache.Values)
360-
{
361-
Runtime.XIncref(attr.pyHandle);
362-
}
363-
// Decref twice in tp_clear, equilibrate them.
364-
Runtime.XIncref(dict);
365-
Runtime.XIncref(dict);
366351
// destroy the cache(s)
367352
foreach (var pair in cache)
368353
{
@@ -377,7 +362,6 @@ protected override void OnSave(InterDomainContext context)
377362
{
378363
throw PythonException.ThrowLastAsClrException();
379364
}
380-
pair.Value.DecrRefCount();
381365
}
382366

383367
cache.Clear();
@@ -386,7 +370,7 @@ protected override void OnSave(InterDomainContext context)
386370
protected override void OnLoad(InterDomainContext context)
387371
{
388372
base.OnLoad(context);
389-
SetObjectDict(pyHandle, dict);
373+
SetObjectDict(pyHandle, new NewReference(dict).Steal());
390374
}
391375
}
392376

src/runtime/runtime.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -433,8 +433,9 @@ private static void ClearClrModules()
433433
{
434434
var modules = PyImport_GetModuleDict();
435435
using var items = PyDict_Items(modules);
436-
long length = PyList_Size(items.Borrow());
437-
for (long i = 0; i < length; i++)
436+
nint length = PyList_Size(items.BorrowOrThrow());
437+
if (length < 0) throw PythonException.ThrowLastAsClrException();
438+
for (nint i = 0; i < length; i++)
438439
{
439440
var item = PyList_GetItem(items.Borrow(), i);
440441
var name = PyTuple_GetItem(item, 0);
@@ -676,6 +677,7 @@ internal static unsafe void XDecref(StolenReference op)
676677
Debug.Assert(_isInitialized || Py_IsInitialized() != 0);
677678
#endif
678679
#if !CUSTOM_INCDEC_REF
680+
if (op == null) return;
679681
Py_DecRef(op);
680682
return;
681683
#else
@@ -1881,11 +1883,11 @@ internal static void Py_CLEAR<T>(ref T? ob)
18811883
ob = null;
18821884
}
18831885

1884-
internal static void ReplaceReference(BorrowedReference ob, int offset, in StolenReference newValue)
1886+
internal static void ReplaceReference(BorrowedReference ob, int offset, StolenReference newValue)
18851887
{
18861888
IntPtr raw = Util.ReadIntPtr(ob, offset);
18871889
Util.WriteNullableRef(ob, offset, newValue);
1888-
XDecref(StolenReference.Take(ref raw));
1890+
XDecref(StolenReference.TakeNullable(ref raw));
18891891
}
18901892

18911893
//====================================================================

src/runtime/typemanager.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,6 @@ private static void InitMethods(BorrowedReference typeDict, Type type)
829829
mi[0] = method;
830830
MethodObject m = new TypeMethod(type, method_name, mi);
831831
Runtime.PyDict_SetItemString(typeDict, method_name, m.ObjectReference);
832-
m.DecrRefCount();
833832
addedMethods.Add(method_name);
834833
}
835834
}

0 commit comments

Comments
 (0)