Skip to content

Commit

Permalink
Minor amendments
Browse files Browse the repository at this point in the history
  • Loading branch information
evgTSV authored and ForNeVeR committed Nov 14, 2024
1 parent 584e7e7 commit 124387e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
4 changes: 2 additions & 2 deletions Cesium.Runtime.Tests/StdIoFunctionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ private static unsafe (int, string) TestFPrintF(string format, long input)
var formatEncoded = Encoding.UTF8.GetBytes(format);

int exitCode;
var streamptr = (void*)IntPtr.Zero;
using var buffer = new MemoryStream();
var streamptr = IntPtr.Zero;
try
{
using var writer = new StreamWriter(buffer);
Expand All @@ -50,7 +50,7 @@ private static unsafe (int, string) TestFPrintF(string format, long input)
}
finally
{
StdIoFunctions.RemoveStream(streamptr);
StdIoFunctions.FreeStream(streamptr);
}

return (exitCode, Encoding.UTF8.GetString(buffer.ToArray()));
Expand Down
46 changes: 25 additions & 21 deletions Cesium.Runtime/StdIoFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static int GetChar()

public static int FPutS(byte* str, void* stream)
{
var streamHandle = GetStream((IntPtr)stream);
var streamHandle = GetStream(stream);
if (streamHandle == null)
{
return -1;
Expand Down Expand Up @@ -115,7 +115,7 @@ public static int PutC(byte character, void* stream)
{
try
{
var streamDescriptor = GetStream((IntPtr)stream);
var streamDescriptor = GetStream(stream);
if (streamDescriptor == null)
{
return -1;
Expand All @@ -133,7 +133,7 @@ public static int PutC(byte character, void* stream)

public static int PrintF(byte* str, void* varargs)
{
return FPrintF((void*)(IntPtr)StdOut, str, varargs);
return FPrintF((void*)StdOut, str, varargs);
}

public static int FPrintF(void* stream, byte* str, void* varargs)
Expand All @@ -144,7 +144,7 @@ public static int FPrintF(void* stream, byte* str, void* varargs)
return -1;
}

var streamHandle = GetStream((IntPtr)stream);
var streamHandle = GetStream(stream);
if (streamHandle == null)
{
return -1;
Expand Down Expand Up @@ -536,25 +536,25 @@ public static int FOpenS(void** streamptr, byte* filename, byte* mode)
handle.Writer = () => new StreamWriter(stream);
}

*streamptr = (void*)AddStream(handle);
*streamptr = AddStream(handle);
return 0;
}

public static int FClose(void* stream)
{
var streamHandle = GetStream((IntPtr)stream);
var streamHandle = GetStream(stream);
if (streamHandle == null)
{
return ErrNo.EBADF;
}

streamHandle.Stream!.Close();
return RemoveStream((IntPtr)stream) ? 0 : ErrNo.EBADF;
return FreeStream(stream) ? 0 : ErrNo.EBADF;
}

public static int FGetC(void* stream)
{
var streamHandle = GetStream((IntPtr)stream);
var streamHandle = GetStream(stream);
if (streamHandle == null)
{
return ErrNo.EBADF;
Expand All @@ -581,7 +581,7 @@ private static int GetCharacterFromFile(StreamHandle streamHandle)

public static byte* FGetS(byte* str, int count, void* stream)
{
var streamHandle = GetStream((IntPtr)stream);
var streamHandle = GetStream(stream);
if (streamHandle == null)
{
return null;
Expand Down Expand Up @@ -611,7 +611,7 @@ private static int GetCharacterFromFile(StreamHandle streamHandle)

public static int FEof(void* stream)
{
var streamHandle = GetStream((IntPtr)stream);
var streamHandle = GetStream(stream);
if (streamHandle == null)
{
return ErrNo.EBADF;
Expand All @@ -622,7 +622,7 @@ public static int FEof(void* stream)

public static int FSeek(void* stream, long offset, int origin)
{
var streamHandle = GetStream((IntPtr)stream);
var streamHandle = GetStream(stream);
if (streamHandle == null)
{
return ErrNo.EBADF;
Expand All @@ -634,7 +634,7 @@ public static int FSeek(void* stream, long offset, int origin)

public static int FError(void* stream)
{
var streamHandle = GetStream((IntPtr)stream);
var streamHandle = GetStream(stream);
if (streamHandle == null)
{
return ErrNo.EBADF;
Expand All @@ -645,7 +645,7 @@ public static int FError(void* stream)

public static int Rewind(void* stream)
{
var streamHandle = GetStream((IntPtr)stream);
var streamHandle = GetStream(stream);
if (streamHandle == null)
{
return ErrNo.EBADF;
Expand All @@ -667,8 +667,10 @@ public static int Remove(byte* pathname)
return 0;
}

internal static StreamHandle? GetStream(IntPtr handle)
internal static StreamHandle? GetStream(void* filePtr)
{
var handle = (IntPtr)filePtr;

var handleValue = handle.ToInt64();
if (handleValue is StdIn or StdOut or StdErr)
{
Expand All @@ -685,31 +687,33 @@ public static int Remove(byte* pathname)
return gch.Target as StreamHandle;
}
}
catch (AccessViolationException)
catch (InvalidOperationException)
{
return null;
}
}

internal static IntPtr AddStream(StreamHandle stream)
internal static void* AddStream(StreamHandle stream)
{
lock (_locker)
{
var gch = GCHandle.Alloc(stream);
var handel = GCHandle.ToIntPtr(gch);
var handle = GCHandle.ToIntPtr(gch);

var ptr = Marshal.AllocHGlobal(sizeof(IntPtr));
Marshal.WriteIntPtr(ptr, handel);
return ptr;
Marshal.WriteIntPtr(ptr, handle);
return (void*)ptr;
}
}

internal static bool RemoveStream(IntPtr handle)
internal static bool FreeStream(void* filePtr)
{
try
{
lock (_locker)
{
var handle = (IntPtr)filePtr;

var gchAddr = Marshal.ReadIntPtr(handle);
var gch = GCHandle.FromIntPtr(gchAddr);

Expand All @@ -720,7 +724,7 @@ internal static bool RemoveStream(IntPtr handle)
return true;
}
}
catch (AccessViolationException)
catch (InvalidOperationException)
{
return false;
}
Expand Down

0 comments on commit 124387e

Please sign in to comment.