Browse Source

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

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

+ 54 - 16
Source/Managed/ENet.cs

@@ -79,6 +79,18 @@ namespace ENet {
 		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 {
 		[ThreadStatic]
 		private static byte[] byteBuffer;
@@ -208,6 +220,26 @@ 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 {
 		private IntPtr nativePacket;
 
@@ -281,6 +313,18 @@ namespace ENet {
 				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) {
 			if (data == null)
 				throw new ArgumentNullException("data");
@@ -880,20 +924,14 @@ namespace ENet {
 			return Native.enet_initialize() == 0;
 		}
 
-		public static void Deinitialize() {
-			Native.enet_deinitialize();
-		}
+		public static bool Initialize(Callbacks callbacks) {
+			ENetCallbacks nativeCallbacks = callbacks.NativeData;
 
-		// 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);
+			return Native.enet_initialize_with_callbacks(version, ref nativeCallbacks) == 0;
 		}
 
-		public static void Free(IntPtr alloc)
-		{
-			Native.enet_mem_release(alloc);
+		public static void Deinitialize() {
+			Native.enet_deinitialize();
 		}
 
 		public static uint Time {
@@ -914,16 +952,13 @@ namespace ENet {
 #else
         // Assume everything else, Windows et al...		
         private const string nativeLibrary = "enet";
-#endif
+#endif	
 
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
 		internal static extern int enet_initialize();
 
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern IntPtr enet_mem_acquire(ulong sz);
-
-		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern void enet_mem_release(IntPtr alloc);
+		internal static extern int enet_initialize_with_callbacks(uint version, ref ENetCallbacks inits);
 
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
 		internal static extern void enet_deinitialize();
@@ -970,6 +1005,9 @@ namespace ENet {
 		[DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
 		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)]
 		internal static extern void enet_packet_dispose(IntPtr packet);
 

+ 62 - 5
Source/Native/enet.h

@@ -34,9 +34,8 @@
 #include <stdint.h>
 #include <time.h>
 
- // include custom data.
+ // include the ENET Logger code.
 #include "custom/enet_logging.h"
-#include "enet_malloc.h"
 
 #define ENET_VERSION_MAJOR 2
 #define ENET_VERSION_MINOR 4
@@ -678,9 +677,7 @@ extern "C" {
 	*/
 
 	ENET_API int enet_initialize(void);
-	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 int enet_initialize_with_callbacks(ENetVersion, const ENetCallbacks*);
 	ENET_API void enet_deinitialize(void);
 	ENET_API ENetVersion enet_linked_version(void);
 	ENET_API int enet_array_is_zeroed(const uint8_t*, int);
@@ -993,7 +990,57 @@ 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
+
 =======================================================================
 */
 
@@ -1045,7 +1092,9 @@ size_t enet_list_size(ENetList* list) {
 
 /*
 =======================================================================
+
 	Utilities
+
 =======================================================================
 */
 
@@ -1066,7 +1115,9 @@ int enet_array_is_zeroed(const uint8_t* array, int length) {
 
 /*
 =======================================================================
+
 	Time
+
 =======================================================================
 */
 
@@ -1185,7 +1236,9 @@ uint32_t enet_time_get(void) {
 
 /*
 =======================================================================
+
 	Checksum
+
 =======================================================================
 */
 
@@ -1247,7 +1300,9 @@ uint32_t enet_crc32(const ENetBuffer* buffers, size_t bufferCount) {
 
 /*
 =======================================================================
+
 	Packet
+
 =======================================================================
 */
 
@@ -1327,7 +1382,9 @@ void enet_packet_destroy(ENetPacket* packet) {
 
 /*
 =======================================================================
+
 	Protocol
+
 =======================================================================
 */
 

+ 0 - 18
Source/Native/enet_malloc.h

@@ -1,18 +0,0 @@
-#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