[ENet-discuss] [near-PATCH] speed optimisation and bugfix: do one memory allocat ion per packet instead of two

Benoit Germain bgermain at ubisoft.fr
Thu Dec 9 05:16:21 PST 2004


Hello,

Sorry, I don't have a patch utility handy to do this cleanly, but the
changes are pretty localized. Also, I have made other changes that are not
submitted here that would spoil the patch anyway.

BTW, this also fixes a bug: packet->data was allocated with enet_malloc(),
but freed with free(). Well, now, packet->data no longer exists :-)

Apart from that, there is no functionnal change.

Enet.h:

typedef struct _ENetPacket
{
   size_t               referenceCount;  /**< internal use only */
   enet_uint32          flags;           /**< bitwise or of ENetPacketFlag
constants */
-  enet_uint8 *         data;            /**< allocated data for packet */
   size_t               dataLength;      /**< length of data */
} ENetPacket;
+ #define packet_data(_packet) ((enet_uint8 *)(((ENetPacket *)(_packet))+1))


- ENET_API int          enet_packet_resize  (ENetPacket *packet, size_t
dataLength );
+ ENET_API ENetPacket * enet_packet_resize  (ENetPacket *packet, size_t
dataLength );

Protocol.c:
Replace the 3 occurrences of <some packet> -> data with packet_data(<some
packet>)

Packet.c:
The easiest thing to do is to replace everything with:

################################ BEGIN ##############################

/** 
 @file  packet.c
 @brief ENet packet management functions
*/
#include <string.h>
#define ENET_BUILDING_LIB 1
#include "enet/enet.h"

/** @defgroup Packet ENet packet functions 
    @{ 
*/

/** Creates a packet that may be sent to a peer.
    @param dataContents initial contents of the packet's data; the packet's
data will remain uninitialized if dataContents is NULL.
    @param dataLength   size of the data allocated for this packet
    @param flags        flags for this packet as described for the
ENetPacket structure.
    @returns the packet on success, NULL on failure
*/
ENetPacket *
enet_packet_create (const void * data, size_t dataLength, enet_uint32 flags)
{
    ENetPacket * packet = (ENetPacket *) enet_malloc (sizeof (ENetPacket) +
dataLength);
    if ( packet != NULL )
    {
      memcpy (packet_data(packet), data, dataLength);

      packet -> referenceCount = 0;
      packet -> flags = flags;
      packet -> dataLength = dataLength;
    }
    return packet;
}

/** Destroys the packet and deallocates its data.
    @param packet packet to be destroyed
*/
void
enet_packet_destroy (ENetPacket * packet)
{
    enet_free (packet);
}

/** Attempts to resize the data in the packet to length specified in the 
    dataLength parameter 
    @param packet packet to resize
    @param dataLength new size for the packet data
    @returns the new packet address on success, NULL on failure (length is
unchanged)
*/
ENetPacket *
enet_packet_resize (ENetPacket * packet, size_t dataLength)
{
    ENetPacket * newPacket ;
    /* reducing length is faster done by leaving the block unchanged... */
    if (dataLength <= packet -> dataLength)
    {
       packet -> dataLength = dataLength;

       return packet;
    }

    newPacket = (ENetPacket *) enet_malloc (sizeof (ENetPacket) +
dataLength);
    if ( newPacket != NULL )
    {
      memcpy (newPacket, packet, sizeof (ENetPacket) + packet ->
dataLength);
      enet_free (packet);

      newPacket -> dataLength = dataLength;
    }
    return newPacket;
}

/** @} */

################################ END ##############################
__________________________________
Benoit Germain
mailto:bgermain at ubisoft.fr <mailto:bgermain at ubisoft.fr> 

Person who says it cannot be done should not interrupt person doing it.
-- Chinese Proverb

People who think they're smart annoy those of us who are.
-- double-U's Proverb ?
__________________________________

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.puremagic.com/pipermail/enet-discuss/attachments/20041209/51b698d8/attachment.html


More information about the ENet-discuss mailing list