added no_memory callback that allows overriding the default abort behavior

This commit is contained in:
eihrul 2010-05-14 02:04:42 +00:00
parent 3ddbfb202d
commit c4138503f9
5 changed files with 22 additions and 14 deletions

View file

@ -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 * now uses dispatch queues for event dispatch rather than potentially
unscalable array walking unscalable array walking
* fixed propagation of memory failures so that an abort is no longer * added no_memory callback that is called when a malloc attempt fails,
required if malloc 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 * now uses packed attribute for protocol structures on platforms with
strange alignment rules strange alignment rules

View file

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

View file

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

View file

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

View file

@ -23,10 +23,13 @@ extern "C"
#include "enet/list.h" #include "enet/list.h"
#include "enet/callbacks.h" #include "enet/callbacks.h"
typedef enum _ENetVersion #define ENET_VERSION_MAJOR 1
{ #define ENET_VERSION_MINOR 2
ENET_VERSION = 1 #define ENET_VERSION_PATCH 2
} ENetVersion; #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 typedef enum _ENetSocketType
{ {
@ -376,7 +379,7 @@ typedef struct _ENetEvent
ENET_API int enet_initialize (void); 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 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 @param inits user-overriden callbacks where any NULL callbacks will use ENet's defaults