Browse Source

Manually merge in PR #11, I broke it mid-PR by pulling 'next' into 'master'. Doh!

Matt Coburn 5 years ago
parent
commit
e8acb84a6a
3 changed files with 39 additions and 116 deletions
  1. 16 54
      Source/Managed/ENet.cs
  2. 5 62
      Source/Native/enet.h
  3. 18 0
      Source/Native/enet_malloc.h

+ 16 - 54
Source/Managed/ENet.cs

@@ -79,18 +79,6 @@ namespace ENet {
 		public IntPtr packet;
 		public IntPtr packet;
 	}
 	}
 
 
-	[StructLayout(LayoutKind.Sequential)]
-	internal struct ENetCallbacks {
-		public AllocCallback malloc;
-		public FreeCallback free;
-		public NoMemoryCallback noMemory;
-	}
-
-	public delegate IntPtr AllocCallback(IntPtr size);
-	public delegate void FreeCallback(IntPtr memory);
-	public delegate void NoMemoryCallback();
-	public delegate void PacketFreeCallback(Packet packet);
-
 	internal static class ArrayPool {
 	internal static class ArrayPool {
 		[ThreadStatic]
 		[ThreadStatic]
 		private static byte[] byteBuffer;
 		private static byte[] byteBuffer;
@@ -220,26 +208,6 @@ namespace ENet {
 		}
 		}
 	}
 	}
 
 
