Skip to content

Commit

Permalink
Opaque typed records: Support adding memory pressure
Browse files Browse the repository at this point in the history
  • Loading branch information
badcel committed Aug 18, 2024
1 parent 2b54394 commit 2d1645c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected override bool ReleaseHandle()
}}
}}
public class {ownedHandleTypeName} : {typeName}
public partial class {ownedHandleTypeName} : {typeName}
{{
/// <summary>
/// Creates a new instance of {ownedHandleTypeName}. Used automatically by PInvoke.
Expand All @@ -113,14 +113,23 @@ public class {ownedHandleTypeName} : {typeName}
/// <returns>A {ownedHandleTypeName}</returns>
public static {ownedHandleTypeName} FromUnowned(IntPtr ptr)
{{
{RenderCopyStatement(record, "ownedPtr", "ptr")}
return new {ownedHandleTypeName}(ownedPtr);
{RenderCopyStatement(record, "ownedPtr", "ptr")}
return new {ownedHandleTypeName}(ownedPtr);
}}
internal void SetMemoryPressure()
{{
AddMemoryPressure();
}}
partial void AddMemoryPressure();
partial void RemoveMemoryPressure();
protected override bool ReleaseHandle()
{{
{RenderFreeStatement(record, "handle")}
return true;
RemoveMemoryPressure();
{RenderFreeStatement(record, "handle")}
return true;
}}
}}";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public sealed partial class {name} : GLib.BoxedRecord, IEquatable<{name}>, IDisp
public {name}({internalHandleName} handle)
{{
Handle = handle;
Handle.SetMemoryPressure();
Initialize();
}}
Expand Down
19 changes: 19 additions & 0 deletions src/Libs/GLib-2.0/Internal/BytesHandle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;

namespace GLib.Internal;

public partial class BytesOwnedHandle
{
private long _pressure;

partial void AddMemoryPressure()
{
_pressure = (long) Bytes.GetSize(this);
GC.AddMemoryPressure(_pressure);
}

partial void RemoveMemoryPressure()
{
GC.RemoveMemoryPressure(_pressure);
}
}
19 changes: 0 additions & 19 deletions src/Libs/GLib-2.0/Public/Bytes.cs

This file was deleted.

0 comments on commit 2d1645c

Please sign in to comment.