This looks great - one question, though. Is there any straightforward way of detecting when a block of memory is no longer being used by a packet so that I can free up that memory? Getting a 'type' argument to the free() callback would be sufficient, but it might also be nice to just have a specialized 'packetDestroyed' callback that lets the user know that a packet's data is no longer in use.
<br><br>In my specific case, to be able to pass a data pointer to enet, I have to pin the data in memory first because I'm using a garbage-collected VM, so I need to know when the data is no longer in use so that it can be unpinned and GCed.
<br><br>Thanks<br>-k<br><br><div><span class="gmail_quote">On 5/1/06, <b class="gmail_sendername">Lee Salzman</b> <<a href="mailto:lsalzman1@cox.net">lsalzman1@cox.net</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Just because I felt inspired I will eventually add something similar to<br>the following code to the main ENet branch. If you really want, just<br>manually apply these diffs for now, and when I get around to the next<br>release it will be in there. You can consider the interface to it
<br>(ENET_PACKET_FLAG_NO_ALLOCATE) final, so don't worry about using it. It<br>will just use the pointer passed to enet_packet_create() verbatim rather<br>than copying from it.<br><br>Index: packet.c<br>===================================================================
<br>RCS file: /cvsroot/sauerbraten/sauerbraten/src/enet/packet.c,v<br>retrieving revision 1.2<br>diff -u -r1.2 packet.c<br>--- packet.c 24 Mar 2006 21:14:07 -0000 1.2<br>+++ packet.c 2 May 2006 04:13:31 -0000<br>
@@ -21,10 +21,15 @@<br> {<br> ENetPacket * packet = (ENetPacket *) enet_malloc (sizeof<br>(ENetPacket));<br><br>- packet -> data = (enet_uint8 *) enet_malloc (dataLength);<br>+ if(flags & ENET_PACKET_FLAG_NO_ALLOCATE)
<br>+ packet -> data = (enet_uint8 *) data;<br>+ else<br>+ {<br>+ packet -> data = (enet_uint8 *) enet_malloc (dataLength);<br><br>- if (data != NULL)<br>- memcpy (packet -> data, data, dataLength);
<br>+ if (data != NULL)<br>+ memcpy (packet -> data, data, dataLength);<br>+ };<br><br> packet -> referenceCount = 0;<br> packet -> flags = flags;<br>@@ -39,7 +44,8 @@<br> void<br> enet_packet_destroy (ENetPacket * packet)
<br> {<br>- enet_free (packet -> data);<br>+ if((packet -> flags & ENET_PACKET_FLAG_NO_ALLOCATE) == 0)<br>+ enet_free (packet -> data);<br> enet_free (packet);<br> }<br><br>@@ -54,7 +60,7 @@
<br> {<br> enet_uint8 * newData;<br><br>- if (dataLength <= packet -> dataLength)<br>+ if (dataLength <= packet -> dataLength || (packet -> flags &<br>ENET_PACKET_FLAG_NO_ALLOCATE))<br> {
<br> packet -> dataLength = dataLength;<br><br>Index: include/enet/enet.h<br>===================================================================<br>RCS file: /cvsroot/sauerbraten/sauerbraten/src/enet/include/enet/enet.h,v
<br>retrieving revision 1.8<br>diff -u -r1.8 enet.h<br>--- include/enet/enet.h 1 May 2006 19:24:17 -0000 1.8<br>+++ include/enet/enet.h 2 May 2006 04:13:31 -0000<br>@@ -80,7 +80,9 @@<br> /** packet will not be sequenced with other packets
<br> * not supported for reliable packets<br> */<br>- ENET_PACKET_FLAG_UNSEQUENCED = (1 << 1)<br>+ ENET_PACKET_FLAG_UNSEQUENCED = (1 << 1),<br>+ /** packet will not allocate data, and user must supply it instead */
<br>+ ENET_PACKET_FLAG_NO_ALLOCATE = (1 << 2)<br> } ENetPacketFlag;<br><br> /**<br><br>Kevin Gadd wrote:<br>> Perfect. Being able to send the same packets multiple times will be a<br>> godsend - glad that's possible. Probably wouldn't hurt to document that
<br>> somewhere. :)<br>><br>> I noticed the callbacks stuff in the new version of the library but<br>> didn't notice that it was for allocation. Makes sense. Is there any way<br>> to turn the callbacks on and off dynamically so that I can only use them
<br>> when I'm creating a packet from an existing memory buffer?<br>><br>> Thanks!<br>> -kg<br>><br>_______________________________________________<br>ENet-discuss mailing list<br><a href="mailto:ENet-discuss@cubik.org">
ENet-discuss@cubik.org</a><br><a href="http://lists.cubik.org/mailman/listinfo/enet-discuss">http://lists.cubik.org/mailman/listinfo/enet-discuss</a><br></blockquote></div><br>