-
Notifications
You must be signed in to change notification settings - Fork 559
Closed as not planned
Labels
enhancementThe issue or pull request is an enhancementThe issue or pull request is an enhancementiOSIssues affecting iOSIssues affecting iOSmacOSIssues affecting macOSIssues affecting macOSrequest-for-commentsThe issue is a suggested idea seeking feedbackThe issue is a suggested idea seeking feedback
Milestone
Description
The current pattern generated by the generator (and which we copy around liberally), goes something like this:
unsafe {
BlockLiteral *block_ptr_handler;
BlockLiteral block_handler;
block_handler = new BlockLiteral ();
block_ptr_handler = &block_handler;
block_handler.SetupBlockUnsafe (blockCallback, userCallback);
NativePInvoke ((void*) block_ptr_handler);
block_ptr_handler->CleanupBlock ();
}I think we can make it possible to simplify this a lot, so that we get down to:
using (var block = new BlockLiteral (blockCallback, userCallback, false))
NativePInvoke (ref block);This requires:
- Make
BlockLiteralimplementIDisposable(and callCleanupBlockinDispose). - Add a
BlockLiteralconstructor that takes the corresponding delegates. - Add support to the linker/optimizer for the new
BlockLiteralconstructor. - Change P/Invokes to take
ref BlockLiteralinstead ofvoid *orIntPtr(this only works if the block isn't nullable).
Advantages
- Much smaller & simpler C# code.
- We won't leak the block if the P/Invoke (or any code after setting up the block) throws an exception.
Disadvantages
- I think we'd end up creating more compiled (both IL and native), because of the additional exception handling by the using statement. This would need to be confirmed by testing though.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
enhancementThe issue or pull request is an enhancementThe issue or pull request is an enhancementiOSIssues affecting iOSIssues affecting iOSmacOSIssues affecting macOSIssues affecting macOSrequest-for-commentsThe issue is a suggested idea seeking feedbackThe issue is a suggested idea seeking feedback