Add beginning of seasonal allocation in addition to the existing ephemeral ones.
authorsahlberg <sahlberg@f5534014-38df-0310-8fa8-9805f1628bb7>
Fri, 12 Aug 2005 08:51:08 +0000 (08:51 +0000)
committersahlberg <sahlberg@f5534014-38df-0310-8fa8-9805f1628bb7>
Fri, 12 Aug 2005 08:51:08 +0000 (08:51 +0000)
git-svn-id: http://anonsvn.wireshark.org/wireshark/trunk@15301 f5534014-38df-0310-8fa8-9805f1628bb7

epan/emem.c
epan/emem.h
epan/packet.c
gtk/main.c
tethereal.c

index 3c4b05c768609d7bf8c581f322cc2a44fc062ea8..ad516350eb74adbd9da7faa3beb75bd33bbea495 100644 (file)
@@ -49,7 +49,8 @@ typedef struct _emem_header_t {
   emem_chunk_t *used_list;
 } emem_header_t;
 
-static emem_header_t emem_packet_mem;
+static emem_header_t ep_packet_mem;
+static emem_header_t se_packet_mem;
 
 /* Initialize the packet-lifetime memory allocation pool.
  * This function should be called only once when Etehreal or Tethereal starts
@@ -58,8 +59,18 @@ static emem_header_t emem_packet_mem;
 void
 ep_init_chunk(void)
 {
-       emem_packet_mem.free_list=NULL; 
-       emem_packet_mem.used_list=NULL; 
+       ep_packet_mem.free_list=NULL;   
+       ep_packet_mem.used_list=NULL;   
+}
+/* Initialize the capture-lifetime memory allocation pool.
+ * This function should be called only once when Etehreal or Tethereal starts
+ * up.
+ */
+void
+se_init_chunk(void)
+{
+       se_packet_mem.free_list=NULL;   
+       se_packet_mem.used_list=NULL;   
 }
 
 /* allocate 'size' amount of memory with an allocation lifetime until the
@@ -79,47 +90,105 @@ ep_alloc(size_t size)
        DISSECTOR_ASSERT(size<(EMEM_PACKET_CHUNK_SIZE>>2));
 
        /* we dont have any free data, so we must allocate a new one */
