2 * Ethereal memory management and garbage collection functions
5 * $Id: emem.c 14510 2005-05-31 21:17:54Z ulfl $
7 * Ethereal - Network traffic analyzer
8 * By Gerald Combs <gerald@ethereal.com>
9 * Copyright 1998 Gerald Combs
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 /* When required, allocate more memory from the OS in this size chunks */
32 #define EMEM_PACKET_CHUNK_SIZE 10485760
34 typedef struct _emem_chunk_t {
35 struct _emem_chunk_t *next;
36 unsigned int amount_free;
37 unsigned int free_offset;
41 typedef struct _emem_header_t {
42 emem_chunk_t *free_list;
43 emem_chunk_t *used_list;
46 static emem_header_t emem_packet_mem;
48 /* Initialize the packet-lifetime memory allocation pool.
49 * This function should be called only once when Etehreal or Tethereal starts
55 emem_packet_mem.free_list=NULL;
56 emem_packet_mem.used_list=NULL;
59 /* allocate 'size' amount of memory with an allocation lifetime until the
67 /* round up to 8 byte boundary */
69 size=(size+7)&0xfffffff8;
72 /* make sure we dont try to allocate too much (arbitrary limit) */
73 g_assert(size<(EMEM_PACKET_CHUNK_SIZE>>2));
75 /* we dont have any free data, so we must allocate a new one */
76 if(!emem_packet_mem.free_list){
78 npc=g_malloc(sizeof(emem_chunk_t));
80 npc->amount_free=EMEM_PACKET_CHUNK_SIZE;
82 npc->buf=g_malloc(EMEM_PACKET_CHUNK_SIZE);
83 emem_packet_mem.free_list=npc;
86 /* oops, we need to allocate more memory to serve this request
87 * than we have free. move this node to the used list and try again
89 if(size>emem_packet_mem.free_list->amount_free){
91 npc=emem_packet_mem.free_list;
92 emem_packet_mem.free_list=emem_packet_mem.free_list->next;
93 npc->next=emem_packet_mem.used_list;
94 emem_packet_mem.used_list=npc;
97 /* we dont have any free data, so we must allocate a new one */
98 if(!emem_packet_mem.free_list){
100 npc=g_malloc(sizeof(emem_chunk_t));
102 npc->amount_free=EMEM_PACKET_CHUNK_SIZE;
104 npc->buf=g_malloc(EMEM_PACKET_CHUNK_SIZE);
105 emem_packet_mem.free_list=npc;
109 buf=emem_packet_mem.free_list->buf+emem_packet_mem.free_list->free_offset;
111 emem_packet_mem.free_list->amount_free-=size;
112 emem_packet_mem.free_list->free_offset+=size;
117 /* release all allocated memory back to the pool.
124 /* move all used chunks ove to the free list */
125 while(emem_packet_mem.used_list){
126 npc=emem_packet_mem.used_list;
127 emem_packet_mem.used_list=emem_packet_mem.used_list->next;
128 npc->next=emem_packet_mem.free_list;
129 emem_packet_mem.free_list=npc;
132 /* clear them all out */
133 for(npc=emem_packet_mem.free_list;npc;npc=npc->next){
134 npc->amount_free=EMEM_PACKET_CHUNK_SIZE;