Browse Source

added no_memory callback that allows overriding the default abort behavior

eihrul 15 years ago
parent
commit
c4138503f9
5 changed files with 22 additions and 14 deletions
  1. 4 3
      ChangeLog
  2. 8 5
      callbacks.c
  3. 1 1
      configure.ac
  4. 1 0
      include/enet/callbacks.h
  5. 8 5
      include/enet/enet.h

+ 4 - 3
ChangeLog

@@ -1,9 +1,10 @@
-ENet CVS (May 13, 2010):
+ENet 1.2.2 (May 13, 2010):
 
 * now uses dispatch queues for event dispatch rather than potentially
 unscalable array walking
-* fixed propagation of memory failures so that an abort is no longer 
-required if malloc fails
+* added no_memory callback that is called when a malloc attempt fails,
+such that if no_memory returns rather than aborts (the default behavior),
+then the error is propagated to the return value of the API calls
 * now uses packed attribute for protocol structures on platforms with 
 strange alignment rules
 

+ 8 - 5
callbacks.c

@@ -5,14 +5,11 @@
 #define ENET_BUILDING_LIB 1
 #include "enet/enet.h"
 
-static ENetCallbacks callbacks = { malloc, free, rand };
+static ENetCallbacks callbacks = { malloc, free, rand, abort };
 
 int
 enet_initialize_with_callbacks (ENetVersion version, const ENetCallbacks * inits)
 {
-   if (version != ENET_VERSION)
-     return -1;
-
    if (inits -> malloc != NULL || inits -> free != NULL)
    {
       if (inits -> malloc == NULL || inits -> free == NULL)
@@ -25,6 +22,12 @@ enet_initialize_with_callbacks (ENetVersion version, const ENetCallbacks * inits
    if (inits -> rand != NULL)
      callbacks.rand = inits -> rand;
 
+   if (version >= ENET_VERSION_CREATE(1, 2, 2))
+   {
+      if (inits -> no_memory != NULL)
+        callbacks.no_memory = inits -> no_memory;
+   }
+
    return enet_initialize ();
 }
            
@@ -34,7 +37,7 @@ enet_malloc (size_t size)
    void * memory = callbacks.malloc (size);
 
    if (memory == NULL)
-     abort ();
+     callbacks.no_memory ();
 
    return memory;
 }

+ 1 - 1
configure.ac

@@ -1,4 +1,4 @@
-AC_INIT([libenet], [5-13-2010])
+AC_INIT([libenet], [1.2.2])
 AC_CONFIG_SRCDIR([include/enet/enet.h])
 AM_INIT_AUTOMAKE([foreign])
 

+ 1 - 0
include/enet/callbacks.h

@@ -12,6 +12,7 @@ typedef struct _ENetCallbacks
     void * (ENET_CALLBACK * malloc) (size_t size);
     void (ENET_CALLBACK * free) (void * memory);
     int (ENET_CALLBACK * rand) (void);
+    void (ENET_CALLBACK * no_memory) (void);
 } ENetCallbacks;
 
 /** @defgroup callbacks ENet internal callbacks

+ 8 - 5
include/enet/enet.h

@@ -23,10 +23,13 @@ extern "C"
 #include "enet/list.h"
 #include "enet/callbacks.h"
 
-typedef enum _ENetVersion
-{
-   ENET_VERSION = 1
-} ENetVersion;
+#define ENET_VERSION_MAJOR 1
+#define ENET_VERSION_MINOR 2
+#define ENET_VERSION_PATCH 2
+#define ENET_VERSION_CREATE(major, minor, patch) (((major)<<16) | ((minor)<<8) | (patch))
+#define ENET_VERSION ENET_VERSION_CREATE(ENET_VERSION_MAJOR, ENET_VERSION_MINOR, ENET_VERSION_PATCH)
+
+typedef enet_uint32 ENetVersion;
 
 typedef enum _ENetSocketType
 {
@@ -376,7 +379,7 @@ typedef struct _ENetEvent
 ENET_API int enet_initialize (void);
 
 /** 
-  Initializes ENet globally and supplies user-overridden callbacks. Must be called prior to using any functions in ENet. Do not use enet_initialize() if you use this variant.
+  Initializes ENet globally and supplies user-overridden callbacks. Must be called prior to using any functions in ENet. Do not use enet_initialize() if you use this variant. Make sure the ENetCallbacks structure is zeroed out so that any additional callbacks added in future versions will be properly ignored.
 
   @param version the constant ENET_VERSION should be supplied so ENet knows which version of ENetCallbacks struct to use
   @param inits user-overriden callbacks where any NULL callbacks will use ENet's defaults