From c4138503f9986e9afe032476daf033bd2d95b4d8 Mon Sep 17 00:00:00 2001 From: eihrul Date: Fri, 14 May 2010 02:04:42 +0000 Subject: [PATCH] added no_memory callback that allows overriding the default abort behavior --- ChangeLog | 7 ++++--- callbacks.c | 13 ++++++++----- configure.ac | 2 +- include/enet/callbacks.h | 1 + include/enet/enet.h | 13 ++++++++----- 5 files changed, 22 insertions(+), 14 deletions(-) diff --git a/ChangeLog b/ChangeLog index a84733e..c9219d7 100644 --- a/ChangeLog +++ b/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 diff --git a/callbacks.c b/callbacks.c index 7f960af..250b967 100644 --- a/callbacks.c +++ b/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; } diff --git a/configure.ac b/configure.ac index a923fbc..c55c9fe 100644 --- a/configure.ac +++ b/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]) diff --git a/include/enet/callbacks.h b/include/enet/callbacks.h index 0b9e693..b488ac7 100644 --- a/include/enet/callbacks.h +++ b/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 diff --git a/include/enet/enet.h b/include/enet/enet.h index b139976..2ffcb3f 100644 --- a/include/enet/enet.h +++ b/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