-	public class Callbacks {
-		private ENetCallbacks nativeCallbacks;
-
-		internal ENetCallbacks NativeData {
-			get {
-				return nativeCallbacks;
-			}
-
-			set {
-				nativeCallbacks = value;
-			}
-		}
-
-		public Callbacks(AllocCallback allocCallback, FreeCallback freeCallback, NoMemoryCallback noMemoryCallback) {
-			nativeCallbacks.malloc = allocCallback;
-			nativeCallbacks.free = freeCallback;
-			nativeCallbacks.noMemory = noMemoryCallback;
-		}
-	}
-
 	public struct Packet : IDisposable {
 	public struct Packet : IDisposable {
 		private IntPtr nativePacket;
 		private IntPtr nativePacket;
 
 
@@ -313,18 +281,6 @@ namespace ENet {
 				throw new InvalidOperationException("Packet not created");
 				throw new InvalidOperationException("Packet not created");
 		}
 		}
 
 
-		public void SetFreeCallback(IntPtr callback) {
-			IsCreated();
-
-			Native.enet_packet_set_free_callback(nativePacket, callback);
-		}
-
-		public void SetFreeCallback(PacketFreeCallback callback) {
-			IsCreated();
-
-			Native.enet_packet_set_free_callback(nativePacket, Marshal.GetFunctionPointerForDelegate(callback));
-		}
-
 		public void Create(byte[] data) {
 		public void Create(byte[] data) {
 			if (data == null)
 			if (data == null)
 				throw new ArgumentNullException("data");
 				throw new ArgumentNullException("data");
@@ -924,14 +880,20 @@ namespace ENet {
 			return Native.enet_initialize() == 0;
 			return Native.enet_initialize() == 0;
 		}
 		}
 
 
-		public static bool Initialize(Callbacks callbacks) {
-			ENetCallbacks nativeCallbacks = callbacks.NativeData;
+		public static void Deinitialize() {
+			Native.enet_deinitialize();
+		}
 
 
-			return Native.enet_initialize_with_callbacks(version, ref nativeCallbacks) == 0;
+		// Supports for mimalloc memory allocator.
+		public static IntPtr Malloc(int size) => Malloc((ulong)size);
+		public static IntPtr Malloc(ulong size)
+		{
+			return Native.enet_mem_acquire(size);
 		}
 		}
 
 
-		public static void Deinitialize() {
-			Native.enet_deinitialize();
+		public static void Free(IntPtr alloc)
+		{
+			Native.enet_mem_release(alloc);
 		}
 		}
 
 
 		public static uint Time {
 		public static uint Time {
@@ -952,13 +914,16 @@ namespace ENet {
 #else
 #else
         // Assume everything else, Windows et al...		
         // Assume everything else, Windows et al...		
         private const string nativeLibrary = "enet";
         private const string nativeLibrary = "enet";
-#endif	
+#endif
 
 
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
 		internal static extern int enet_initialize();
 		internal static extern int enet_initialize();
 
 
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern int enet_initialize_with_callbacks(uint version, ref ENetCallbacks inits);
+		internal static extern IntPtr enet_mem_acquire(ulong sz);
+
+		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
+		internal static extern void enet_mem_release(IntPtr alloc);
 
 
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
 		internal static extern void enet_deinitialize();
 		internal static extern void enet_deinitialize();
@@ -1005,9 +970,6 @@ namespace ENet {
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
 		internal static extern int enet_packet_get_length(IntPtr packet);
 		internal static extern int enet_packet_get_length(IntPtr packet);
 
 
-		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern void enet_packet_set_free_callback(IntPtr packet, IntPtr callback);
-
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
 		internal static extern void enet_packet_dispose(IntPtr packet);
 		internal static extern void enet_packet_dispose(IntPtr packet);
 
 

+ 5 - 62
Source/Native/enet.h

@@ -34,8 +34,9 @@
 #include <stdint.h>
 #include <stdint.h>
 #include <time.h>
 #include <time.h>
 
 
- // include the ENET Logger code.
+ // include custom data.
 #include "custom/enet_logging.h"
 #include "custom/enet_logging.h"
+#include "enet_malloc.h"
 
 
 #define ENET_VERSION_MAJOR 2
 #define ENET_VERSION_MAJOR 2
 #define ENET_VERSION_MINOR 4
 #define ENET_VERSION_MINOR 4
@@ -677,7 +678,9 @@ extern "C" {
 	*/
 	*/
 
 
 	ENET_API int enet_initialize(void);
 	ENET_API int enet_initialize(void);
-	ENET_API int enet_initialize_with_callbacks(ENetVersion, const ENetCallbacks*);
+	ENET_API void* enet_mem_acquire(size_t sz) { return enet_malloc(sz); }
+	ENET_API void enet_mem_release(void* alloc) { enet_free(alloc); }
+
 	ENET_API void enet_deinitialize(void);
 	ENET_API void enet_deinitialize(void);
 	ENET_API ENetVersion enet_linked_version(void);
 	ENET_API ENetVersion enet_linked_version(void);
 	ENET_API int enet_array_is_zeroed(const uint8_t*, int);
 	ENET_API int enet_array_is_zeroed(const uint8_t*, int);
@@ -990,57 +993,7 @@ problem. */
 
 
 /*
 /*
 =======================================================================
 =======================================================================
-	Callbacks
-=======================================================================
-*/
-
-static ENetCallbacks callbacks = {
-	malloc,
-	free,
-	abort
-};
-
-int enet_initialize_with_callbacks(ENetVersion version, const ENetCallbacks* inits) {
-	if (version < ENET_VERSION_CREATE(1, 3, 0)) {
-		ENET_LOG_ERROR("ENET version is too old");
-		return -1;
-	}
-
-	if (inits->malloc != NULL || inits->free != NULL) {
-		if (inits->malloc == NULL || inits->free == NULL) {
-			ENET_LOG_ERROR("Memory allocator/free mechanism is NULL");
-			return -1;
-		}
-
-
-		callbacks.malloc = inits->malloc;
-		callbacks.free = inits->free;
-	}
-
-	if (inits->noMemory != NULL)
-		callbacks.noMemory = inits->noMemory;
-
-	return enet_initialize();
-}
-
-void* enet_malloc(size_t size) {
-	void* memory = callbacks.malloc(size);
-
-	if (memory == NULL)
-		callbacks.noMemory();
-
-	return memory;
-}
-
-void enet_free(void* memory) {
-	callbacks.free(memory);
-}
-
-/*
-=======================================================================
-
 	List
 	List
-
 =======================================================================
 =======================================================================
 */
 */
 
 
@@ -1092,9 +1045,7 @@ size_t enet_list_size(ENetList* list) {
 
 
 /*
 /*
 =======================================================================
 =======================================================================
-
 	Utilities
 	Utilities
-
 =======================================================================
 =======================================================================
 */
 */
 
 
@@ -1115,9 +1066,7 @@ int enet_array_is_zeroed(const uint8_t* array, int length) {
 
 
 /*
 /*
 =======================================================================
 =======================================================================
-
 	Time
 	Time
-
 =======================================================================
 =======================================================================
 */
 */
 
 
@@ -1236,9 +1185,7 @@ uint32_t enet_time_get(void) {
 
 
 /*
 /*
 =======================================================================
 =======================================================================
-
 	Checksum
 	Checksum
-
 =======================================================================
 =======================================================================
 */
 */
 
 
@@ -1300,9 +1247,7 @@ uint32_t enet_crc32(const ENetBuffer* buffers, size_t bufferCount) {
 
 
 /*
 /*
 =======================================================================
 =======================================================================
-
 	Packet
 	Packet
-
 =======================================================================
 =======================================================================
 */
 */
 
 
@@ -1382,9 +1327,7 @@ void enet_packet_destroy(ENetPacket* packet) {
 
 
 /*
 /*
 =======================================================================
 =======================================================================
-
 	Protocol
 	Protocol
-
 =======================================================================
 =======================================================================
 */
 */
 
 

+ 18 - 0
Source/Native/enet_malloc.h

@@ -0,0 +1,18 @@
+#ifndef ENET_ALLOC_H
+#define ENET_ALLOC_H
+	#include <stdlib.h>
+
+#ifndef ENET_MIMALLOC
+static const char* enet_malloc_name_str = "malloc";
+static inline void* enet_malloc(size_t size_in_bytes) { return malloc(size_in_bytes); }
+static inline void enet_free(void* alloc) { free(alloc); }
+#else
+	#include "mimalloc.h"
+static const char* enet_malloc_name_str = "mi_malloc";
+static inline void* enet_malloc(size_t size_in_bytes) { return mi_malloc(size_in_bytes); }
+static inline void enet_free(void* alloc) { mi_free(alloc); }
+#endif
+
+static inline const char* enet_malloc_name() { return enet_malloc_name_str; }
+
+#endif