Replace two expensive macros with a much simpler mechanism
authorsahlberg <sahlberg@f5534014-38df-0310-8fa8-9805f1628bb7>
Wed, 3 Dec 2003 10:14:34 +0000 (10:14 +0000)
committersahlberg <sahlberg@f5534014-38df-0310-8fa8-9805f1628bb7>
Wed, 3 Dec 2003 10:14:34 +0000 (10:14 +0000)
to prevent memory leaks due to exceptions.

makes my tethereal testcase ~1% faster.

git-svn-id: http://anonsvn.wireshark.org/wireshark/trunk@9153 f5534014-38df-0310-8fa8-9805f1628bb7

epan/tvbuff.c

index b1bcd31c149e0757528229679f8999ad929d1ca2..35846a26c94364b24316a5eca63882e18aa7c366 100644 (file)
@@ -9,7 +9,7 @@
  *             the data of a backing tvbuff, or can be a composite of
  *             other tvbuffs.
  *
- * $Id: tvbuff.c,v 1.53 2003/12/03 09:50:40 sahlberg Exp $
+ * $Id: tvbuff.c,v 1.54 2003/12/03 10:14:34 sahlberg Exp $
  *
  * Copyright (c) 2000 by Gilbert Ramirez <gram@alumni.rice.edu>
  *
@@ -126,15 +126,6 @@ tvb_new(tvbuff_type type)
        return tvb;
 }
 
-/* We accept a void* instead of a field_info* to satisfy CLEANUP_POP */
-static void
-tvb_free_void(void *tvb)
-{
-       tvb_free((tvbuff_t*)tvb);
-}
-
-
-
 void
 tvb_free(tvbuff_t* tvb)
 {
@@ -268,11 +259,18 @@ tvb_set_real_data(tvbuff_t* tvb, const guint8* data, guint length, gint reported
 tvbuff_t*
 tvb_new_real_data(const guint8* data, guint length, gint reported_length)
 {
+       static tvbuff_t *last_tvb=NULL;
        tvbuff_t        *tvb;
 
        tvb = tvb_new(TVBUFF_REAL_DATA);
 
-       CLEANUP_PUSH(tvb_free_void, tvb);
+       if(last_tvb){
+               tvb_free(last_tvb);
+       }
+       /* remember this tvb in case we throw an exception and
+        * lose the pointer to it.
+        */
+       last_tvb=tvb;
 
        tvb_set_real_data(tvb, data, length, reported_length);
 
@@ -282,7 +280,8 @@ tvb_new_real_data(const guint8* data, guint length, gint reported_length)
         */
        tvb->ds_tvb = tvb;
 
-       CLEANUP_POP;
+       /* ok no exception so we dont need to remember it any longer */
+       last_tvb=NULL;
 
        return tvb;
 }
@@ -470,11 +469,18 @@ tvb_set_subset(tvbuff_t *tvb, tvbuff_t *backing,
 tvbuff_t*
 tvb_new_subset(tvbuff_t *backing, gint backing_offset, gint backing_length, gint reported_length)
 {
+       static tvbuff_t *last_tvb=NULL;
        tvbuff_t        *tvb;
 
        tvb = tvb_new(TVBUFF_SUBSET);
 
-       CLEANUP_PUSH(tvb_free_void, tvb);
+       if(last_tvb){
+               tvb_free(last_tvb);
+       }
+       /* remember this tvb in case we throw an exception and
+        * lose the pointer to it.
+        */
+       last_tvb=tvb;
 
        tvb_set_subset(tvb, backing, backing_offset, backing_length, reported_length);
 
@@ -484,7 +490,8 @@ tvb_new_subset(tvbuff_t *backing, gint backing_offset, gint backing_length, gint
         */
        tvb->ds_tvb = backing->ds_tvb;
 
-       CLEANUP_POP;
+       /* ok no exception so we dont need to remember it any longer */
+       last_tvb=NULL;
 
        return tvb;
 }