diff options
Diffstat (limited to 'packet.c')
-rw-r--r-- | packet.c | 15 |
1 files changed, 13 insertions, 2 deletions
@@ -21,11 +21,16 @@ enet_packet_create (const void * data, size_t dataLength, enet_uint32 flags) { ENetPacket * packet = (ENetPacket *) enet_malloc (sizeof (ENetPacket)); - if(flags & ENET_PACKET_FLAG_NO_ALLOCATE) + if (flags & ENET_PACKET_FLAG_NO_ALLOCATE) packet -> data = (enet_uint8 *) data; else { packet -> data = (enet_uint8 *) enet_malloc (dataLength); + if (packet -> data == NULL) + { + enet_free (packet); + return NULL; + }; if (data != NULL) memcpy (packet -> data, data, dataLength); @@ -34,6 +39,7 @@ enet_packet_create (const void * data, size_t dataLength, enet_uint32 flags) packet -> referenceCount = 0; packet -> flags = flags; packet -> dataLength = dataLength; + packet -> freeCallback = NULL; return packet; } @@ -44,7 +50,9 @@ enet_packet_create (const void * data, size_t dataLength, enet_uint32 flags) void enet_packet_destroy (ENetPacket * packet) { - if((packet -> flags & ENET_PACKET_FLAG_NO_ALLOCATE) == 0) + if (packet -> freeCallback != NULL) + (* packet -> freeCallback) (packet); + if (! (packet -> flags & ENET_PACKET_FLAG_NO_ALLOCATE)) enet_free (packet -> data); enet_free (packet); } @@ -68,6 +76,9 @@ enet_packet_resize (ENetPacket * packet, size_t dataLength) } newData = (enet_uint8 *) enet_malloc (dataLength); + if (newData == NULL) + return -1; + memcpy (newData, packet -> data, packet -> dataLength); enet_free (packet -> data); |