Skip to content

Commit 8daa4e7

Browse files
committed
Add project files.
1 parent 10c66d2 commit 8daa4e7

29 files changed

+7440
-0
lines changed

Hexa.NET.Utilities.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.10.35027.167
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Hexa.NET.Utilities", "Hexa.NET.Utilities\Hexa.NET.Utilities.csproj", "{055E63D3-810C-4289-97D4-E27BCDB86548}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{055E63D3-810C-4289-97D4-E27BCDB86548}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{055E63D3-810C-4289-97D4-E27BCDB86548}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{055E63D3-810C-4289-97D4-E27BCDB86548}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{055E63D3-810C-4289-97D4-E27BCDB86548}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {33E217DF-77B4-4AE7-8F53-548592C9A6DD}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
namespace HexaEngine.Core.Unsafes
2+
{
3+
using System.Runtime.InteropServices;
4+
5+
public unsafe class AllocationCallbacks : IAllocationCallbacks
6+
{
7+
#if TRACELEAK
8+
private readonly List<Allocation> allocations = [];
9+
10+
public IReadOnlyList<Allocation> Allocations => allocations;
11+
#endif
12+
#if TRACELEAK
13+
14+
public struct Allocation
15+
{
16+
public void* Ptr;
17+
public nint Size;
18+
public string Caller;
19+
20+
public Allocation(void* ptr, nint size, string caller)
21+
{
22+
Ptr = ptr;
23+
Size = size;
24+
Caller = caller;
25+
}
26+
}
27+
28+
#endif
29+
30+
#if TRACELEAK
31+
32+
private void Remove(void* ptr)
33+
{
34+
if (ptr == null)
35+
{
36+
return;
37+
}
38+
39+
lock (allocations)
40+
{
41+
for (int i = 0; i < allocations.Count; i++)
42+
{
43+
Allocation allocation = allocations[i];
44+
if (allocation.Ptr == ptr)
45+
{
46+
allocations.RemoveAt(i);
47+
return;
48+
}
49+
}
50+
}
51+
}
52+
53+
#endif
54+
55+
public void ReportInstances()
56+
{
57+
#if TRACELEAK
58+
lock (allocations)
59+
{
60+
foreach (Allocation allocation in allocations)
61+
{
62+
Logger.Warn($"*** Live allocation ({allocation.Caller}):\n\t{(nint)allocation.Ptr:X}\n\tSize: {allocation.Size}\n");
63+
}
64+
}
65+
#endif
66+
}
67+
68+
public void ReportDuplicateInstances()
69+
{
70+
#if TRACELEAK
71+
lock (allocations)
72+
{
73+
foreach (Allocation allocation in allocations)
74+
{
75+
foreach (Allocation allocationCmp in allocations)
76+
{
77+
if (allocation.Caller == allocationCmp.Caller && allocation.Size == allocationCmp.Size)
78+
{
79+
Logger.Warn($"*** Possible duplicate instance ({allocation.Caller}):\n\t{(nint)allocation.Ptr:X}\n\tSize: {allocation.Size}\n");
80+
}
81+
}
82+
}
83+
}
84+
#endif
85+
}
86+
87+
public void* Alloc(nint size
88+
#if TRACELEAK
89+
, string name
90+
#endif
91+
)
92+
{
93+
void* ptr = (void*)Marshal.AllocHGlobal(size); ;
94+
#if TRACELEAK
95+
Allocation allocation = new(ptr, size, name);
96+
lock (allocations)
97+
{
98+
allocations.Add(allocation);
99+
}
100+
#endif
101+
return ptr;
102+
}
103+
104+
public void* ReAlloc(void* ptr, nint size
105+
#if TRACELEAK
106+
, string name
107+
#endif
108+
)
109+
{
110+
#if TRACELEAK
111+
Remove(ptr);
112+
#endif
113+
ptr = (void*)Marshal.ReAllocHGlobal((nint)ptr, size); ;
114+
#if TRACELEAK
115+
Allocation allocation = new(ptr, size, name);
116+
lock (allocations)
117+
{
118+
allocations.Add(allocation);
119+
}
120+
#endif
121+
return ptr;
122+
}
123+
124+
public void Free(void* ptr)
125+
{
126+
#if TRACELEAK
127+
Remove(ptr);
128+
#endif
129+
Marshal.FreeHGlobal((nint)ptr);
130+
}
131+
}
132+
}

Hexa.NET.Utilities/ArrayUtils.cs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
namespace HexaEngine.Core
2+
{
3+
using System;
4+
5+
/// <summary>
6+
/// Utilities for managed and unmanaged arrays.
7+
/// </summary>
8+
public static unsafe class ArrayUtils
9+
{
10+
/// <summary>
11+
/// Adds a value to an array and resizes it to accommodate the new value.
12+
/// </summary>
13+
/// <typeparam name="T">The type of elements in the array.</typeparam>
14+
/// <param name="array">Reference to the array to which the value will be added.</param>
15+
/// <param name="value">The value to add to the array.</param>
16+
public static void Add<T>(ref T[] array, T value)
17+
{
18+
Array.Resize(ref array, array.Length + 1);
19+
array[^1] = value;
20+
}
21+
22+
/// <summary>
23+
/// Removes the first occurrence of a value from an array and resizes it accordingly.
24+
/// </summary>
25+
/// <typeparam name="T">The type of elements in the array.</typeparam>
26+
/// <param name="array">Reference to the array from which the value will be removed.</param>
27+
/// <param name="value">The value to remove from the array.</param>
28+
public static void Remove<T>(ref T[] array, T value)
29+
{
30+
int index = Array.IndexOf(array, value);
31+
var count = array.Length - index;
32+
Buffer.BlockCopy(array, index + 1, array, index, count);
33+
Array.Resize(ref array, array.Length - 1);
34+
}
35+
36+
/// <summary>
37+
/// Adds a value to a list if it doesn't already exist and returns a boolean indicating success.
38+
/// </summary>
39+
/// <typeparam name="T">The type of elements in the list.</typeparam>
40+
/// <param name="list">The list to which the value will be added.</param>
41+
/// <param name="t">The value to add to the list.</param>
42+
/// <returns>Returns true if the value was added (not already in the list), otherwise false.</returns>
43+
public static bool AddUnique<T>(this IList<T> list, T t)
44+
{
45+
if (list.Contains(t))
46+
{
47+
return false;
48+
}
49+
50+
list.Add(t);
51+
52+
return true;
53+
}
54+
55+
/// <summary>
56+
/// Finds the index of a specified item within an unmanaged array.
57+
/// </summary>
58+
/// <typeparam name="T">The type of elements in the array.</typeparam>
59+
/// <param name="ptr">Pointer to the beginning of the array.</param>
60+
/// <param name="item">Pointer to the item to search for.</param>
61+
/// <param name="count">The number of items in the array.</param>
62+
/// <returns>The index of the item if found; otherwise, -1.</returns>
63+
public static int IndexOf<T>(T* ptr, T* item, int count) where T : unmanaged
64+
{
65+
for (int i = 0; i < count; i++)
66+
{
67+
if (&ptr[i] == item)
68+
{
69+
return i;
70+
}
71+
}
72+
return -1;
73+
}
74+
}
75+
}

0 commit comments

Comments
 (0)