Skip to content

Commit

Permalink
Refactor entity extensions: safety + maintenance
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmad-el-sayed committed Nov 25, 2024
1 parent b26172a commit d1c7f32
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 94 deletions.
2 changes: 1 addition & 1 deletion libs/UGridNET/dll/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ set(
SOURCES
${SWIG_GENERATED_CSHARP_SRCS}
${SOURCE_DIR}/ContactsExtensions.cs
${SOURCE_DIR}/ExtensionsConstants.cs
${SOURCE_DIR}/IntPtrExtensions.cs
${SOURCE_DIR}/IntPtrHelpers.cs
${SOURCE_DIR}/Mesh1DExtensions.cs
${SOURCE_DIR}/Mesh2DExtensions.cs
${SOURCE_DIR}/Network1DExtensions.cs
Expand Down
37 changes: 23 additions & 14 deletions libs/UGridNET/dll/src/ContactsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,33 @@ internal static class ContactsExtensions
{
public static void Allocate(this Contacts contacts)
{
contacts.name = Marshal.AllocHGlobal(UGrid.name_long_length);
contacts.contact_name_id = Marshal.AllocHGlobal(UGrid.name_length * contacts.num_contacts);
contacts.mesh_from_name = Marshal.AllocHGlobal(UGrid.name_long_length);
contacts.mesh_to_name = Marshal.AllocHGlobal(UGrid.name_long_length);
contacts.contact_name_long = Marshal.AllocHGlobal(UGrid.name_long_length * contacts.num_contacts);
contacts.edges = Marshal.AllocHGlobal(contacts.num_contacts * 2 * Constants.intBytes);
contacts.contact_type = Marshal.AllocHGlobal(contacts.num_contacts * Constants.intBytes);
try
{
contacts.name = IntPtrHelpers.Allocate(UGrid.name_long_length);
contacts.contact_name_id = IntPtrHelpers.Allocate(UGrid.name_length * contacts.num_contacts);
contacts.mesh_from_name = IntPtrHelpers.Allocate(UGrid.name_long_length);
contacts.mesh_to_name = IntPtrHelpers.Allocate(UGrid.name_long_length);
contacts.contact_name_long = IntPtrHelpers.Allocate(UGrid.name_long_length * contacts.num_contacts);
contacts.edges = IntPtrHelpers.Allocate(contacts.num_contacts * 2 * IntPtrHelpers.Constants.intBytes);
contacts.contact_type = IntPtrHelpers.Allocate(contacts.num_contacts * IntPtrHelpers.Constants.intBytes);
}
catch
{
// AllocHGlobal may throw OutOfMemoryException exception, clean up and re-throw
contacts.Free();
throw;
}
}

public static void Free(this Contacts contacts)
{
if (contacts.name != IntPtr.Zero) Marshal.FreeHGlobal(contacts.name);
if (contacts.contact_name_id != IntPtr.Zero) Marshal.FreeHGlobal(contacts.contact_name_id);
if (contacts.mesh_from_name != IntPtr.Zero) Marshal.FreeHGlobal(contacts.mesh_from_name);
if (contacts.mesh_to_name != IntPtr.Zero) Marshal.FreeHGlobal(contacts.mesh_to_name);
if (contacts.contact_name_long != IntPtr.Zero) Marshal.FreeHGlobal(contacts.contact_name_long);
if (contacts.edges != IntPtr.Zero) Marshal.FreeHGlobal(contacts.edges);
if (contacts.contact_type != IntPtr.Zero) Marshal.FreeHGlobal(contacts.contact_type);
IntPtrHelpers.Free(contacts.name);
IntPtrHelpers.Free(contacts.contact_name_id);
IntPtrHelpers.Free(contacts.mesh_from_name);
IntPtrHelpers.Free(contacts.mesh_to_name);
IntPtrHelpers.Free(contacts.contact_name_long);
IntPtrHelpers.Free(contacts.edges);
IntPtrHelpers.Free(contacts.contact_type);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,27 @@

namespace UGridNET
{
namespace Extensions
internal static class IntPtrHelpers
{
internal static class Constants
{
public static readonly int intBytes = Marshal.SizeOf<int>();
public static readonly int doubleBytes = Marshal.SizeOf<double>();
public static readonly int byteBytes = Marshal.SizeOf<byte>();
}
public static IntPtr Allocate(int size)
{
return Marshal.AllocHGlobal(size);
}

public static void Free(IntPtr ptr)
{
if (ptr != IntPtr.Zero)
{
Marshal.FreeHGlobal(ptr);
ptr = IntPtr.Zero;
}
}
}

}
53 changes: 31 additions & 22 deletions libs/UGridNET/dll/src/Mesh1DExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,41 @@ internal static class Mesh1DExtensions
{
public static void Allocate(this Mesh1D mesh1D)
{
mesh1D.name = Marshal.AllocHGlobal(UGrid.name_long_length);
mesh1D.node_long_name = Marshal.AllocHGlobal(UGrid.name_long_length * mesh1D.num_nodes);
mesh1D.network_name = Marshal.AllocHGlobal(UGrid.name_long_length);
mesh1D.node_x = Marshal.AllocHGlobal(mesh1D.num_nodes * Constants.doubleBytes);
mesh1D.node_y = Marshal.AllocHGlobal(mesh1D.num_nodes * Constants.doubleBytes);
mesh1D.edge_x = Marshal.AllocHGlobal(mesh1D.num_edges * Constants.doubleBytes);
mesh1D.edge_y = Marshal.AllocHGlobal(mesh1D.num_edges * Constants.doubleBytes);
mesh1D.edge_nodes = Marshal.AllocHGlobal(mesh1D.num_edges * 2 * Constants.intBytes);
mesh1D.edge_edge_id = Marshal.AllocHGlobal(mesh1D.num_nodes * Constants.intBytes); // size?
mesh1D.node_edge_id = Marshal.AllocHGlobal(mesh1D.num_nodes * Constants.intBytes); // size?
mesh1D.node_edge_offset = Marshal.AllocHGlobal(mesh1D.num_nodes * Constants.doubleBytes);
try
{
mesh1D.name = IntPtrHelpers.Allocate(UGrid.name_long_length);
mesh1D.node_long_name = IntPtrHelpers.Allocate(UGrid.name_long_length * mesh1D.num_nodes);
mesh1D.network_name = IntPtrHelpers.Allocate(UGrid.name_long_length);
mesh1D.node_x = IntPtrHelpers.Allocate(mesh1D.num_nodes * IntPtrHelpers.Constants.doubleBytes);
mesh1D.node_y = IntPtrHelpers.Allocate(mesh1D.num_nodes * IntPtrHelpers.Constants.doubleBytes);
mesh1D.edge_x = IntPtrHelpers.Allocate(mesh1D.num_edges * IntPtrHelpers.Constants.doubleBytes);
mesh1D.edge_y = IntPtrHelpers.Allocate(mesh1D.num_edges * IntPtrHelpers.Constants.doubleBytes);
mesh1D.edge_nodes = IntPtrHelpers.Allocate(mesh1D.num_edges * 2 * IntPtrHelpers.Constants.intBytes);
mesh1D.edge_edge_id = IntPtrHelpers.Allocate(mesh1D.num_nodes * IntPtrHelpers.Constants.intBytes); // size?
mesh1D.node_edge_id = IntPtrHelpers.Allocate(mesh1D.num_nodes * IntPtrHelpers.Constants.intBytes); // size?
mesh1D.node_edge_offset = IntPtrHelpers.Allocate(mesh1D.num_nodes * IntPtrHelpers.Constants.doubleBytes);
}
catch
{
// AllocHGlobal may throw OutOfMemoryException exception, clean up and re-throw
mesh1D.Free();
throw;
}
}

public static void Free(this Mesh1D mesh1D)
{
if (mesh1D.name != IntPtr.Zero) Marshal.FreeHGlobal(mesh1D.name);
if (mesh1D.node_long_name != IntPtr.Zero) Marshal.FreeHGlobal(mesh1D.node_long_name);
if (mesh1D.network_name != IntPtr.Zero) Marshal.FreeHGlobal(mesh1D.network_name);
if (mesh1D.node_x != IntPtr.Zero) Marshal.FreeHGlobal(mesh1D.node_x);
if (mesh1D.node_y != IntPtr.Zero) Marshal.FreeHGlobal(mesh1D.node_y);
if (mesh1D.edge_x != IntPtr.Zero) Marshal.FreeHGlobal(mesh1D.edge_x);
if (mesh1D.edge_y != IntPtr.Zero) Marshal.FreeHGlobal(mesh1D.edge_y);
if (mesh1D.edge_nodes != IntPtr.Zero) Marshal.FreeHGlobal(mesh1D.edge_nodes);
if (mesh1D.edge_edge_id != IntPtr.Zero) Marshal.FreeHGlobal(mesh1D.edge_edge_id);
if (mesh1D.node_edge_id != IntPtr.Zero) Marshal.FreeHGlobal(mesh1D.node_edge_id);
if (mesh1D.node_edge_offset != IntPtr.Zero) Marshal.FreeHGlobal(mesh1D.node_edge_offset);
IntPtrHelpers.Free(mesh1D.name);
IntPtrHelpers.Free(mesh1D.node_long_name);
IntPtrHelpers.Free(mesh1D.network_name);
IntPtrHelpers.Free(mesh1D.node_x);
IntPtrHelpers.Free(mesh1D.node_y);
IntPtrHelpers.Free(mesh1D.edge_x);
IntPtrHelpers.Free(mesh1D.edge_y);
IntPtrHelpers.Free(mesh1D.edge_nodes);
IntPtrHelpers.Free(mesh1D.edge_edge_id);
IntPtrHelpers.Free(mesh1D.node_edge_id);
IntPtrHelpers.Free(mesh1D.node_edge_offset);
}
}
}
Expand Down
69 changes: 39 additions & 30 deletions libs/UGridNET/dll/src/Mesh2DExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,49 @@ internal static class Mesh2DExtensions
{
public static void Allocate(this Mesh2D mesh2D)
{
mesh2D.name = Marshal.AllocHGlobal(UGrid.name_long_length);
mesh2D.node_x = Marshal.AllocHGlobal(mesh2D.num_nodes * Constants.doubleBytes);
mesh2D.node_y = Marshal.AllocHGlobal(mesh2D.num_nodes * Constants.doubleBytes);
mesh2D.node_z = Marshal.AllocHGlobal(mesh2D.num_nodes * Constants.doubleBytes);
mesh2D.edge_x = Marshal.AllocHGlobal(mesh2D.num_edges * Constants.doubleBytes);
mesh2D.edge_y = Marshal.AllocHGlobal(mesh2D.num_edges * Constants.doubleBytes);
mesh2D.edge_z = Marshal.AllocHGlobal(mesh2D.num_edges * Constants.doubleBytes);
mesh2D.face_x = Marshal.AllocHGlobal(mesh2D.num_faces * Constants.doubleBytes);
mesh2D.face_y = Marshal.AllocHGlobal(mesh2D.num_faces * Constants.doubleBytes);
mesh2D.face_z = Marshal.AllocHGlobal(mesh2D.num_faces * Constants.doubleBytes);
mesh2D.edge_nodes = Marshal.AllocHGlobal(mesh2D.num_edges * 2 * Constants.intBytes);
mesh2D.edge_faces = Marshal.AllocHGlobal(mesh2D.num_edges * 2 * Constants.intBytes);
mesh2D.face_nodes = Marshal.AllocHGlobal(mesh2D.num_faces * mesh2D.num_face_nodes_max * Constants.intBytes);
mesh2D.face_edges = Marshal.AllocHGlobal(mesh2D.num_faces * mesh2D.num_face_nodes_max * Constants.intBytes);
mesh2D.face_faces = Marshal.AllocHGlobal(mesh2D.num_faces * mesh2D.num_face_nodes_max * Constants.intBytes);
try
{
mesh2D.name = IntPtrHelpers.Allocate(UGrid.name_long_length);
mesh2D.node_x = IntPtrHelpers.Allocate(mesh2D.num_nodes * IntPtrHelpers.Constants.doubleBytes);
mesh2D.node_y = IntPtrHelpers.Allocate(mesh2D.num_nodes * IntPtrHelpers.Constants.doubleBytes);
mesh2D.node_z = IntPtrHelpers.Allocate(mesh2D.num_nodes * IntPtrHelpers.Constants.doubleBytes);
mesh2D.edge_x = IntPtrHelpers.Allocate(mesh2D.num_edges * IntPtrHelpers.Constants.doubleBytes);
mesh2D.edge_y = IntPtrHelpers.Allocate(mesh2D.num_edges * IntPtrHelpers.Constants.doubleBytes);
mesh2D.edge_z = IntPtrHelpers.Allocate(mesh2D.num_edges * IntPtrHelpers.Constants.doubleBytes);
mesh2D.face_x = IntPtrHelpers.Allocate(mesh2D.num_faces * IntPtrHelpers.Constants.doubleBytes);
mesh2D.face_y = IntPtrHelpers.Allocate(mesh2D.num_faces * IntPtrHelpers.Constants.doubleBytes);
mesh2D.face_z = IntPtrHelpers.Allocate(mesh2D.num_faces * IntPtrHelpers.Constants.doubleBytes);
mesh2D.edge_nodes = IntPtrHelpers.Allocate(mesh2D.num_edges * 2 * IntPtrHelpers.Constants.intBytes);
mesh2D.edge_faces = IntPtrHelpers.Allocate(mesh2D.num_edges * 2 * IntPtrHelpers.Constants.intBytes);
mesh2D.face_nodes = IntPtrHelpers.Allocate(mesh2D.num_faces * mesh2D.num_face_nodes_max * IntPtrHelpers.Constants.intBytes);
mesh2D.face_edges = IntPtrHelpers.Allocate(mesh2D.num_faces * mesh2D.num_face_nodes_max * IntPtrHelpers.Constants.intBytes);
mesh2D.face_faces = IntPtrHelpers.Allocate(mesh2D.num_faces * mesh2D.num_face_nodes_max * IntPtrHelpers.Constants.intBytes);
}
catch
{
// AllocHGlobal may throw OutOfMemoryException exception, clean up and re-throw
mesh2D.Free();
throw;
}
}

public static void Free(this Mesh2D mesh2D)
{
if (mesh2D.name != IntPtr.Zero) Marshal.FreeHGlobal(mesh2D.name);
if (mesh2D.node_x != IntPtr.Zero) Marshal.FreeHGlobal(mesh2D.node_x);
if (mesh2D.node_y != IntPtr.Zero) Marshal.FreeHGlobal(mesh2D.node_y);
if (mesh2D.node_z != IntPtr.Zero) Marshal.FreeHGlobal(mesh2D.node_z);
if (mesh2D.edge_x != IntPtr.Zero) Marshal.FreeHGlobal(mesh2D.edge_x);
if (mesh2D.edge_y != IntPtr.Zero) Marshal.FreeHGlobal(mesh2D.edge_y);
if (mesh2D.edge_z != IntPtr.Zero) Marshal.FreeHGlobal(mesh2D.edge_z);
if (mesh2D.face_x != IntPtr.Zero) Marshal.FreeHGlobal(mesh2D.face_x);
if (mesh2D.face_y != IntPtr.Zero) Marshal.FreeHGlobal(mesh2D.face_y);
if (mesh2D.face_z != IntPtr.Zero) Marshal.FreeHGlobal(mesh2D.face_z);
if (mesh2D.edge_nodes != IntPtr.Zero) Marshal.FreeHGlobal(mesh2D.edge_nodes);
if (mesh2D.edge_faces != IntPtr.Zero) Marshal.FreeHGlobal(mesh2D.edge_faces);
if (mesh2D.face_nodes != IntPtr.Zero) Marshal.FreeHGlobal(mesh2D.face_nodes);
if (mesh2D.face_edges != IntPtr.Zero) Marshal.FreeHGlobal(mesh2D.face_edges);
if (mesh2D.face_faces != IntPtr.Zero) Marshal.FreeHGlobal(mesh2D.face_faces);
IntPtrHelpers.Free(mesh2D.name);
IntPtrHelpers.Free(mesh2D.node_x);
IntPtrHelpers.Free(mesh2D.node_y);
IntPtrHelpers.Free(mesh2D.node_z);
IntPtrHelpers.Free(mesh2D.edge_x);
IntPtrHelpers.Free(mesh2D.edge_y);
IntPtrHelpers.Free(mesh2D.edge_z);
IntPtrHelpers.Free(mesh2D.face_x);
IntPtrHelpers.Free(mesh2D.face_y);
IntPtrHelpers.Free(mesh2D.face_z);
IntPtrHelpers.Free(mesh2D.edge_nodes);
IntPtrHelpers.Free(mesh2D.edge_faces);
IntPtrHelpers.Free(mesh2D.face_nodes);
IntPtrHelpers.Free(mesh2D.face_edges);
IntPtrHelpers.Free(mesh2D.face_faces);
}
}
}
Expand Down
Loading

0 comments on commit d1c7f32

Please sign in to comment.