Commit 148a553
committed
[generator] Emit partial method for peer constructor
Context: #1085
Context: dotnet/runtime#82121
Some Java objects are *big*, e.g. [`Bitmap`][0] instances, but as
far as MonoVM is concerned, the `Bitmap` instances are *tiny*: a
few pointers, and that's it. MonoVM doesn't know that it could be
referencing several MB of data in the Java VM.
MonoVM is gaining support for [`GC.AddMemoryPressure()`][1] and
[`GC.RemoveMemoryPressure()`][2], which potentially allows for the
parent of all cross-VM GC integrations: using the `GC` methods to
inform MonoVM of how much non-managed memory has been allocated.
This could allow MonoVM to collect more frequently when total process
memory is low.
How should we call `GC.AddMemoryPressure()` and
`GC.RemoveMemoryPressure()`?
`GC.RemoveMemoryPressure()` is straightforward: a class can override
`Java.Lang.Object.Dispose(bool)`.
`GC.AddMemoryPressure()` is the problem: where and how can it be
called from a class binding? This is trickier, as there was no way
to have custom code called by the bound type. Instead, various
forms of "hacky workarounds" are often employed, e.g. copying
`generator`-emitted content into a `partial` class, then using
`metadata` to *prevent* `generator` from binding those members.
It's all around fugly.
Fortunately C# has a solution: [`partial` methods][3]!
We take our existing "peer constructor" generation code, a'la:
partial class Bitmap : Java.Lang.Object {
internal Bitmap (IntPtr h, JniHandleOwnership t) : base (h, t)
{
}
}
and extend it to:
partial class Bitmap : Java.Lang.Object {
partial void _OnBitmapCreated ();
internal Bitmap (IntPtr h, JniHandleOwnership t) : base (h, t)
{
_OnBitmapCreated ();
}
}
This allows a `partial class Bitmap` to do:
// Hand-written code
partial class Bitmap {
int _memoryPressure;
partial void _OnBitmapCreated ()
{
_memoryPressure = ByteCount;
GC.AddMemoryPressure (_memoryPressure);
}
protected override void Dispose (bool disposing)
{
if (_memoryPressure != 0) {
GC.RemoveMemoryPressure (_memoryPressure);
_memoryPressure = 0;
}
}
}
TODO: Are there any other places where such `partial` methods would
be useful? This appears to be the minimum required for this scenario.
[0]: https://developer.android.com/reference/android/graphics/Bitmap
[1]: https://learn.microsoft.com/dotnet/api/system.gc.addmemorypressure?view=net-7.0
[2]: https://learn.microsoft.com/dotnet/api/system.gc.removememorypressure?view=net-7.0
[3]: https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/partial-method1 parent 73ebad2 commit 148a553
File tree
95 files changed
+396
-0
lines changed- tests/generator-Tests
- Unit-Tests/CodeGeneratorExpectedResults
- JavaInterop1
- XAJavaInterop1-NRT
- XAJavaInterop1
- expected.ji
- AccessModifiers
- Android.Graphics.Color
- Arrays
- CSharpKeywords
- Constructors
- Core_ClassParse
- InterfaceMethodsConflict
- NonStaticFields
- NormalProperties
- StaticFields
- StaticMethods
- StaticProperties
- Streams
- expected.xaji
- AccessModifiers
- Adapters
- Android.Graphics.Color
- Arrays
- CSharpKeywords
- Constructors
- Core_ClassParse
- Core_Jar2Xml
- GenericArguments
- InterfaceMethodsConflict
- NestedTypes
- NonStaticFields
- NormalMethods
- NormalProperties
- ParameterXPath
- StaticFields
- StaticMethods
- StaticProperties
- Streams
- TestInterface
- java.lang.Enum
- java.util.List
- tools/generator/SourceWriters
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
95 files changed
+396
-0
lines changedLines changed: 4 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
6 | 9 | | |
7 | 10 | | |
| 11 | + | |
8 | 12 | | |
9 | 13 | | |
10 | 14 | | |
| |||
Lines changed: 4 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
6 | 9 | | |
7 | 10 | | |
| 11 | + | |
8 | 12 | | |
9 | 13 | | |
10 | 14 | | |
| |||
Lines changed: 4 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
6 | 9 | | |
7 | 10 | | |
| 11 | + | |
8 | 12 | | |
9 | 13 | | |
10 | 14 | | |
| |||
Lines changed: 4 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
6 | 9 | | |
7 | 10 | | |
| 11 | + | |
8 | 12 | | |
9 | 13 | | |
10 | 14 | | |
| |||
Lines changed: 4 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
6 | 9 | | |
7 | 10 | | |
| 11 | + | |
8 | 12 | | |
9 | 13 | | |
10 | 14 | | |
| |||
Lines changed: 4 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
12 | 15 | | |
13 | 16 | | |
| 17 | + | |
14 | 18 | | |
15 | 19 | | |
16 | 20 | | |
| |||
Lines changed: 4 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
26 | 29 | | |
27 | 30 | | |
| 31 | + | |
28 | 32 | | |
29 | 33 | | |
30 | 34 | | |
Lines changed: 4 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
10 | 13 | | |
11 | 14 | | |
| 15 | + | |
12 | 16 | | |
13 | 17 | | |
14 | 18 | | |
| |||
Lines changed: 4 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
28 | 31 | | |
29 | 32 | | |
| 33 | + | |
30 | 34 | | |
31 | 35 | | |
32 | 36 | | |
| |||
Lines changed: 4 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
32 | 35 | | |
33 | 36 | | |
| 37 | + | |
34 | 38 | | |
35 | 39 | | |
36 | 40 | | |
0 commit comments