Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions src/Interop/Ubiquity.NET.Llvm.Interop/ABI/llvm-c/Orc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,9 @@ public static unsafe partial void LLVMOrcExecutionSessionLookup(
[UnmanagedCallConv( CallConvs = [ typeof( CallConvCdecl ) ] )]
public static unsafe partial void LLVMOrcReleaseSymbolStringPoolEntry( LLVMOrcSymbolStringPoolEntryRefAlias S );

// This does NOT marshal the string, it only provides the raw pointer so that a span is constructible
// from the pointer. The memory for the string is OWNED by the entry so the returned pointer is valid
// for the lifetime of the referenced entry.
// TODO: Consider if this can't or shouldn't be a LazyEncodedString to contain potentially both forms
// as needed.
[LibraryImport( LibraryName )]
[UnmanagedCallConv( CallConvs = [ typeof( CallConvCdecl ) ] )]
public static unsafe partial byte* LLVMOrcSymbolStringPoolEntryStr( LLVMOrcSymbolStringPoolEntryRef S );
public static unsafe partial LazyEncodedString LLVMOrcSymbolStringPoolEntryStr( LLVMOrcSymbolStringPoolEntryRef S );

[LibraryImport( LibraryName )]
[UnmanagedCallConv( CallConvs = [ typeof( CallConvCdecl ) ] )]
Expand Down
26 changes: 8 additions & 18 deletions src/Ubiquity.NET.Llvm/OrcJITv2/SymbolStringPoolEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Ubiquity.NET.Llvm.OrcJITv2
{
/// <summary>Reference to an entry in a symbol string pool for ORC JIT v2</summary>
/// <remarks>
/// This holds a reference to the symbol string which is ONLY marshalled/converted to
/// This holds a reference to the symbol string which is ONLY marshaled/converted to
/// a managed string in the <see cref="ToString"/> method. This allows comparing strings
/// etc... without the need of conversion.
/// <note type="information">
Expand Down Expand Up @@ -48,7 +48,7 @@ public SymbolStringPoolEntry( SymbolStringPoolEntry other )
{
ObjectDisposedException.ThrowIf( IsDisposed, this );

return ManagedString.Value;
return DeferredInitSymbolString.Value.ToString();
}

#region IEquatable<SymbolStringPoolEntry>
Expand Down Expand Up @@ -119,7 +119,7 @@ public ReadOnlySpan<byte> ReadOnlySpan

unsafe
{
return new( (void*)NativeStringPtr.Value, LazyStrLen.Value );
return DeferredInitSymbolString.Value.ToReadOnlySpan();
}
}
}
Expand All @@ -140,23 +140,13 @@ internal SymbolStringPoolEntry( LLVMOrcSymbolStringPoolEntryRef h )
}

Handle = h.Move();
unsafe
{
NativeStringPtr = new( ( ) => (nint)LLVMOrcSymbolStringPoolEntryStr( Handle ), LazyThreadSafetyMode.ExecutionAndPublication );
ManagedString = new( ( ) => ExecutionEncodingStringMarshaller.ConvertToManaged( (byte*)NativeStringPtr.Value ), LazyThreadSafetyMode.ExecutionAndPublication );
LazyStrLen = new( ( ) => MemoryMarshal.CreateReadOnlySpanFromNullTerminated( (byte*)NativeStringPtr.Value ).Length, LazyThreadSafetyMode.ExecutionAndPublication );
}
DeferredInitSymbolString = new(()=>LLVMOrcSymbolStringPoolEntryStr( Handle ), LazyThreadSafetyMode.PublicationOnly);
}

internal SymbolStringPoolEntry( nint abiHandle, bool alias = false )
{
Handle = new( abiHandle, !alias );
unsafe
{
NativeStringPtr = new( ( ) => (nint)LLVMOrcSymbolStringPoolEntryStr( Handle ), LazyThreadSafetyMode.ExecutionAndPublication );
ManagedString = new( ( ) => ExecutionEncodingStringMarshaller.ConvertToManaged( (byte*)NativeStringPtr.Value ), LazyThreadSafetyMode.ExecutionAndPublication );
LazyStrLen = new( ( ) => MemoryMarshal.CreateReadOnlySpanFromNullTerminated( (byte*)NativeStringPtr.Value ).Length, LazyThreadSafetyMode.ExecutionAndPublication );
}
DeferredInitSymbolString = new(()=>LLVMOrcSymbolStringPoolEntryStr( Handle ), LazyThreadSafetyMode.PublicationOnly);
}

internal LLVMOrcSymbolStringPoolEntryRef Handle { get; }
Expand Down Expand Up @@ -195,9 +185,9 @@ private LLVMOrcSymbolStringPoolEntryRef AddRefHandle( )
return new( Handle.DangerousGetHandle(), owner: true );
}

private readonly Lazy<string?> ManagedString;
private readonly Lazy<int> LazyStrLen; // count of bytes in the native string (Not including null terminator)
private readonly Lazy<nint> NativeStringPtr;
// Callers might never need the contents of the string so it is lazily
// initialize when needed. String pool entries are interned and thus are immutable, like a .NET string
private readonly Lazy<LazyEncodedString> DeferredInitSymbolString;
}

[SuppressMessage( "StyleCop.CSharp.MaintainabilityRules", "SA1400:Access modifier should be declared", Justification = "'file' is an accessibility" )]
Expand Down
Loading