shithub: choc

Download patch

ref: 3d04d0264f490823896a37071300f8e2d2526a1b
parent: ab6ecc6404515d6c400b6dc4c3a185df5aca8344
author: Simon Howard <[email protected]>
date: Thu Feb 23 18:41:13 EST 2006

Track memory used by packet data to help detect memory leaks

Subversion-branch: /trunk/chocolate-doom
Subversion-revision: 385

--- a/src/net_packet.c
+++ b/src/net_packet.c
@@ -1,7 +1,7 @@
 // Emacs style mode select   -*- C++ -*- 
 //-----------------------------------------------------------------------------
 //
-// $Id: net_packet.c 287 2006-01-13 02:20:12Z fraggle $
+// $Id: net_packet.c 385 2006-02-23 23:41:13Z fraggle $
 //
 // Copyright(C) 2005 Simon Howard
 //
@@ -21,6 +21,9 @@
 // 02111-1307, USA.
 //
 // $Log$
+// Revision 1.4  2006/02/23 23:41:13  fraggle
+// Track memory used by packet data to help detect memory leaks
+//
 // Revision 1.3  2006/01/13 02:20:12  fraggle
 // Signed integer read functions.  Use these when reading ticcmd diffs.
 //
@@ -40,6 +43,8 @@
 #include "net_packet.h"
 #include "z_zone.h"
 
+static int total_packet_memory = 0;
+
 net_packet_t *NET_NewPacket(int initial_size)
 {
     net_packet_t *packet;
@@ -54,6 +59,11 @@
     packet->len = 0;
     packet->pos = 0;
 
+    total_packet_memory += sizeof(net_packet_t) + initial_size;
+
+    //printf("total packet memory: %i bytes\n", total_packet_memory);
+    //printf("%p: allocated\n", packet);
+
     return packet;
 }
 
@@ -72,6 +82,9 @@
 
 void NET_FreePacket(net_packet_t *packet)
 {
+    //printf("%p: destroyed\n", packet);
+    
+    total_packet_memory -= sizeof(net_packet_t) + packet->alloced;
     Z_Free(packet->data);
     Z_Free(packet);
 }
@@ -217,6 +230,8 @@
 static void NET_IncreasePacket(net_packet_t *packet)
 {
     byte *newdata;
+
+    total_packet_memory -= packet->alloced;
    
     packet->alloced *= 2;
 
@@ -226,6 +241,8 @@
 
     Z_Free(packet->data);
     packet->data = newdata;
+
+    total_packet_memory += packet->alloced;
 }
 
 // Write a single byte to the packet