-       if(!emem_packet_mem.free_list){
+       if(!ep_packet_mem.free_list){
+               emem_chunk_t *npc;
+               npc=g_malloc(sizeof(emem_chunk_t));
+               npc->next=NULL;
+               npc->amount_free=EMEM_PACKET_CHUNK_SIZE;
+               npc->free_offset=0;
+               npc->buf=g_malloc(EMEM_PACKET_CHUNK_SIZE);
+               ep_packet_mem.free_list=npc;
+       }
+
+       /* oops, we need to allocate more memory to serve this request
+         * than we have free. move this node to the used list and try again
+        */
+       if(size>ep_packet_mem.free_list->amount_free){
+               emem_chunk_t *npc;
+               npc=ep_packet_mem.free_list;
+               ep_packet_mem.free_list=ep_packet_mem.free_list->next;
+               npc->next=ep_packet_mem.used_list;
+               ep_packet_mem.used_list=npc;
+       }
+
+       /* we dont have any free data, so we must allocate a new one */
+       if(!ep_packet_mem.free_list){
+               emem_chunk_t *npc;
+               npc=g_malloc(sizeof(emem_chunk_t));
+               npc->next=NULL;
+               npc->amount_free=EMEM_PACKET_CHUNK_SIZE;
+               npc->free_offset=0;
+               npc->buf=g_malloc(EMEM_PACKET_CHUNK_SIZE);
+               ep_packet_mem.free_list=npc;
+       }
+
+
+       buf=ep_packet_mem.free_list->buf+ep_packet_mem.free_list->free_offset;
+
+       ep_packet_mem.free_list->amount_free-=size;
+       ep_packet_mem.free_list->free_offset+=size;
+
+       return buf;
+}
+/* allocate 'size' amount of memory with an allocation lifetime until the
+ * next capture.
+ */
+void *
+se_alloc(size_t size)
+{
+       void *buf;
+
+       /* round up to 8 byte boundary */
+       if(size&0x07){
+               size=(size+7)&0xfffffff8;
+       }
+
+       /* make sure we dont try to allocate too much (arbitrary limit) */
+       DISSECTOR_ASSERT(size<(EMEM_PACKET_CHUNK_SIZE>>2));
+
+       /* we dont have any free data, so we must allocate a new one */
+       if(!se_packet_mem.free_list){
                emem_chunk_t *npc;
                npc=g_malloc(sizeof(emem_chunk_t));
                npc->next=NULL;
                npc->amount_free=EMEM_PACKET_CHUNK_SIZE;
                npc->free_offset=0;
                npc->buf=g_malloc(EMEM_PACKET_CHUNK_SIZE);
-               emem_packet_mem.free_list=npc;
+               se_packet_mem.free_list=npc;
        }
 
        /* oops, we need to allocate more memory to serve this request
          * than we have free. move this node to the used list and try again
         */
-       if(size>emem_packet_mem.free_list->amount_free){
+       if(size>se_packet_mem.free_list->amount_free){
                emem_chunk_t *npc;
-               npc=emem_packet_mem.free_list;
-               emem_packet_mem.free_list=emem_packet_mem.free_list->next;
-               npc->next=emem_packet_mem.used_list;
-               emem_packet_mem.used_list=npc;
+               npc=se_packet_mem.free_list;
+               se_packet_mem.free_list=se_packet_mem.free_list->next;
+               npc->next=se_packet_mem.used_list;
+               se_packet_mem.used_list=npc;
        }
 
        /* we dont have any free data, so we must allocate a new one */
-       if(!emem_packet_mem.free_list){
+       if(!se_packet_mem.free_list){
                emem_chunk_t *npc;
                npc=g_malloc(sizeof(emem_chunk_t));
                npc->next=NULL;
                npc->amount_free=EMEM_PACKET_CHUNK_SIZE;
                npc->free_offset=0;
                npc->buf=g_malloc(EMEM_PACKET_CHUNK_SIZE);
-               emem_packet_mem.free_list=npc;
+               se_packet_mem.free_list=npc;
        }
 
 
-       buf=emem_packet_mem.free_list->buf+emem_packet_mem.free_list->free_offset;
+       buf=se_packet_mem.free_list->buf+se_packet_mem.free_list->free_offset;
 
-       emem_packet_mem.free_list->amount_free-=size;
-       emem_packet_mem.free_list->free_offset+=size;
+       se_packet_mem.free_list->amount_free-=size;
+       se_packet_mem.free_list->free_offset+=size;
 
        return buf;
 }
 
+
 void* ep_alloc0(size_t size) {
        return memset(ep_alloc(size),'\0',size);
 }
@@ -251,15 +320,36 @@ ep_free_all(void)
        emem_chunk_t *npc;
 
        /* move all used chunks ove to the free list */
-       while(emem_packet_mem.used_list){
-               npc=emem_packet_mem.used_list;
-               emem_packet_mem.used_list=emem_packet_mem.used_list->next;
-               npc->next=emem_packet_mem.free_list;
-               emem_packet_mem.free_list=npc;
+       while(ep_packet_mem.used_list){
+               npc=ep_packet_mem.used_list;
+               ep_packet_mem.used_list=ep_packet_mem.used_list->next;
+               npc->next=ep_packet_mem.free_list;
+               ep_packet_mem.free_list=npc;
+       }
+
+       /* clear them all out */
+       for(npc=ep_packet_mem.free_list;npc;npc=npc->next){
+               npc->amount_free=EMEM_PACKET_CHUNK_SIZE;
+               npc->free_offset=0;
+       }
+}
+/* release all allocated memory back to the pool.
+ */
+void
+se_free_all(void)
+{
+       emem_chunk_t *npc;
+
+       /* move all used chunks ove to the free list */
+       while(se_packet_mem.used_list){
+               npc=se_packet_mem.used_list;
+               se_packet_mem.used_list=se_packet_mem.used_list->next;
+               npc->next=se_packet_mem.free_list;
+               se_packet_mem.free_list=npc;
        }
 
        /* clear them all out */
-       for(npc=emem_packet_mem.free_list;npc;npc=npc->next){
+       for(npc=se_packet_mem.free_list;npc;npc=npc->next){
                npc->amount_free=EMEM_PACKET_CHUNK_SIZE;
                npc->free_offset=0;
        }
index af10c7e6375abcb167a6dfe2bd7924859084b3fc..d2a9487328746d33f8b795a543697a3a17166ca5 100644 (file)
@@ -74,4 +74,30 @@ gchar** ep_strsplit(const gchar* string, const gchar* delimiter, int max_tokens)
 /* release all memory allocated in the previous packet dissector */
 void ep_free_all(void);
 
+
+
+
+
+/* Functions for handling memory allocation and garbage collection with 
+ * a capture lifetime scope.
+ * These functions are used to allocate memory that will only remain persistent
+ * until ethereal opens a new capture or capture file.
+ * Everytime ethereal starts a new capture or opens a new capture file
+ * all the data allocated through these functions will be released back 
+ * to the free pool.
+ *
+ * These functions are very fast and offer automatic garbage collection.
+ */
+/* Initialize capture-lifetime memory allocation pool. This function is called 
+ * once when [t]ethereal is initialized to set up the required structures.
+ */
+void se_init_chunk(void);
+
+/* Allocate memory with a capture lifetime scope */
+void *se_alloc(size_t size);
+
+/* release all memory allocated */
+void se_free_all(void);
+
+
 #endif /* emem.h */
index e9c691b35bec911624ed91d2adf2e58e7b95b662..eb9cdbc49398357aa0efe5ea95f1641a3e73d562 100644 (file)
@@ -52,6 +52,7 @@
 #include "tvbuff.h"
 #include "plugins.h"
 #include "epan_dissect.h"
+#include "emem.h"
 
 #include <epan/reassemble.h>
 
@@ -129,6 +130,9 @@ call_init_routine(gpointer routine, gpointer dummy _U_)
 void
 init_dissection(void)
 {
+       /* Reclaim and reinitialize all memory of seasonal scope */
+       se_free_all();
+
        /* Initialize the table of conversations. */
        epan_conversation_init();
 
index ec93f57d0f766fa6486c139e18a8dc0810007de8..30150bfa129471a3f49b509def1df8f36665cdc4 100644 (file)
@@ -1640,6 +1640,7 @@ main(int argc, char *argv[])
 
   /* initialize memory allocation subsystem */
   ep_init_chunk();
+  se_init_chunk();
 
   /*** create the compile and runtime version strings ***/
 #ifdef _WIN32
index c25ee5a09c55324e5883101b148cebc8913f83c3..bb8c60880032604229fee393a7d0c803c7d5b270 100644 (file)
@@ -654,6 +654,7 @@ main(int argc, char *argv[])
 
   /* initialize memory allocation subsystem */
   ep_init_chunk();
+  se_init_chunk();
   
 #ifdef HAVE_LIBPCAP
   capture_opts_init(&capture_opts, NULL /* cfile */);