Skip to content

Conversation

@konard
Copy link
Member

@konard konard commented Sep 14, 2025

Summary

This PR implements a solution for issue #22 by providing adapters that convert IArrayMemory<TElement> into IDirectMemory. The implementation uses GCHandle.Alloc() with GCHandleType.Pinned to pin managed arrays in memory and provide stable pointers for direct memory access.

Key Features:

  • ArrayMemoryAsDirectMemoryAdapter: Optimized adapter specifically for ArrayMemory<TElement> that uses reflection to access the internal array and pin it directly
  • ArrayMemoryAsDirectMemoryAdapterGeneral: General-purpose adapter that works with any IArrayMemory<TElement> implementation by creating a temporary array copy and pinning it
  • Comprehensive memory safety: Both adapters properly manage GCHandle lifecycle and provide stable pointer access
  • Full test coverage: Unit tests verify functionality, pointer stability, memory safety, and proper dispose behavior

Technical Approach:

The solution leverages the .NET GC pinning mechanism referenced in the issue comments:

  • Uses GCHandle.Alloc(array, GCHandleType.Pinned) to prevent GC from moving the array
  • Implements IDisposable pattern to ensure proper cleanup of pinned handles
  • Provides stable IntPtr access through the IDirectMemory.Pointer property

Usage Examples:

// Using the optimized adapter for ArrayMemory
var arrayMemory = new ArrayMemory<int>(100);
using var directAdapter = new ArrayMemoryAsDirectMemoryAdapter<int>(arrayMemory);
var pointer = directAdapter.Pointer; // Stable pointer for direct access

// Using the general adapter for any IArrayMemory implementation  
var fileArrayMemory = new FileArrayMemory<byte>("data.bin");
using var generalAdapter = new ArrayMemoryAsDirectMemoryAdapterGeneral<byte>(fileArrayMemory);
var filePointer = generalAdapter.Pointer; // Works with file-backed memory too

Test Plan

  • ✅ All existing tests pass
  • ✅ New comprehensive test suite covering both adapters
  • ✅ Memory safety tests verify proper GC handle management
  • ✅ Pointer stability tests ensure pinning works correctly
  • ✅ Disposal tests verify proper cleanup
  • ✅ Cross-implementation tests (works with FileArrayMemory, etc.)
  • ✅ Build passes in both Debug and Release configurations

🤖 Generated with Claude Code


Resolves #22

Adding CLAUDE.md with task information for AI processing.
This file will be removed when the task is complete.

Issue: #22
@konard konard self-assigned this Sep 14, 2025
Implement solution for issue #22 by creating adapters that convert
IArrayMemory into IDirectMemory through GCHandle pinning mechanism.

- ArrayMemoryAsDirectMemoryAdapter: Optimized adapter for ArrayMemory<T>
  using reflection to access internal array and pin it directly
- ArrayMemoryAsDirectMemoryAdapterGeneral: General adapter for any
  IArrayMemory<T> implementation using array copy and pinning strategy
- Comprehensive unit tests covering both adapters with memory safety
  and pointer stability verification

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@konard konard changed the title [WIP] Think about a way to convert IArrayMemory into IDirectMemory Add ArrayMemory to DirectMemory adapter implementation (Fixes #22) Sep 14, 2025
@konard konard marked this pull request as ready for review September 14, 2025 07:19
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.

Think about a way to convert IArrayMemory into IDirectMemory

2 participants