Enhance mathematical capabilities and job system
Added new numeric types for unsigned integers, including uint2, uint3, and uint4, along with their matrix types. Added a new `quaternion` struct with constructors and methods for creating and manipulating quaternions. Added methods for projecting and reflecting vectors, enhancing geometric operations. Added utility functions for generating orthonormal bases and changing vector signs. Added comprehensive unit tests for new mathematical functions and quaternion operations. Added a high-performance job scheduling system with job management features and worker thread management. Added new structs for job execution, allowing efficient job scheduling and execution. Added utility functions for job execution, including methods for obtaining unique job IDs. Changed access modifiers and property definitions in several files for improved clarity and maintainability. Changed property definitions and method implementations in `ImageInfo.cs`, `ImageResult.cs`, and `ImageResultFloat.cs` for better readability. Changed memory management functions in `CRuntime.cs` and improved memory allocation tracking in `MemoryStats.cs`. Changed the project file to include references to necessary projects and enable unsafe code blocks. Removed the `WorkerThreadPool.cs` file, integrating worker thread management directly into the `JobScheduler`. Removed the `float4` struct and its associated methods and properties, transitioning to a new code generation strategy. Removed the `float4.tt` template and other related files, indicating a shift in code generation approach. Removed the `Vectorize.cs` file, indicating a change in how vector operations are handled. Updated the `.gitignore` file to include IDE-specific settings. Updated various XML files to define project components and structure. Updated the `AllocationManager.cs` to improve memory allocation management and introduce new strategies. Updated the `UnsafeArray.cs`, `UnsafeHashMap.cs`, and `UnsafeList.cs` to enhance performance and safety in unsafe contexts. Updated error handling and function pointer management in `MemoryLeakException.cs` and `FunctionPointer.cs`. Updated the `AssemblyInfo.cs` file to include global using directives for better code organization.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
using Misaki.HighPerformance.LowLevel.Collections;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Misaki.HighPerformance.LowLevel.Buffer;
|
||||
|
||||
@@ -6,29 +6,33 @@ namespace Misaki.HighPerformance.LowLevel.Buffer;
|
||||
/// A dynamic memory management structure that automatically grows by creating linked arenas
|
||||
/// when more space is needed.
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Explicit, Size = 128)]
|
||||
public unsafe struct DynamicArena : IDisposable
|
||||
{
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
private struct ArenaNode
|
||||
{
|
||||
public Arena arena;
|
||||
public ArenaNode* next;
|
||||
}
|
||||
|
||||
[FieldOffset(0)]
|
||||
private ArenaNode* _root;
|
||||
[FieldOffset(8)]
|
||||
private ArenaNode* _current;
|
||||
[FieldOffset(16)]
|
||||
private uint _initialSize;
|
||||
|
||||
[FieldOffset(20)]
|
||||
private volatile int _nodeCreationLock;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of DynamicArena with the specified initial size.
|
||||
/// </summary>
|
||||
/// <param name="initialSize">The initial size in bytes for the first arena block.</param>
|
||||
public DynamicArena(uint initialSize)
|
||||
{
|
||||
_initialSize = initialSize;
|
||||
_root = (ArenaNode*)Malloc(SizeOf<ArenaNode>());
|
||||
_root->arena = new Arena(initialSize);
|
||||
_root->next = null;
|
||||
_current = _root;
|
||||
Initialize(initialSize);
|
||||
}
|
||||
|
||||
public void Initialize(uint initialSize)
|
||||
@@ -45,23 +49,63 @@ public unsafe struct DynamicArena : IDisposable
|
||||
_current = _root;
|
||||
}
|
||||
|
||||
private bool CreateNewNode(nuint size)
|
||||
private bool TryCreateNewNode(nuint size)
|
||||
{
|
||||
var newNode = (ArenaNode*)Malloc(SizeOf<ArenaNode>());
|
||||
while (Interlocked.CompareExchange(ref _nodeCreationLock, 1, 0) != 0)
|
||||
{
|
||||
Thread.SpinWait(1);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
newNode->arena = new Arena(size);
|
||||
newNode->next = null;
|
||||
var current = _current;
|
||||
if (current->next != null)
|
||||
{
|
||||
// Another thread created a node while we were waiting
|
||||
_current = current->next;
|
||||
return true;
|
||||
}
|
||||
|
||||
_current->next = newNode;
|
||||
_current = newNode;
|
||||
return true;
|
||||
var newNode = (ArenaNode*)Malloc(SizeOf<ArenaNode>());
|
||||
try
|
||||
{
|
||||
newNode->arena = new Arena(size);
|
||||
newNode->next = null;
|
||||
|
||||
// Atomically link the new node
|
||||
current->next = newNode;
|
||||
|
||||
// Update current pointer
|
||||
_current = newNode;
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
Free(newNode);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
catch
|
||||
finally
|
||||
{
|
||||
Free(newNode);
|
||||
return false;
|
||||
// Release the spinlock
|
||||
Interlocked.Exchange(ref _nodeCreationLock, 0);
|
||||
}
|
||||
|
||||
//var newNode = (ArenaNode*)Malloc(SizeOf<ArenaNode>());
|
||||
//try
|
||||
//{
|
||||
// newNode->arena = new Arena(size);
|
||||
// newNode->next = null;
|
||||
|
||||
// _current->next = newNode;
|
||||
// _current = newNode;
|
||||
// return true;
|
||||
//}
|
||||
//catch
|
||||
//{
|
||||
// Free(newNode);
|
||||
// return false;
|
||||
//}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -89,7 +133,7 @@ public unsafe struct DynamicArena : IDisposable
|
||||
return result;
|
||||
}
|
||||
|
||||
if (current->next == null && !CreateNewNode(Math.Max(size, _initialSize)))
|
||||
if (current->next == null && !TryCreateNewNode(Math.Max(size, _initialSize)))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user