Skip to content

Conversation

@smaillet
Copy link
Member

Update to support custom memory allocation for JIT and dramatically simplify managed objects as contexts for native callbacks.

* This adds the types but doesn't yet use/test them
    - Future PR will add them once testing is done
* Fixed declaration of native callback function pointers
    - Can't use bool for LLVMBool as that requires marshaling
* Added debug assert for `SafeGCHandle.AddRefAndGetNativeContext` to complain in a debug build if the underlying AddRef fails for some reason.
    - Docs on behavior of that API are a bit dodgy and it isn't clear how it could ever provide an out parameter of non-success
* Added additional preprocessor checks to disable unused code
* Added `IJitMemoryAllocator` to support either MCJIT simple allocator or an OrcJitV2 Object layer.
* Readme and doc comment corrections/clarifications
Native contexts are effectively an opaque `void*` they can have any value that fits in a pointer. The native layers don't care about it and store the value to provide as a parameter to callback functions. The implementation of the callback has all the knowledge to interpret the intended meaning of the context value.

For managed code this is an allocated GCHandle converted to a pointer. The managed code can resolve the pointer to a handle, and then the target instance to get a viable managed ref. When no more call backs can occur the context is released. This simplifies the use of callbacks as a native context can be created for any managed heap object. (Extension method added for this action)

* Updated comments
* Removed some dead code
* Added comments to explain limits of the C# 14 keyword `extension` and why it isn't used.
* Removed SafeGCHandle and MarshalGCHandles as context handles are MUCH simpler now.
* Added the memory allocator base types
    - These are not yet used or tested and are subject to change in the future and therfore attributed as experimental.
* Defined interface for materializer callbacks to allow internal implementation to hold delegates as a single unit to become a native context for call backs.
* Removed unnecessary using statements
@github-actions
Copy link

Test Results

291 tests   176 ✅  13s ⏱️
  8 suites  115 💤
  8 files      0 ❌

Results for commit ce9db22.

@smaillet smaillet merged commit d6b4e37 into UbiquityDotNET:develop Oct 23, 2025
4 checks passed
@smaillet smaillet deleted the CustomMemoryManager branch October 23, 2025 23:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant