Huge thanks to our Platinum Members Endace and LiveAction,
and our Silver Member Veeam, for supporting the Wireshark Foundation and project.

Ethereal-dev: [Ethereal-dev] Proposal for mew memory allocation API

Note: This archive is from the project's previous web site, ethereal.com. This list is no longer active.

Date Prev · Date Next · Thread Prev · Thread Next
From: ronnie sahlberg <ronniesahlberg@xxxxxxxxx>
Date: Fri, 1 Jul 2005 04:06:13 -0400
List

Attached are two new files with a new FAST api to provide memory
allocation and automatic garbage collection with an allocation
lifetime of
either    until-next-packet
or until-next-capture-file


The functions are very very simple and should be fast. If used
properly we should also get rid of all memory leaks.


The purpose of these functions is twofold:
1, to speed up memory allocation
2, to reduce the probability of memory leaks.


Two areas where functions such as these could immediately benefit are the

1, Packet lifetime : silly things such as
 cur_idx++;
  if(cur_idx>3){
     cur_idx=0;
  }
  cur=&str[cur_idx][0];

which we have just about everywehere.

2, Capture lifetime :  
many of those GMemChunk that are used everywhere.



please comment.
/* emem.c
 * Ethereal memory management and garbage collection functions
 *
 * $Id: emem.c 14510 2005-05-31 21:17:54Z ulfl $
 *
 * Ethereal - Network traffic analyzer
 * By Gerald Combs <gerald@xxxxxxxxxxxx>
 * Copyright 1998 Gerald Combs
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#include "emem.h"

/* When required, allocate more memory from the OS in this size chunks */
#define EMEM_PACKET_CHUNK_SIZE 1048576

typedef struct _emem_chunk_t {
	struct _emem_chunk_t *next;
	int	amount_free;
	int	free_offset;
	char *buf;
} emem_chunk_t;

static emem_chunk_t *emem_packet_chunk=NULL;
static emem_chunk_t *emem_capture_chunk=NULL;

/* Initialize the packet-lifetime memory allocation pool.
 * This function should be called only once when Etehreal or Tethereal starts
 * up.
 */
void
emem_packet_init_chunk(void)
{
	emem_packet_chunk=g_malloc(sizeof(emem_chunk_t));
	emem_packet_chunk->next=NULL;
	emem_packet_chunk->amount_free=EMEM_PACKET_CHUNK_SIZE;
	emem_packet_chunk->free_offset=0;
	emem_packet_chunk->g_malloc(EMEM_PACKET_CHUNK_SIZE);
}

/* allocate 'size' amount of memory with an allocation lifetime until the
 * next packet.
 */
char *
emem_packet_alloc(int size)
{
	char *buf;

	/* round up to 4 byte boundary */
	if(size&0x03){
		size=(size+3)&0xfffffffc;
	}

	/* make sure we dont try to allocate too much */
	g_assert(size<(EMEM_PACKET_CHUNK_SIZE>>2));

	/* oops, we need to allocate more memory to serve this request */
	if(size>emem_packet_chunk->amount_free){
		emem_chunk_t *npc;
		npc=g_malloc(sizeof(emem_chunk_t));
		npc->next=emem_packet_chunk;
		emem_packet_chunk=npc;
		emem_packet_chunk->amount_free=EMEM_PACKET_CHUNK_SIZE;
		emem_packet_chunk->free_offset=0;
		emem_packet_chunk->g_malloc(EMEM_PACKET_CHUNK_SIZE);
	}

	buf=emem_packet_chunk->data+emem_packet_chunk->free_offset;

	emem_packet_chunk->amount_free-=size;
	emem_packet_chunk->free_offset+=size;

	return buf;
}

/* release all allocated memory back to the pool.
 */
void
emem_packet_free_all(void)
{
	emem_chunk_t *tmp;

	for(tmp=emem_packet_chunk;tmp;tmp=tmp->next){
		emem_packet_chunk->amount_free=EMEM_PACKET_CHUNK_SIZE;
		emem_packet_chunk->free_offset=0;
	}
}
		


/* Initialize the capture-lifetime memory allocation pool.
 * This function should be called only once when Etehreal or Tethereal starts
 * up.
 */
void
emem_capture_init_chunk(void)
{
	emem_capture_chunk=g_malloc(sizeof(emem_chunk_t));
	emem_capture_chunk->next=NULL;
	emem_capture_chunk->amount_free=EMEM_PACKET_CHUNK_SIZE;
	emem_capture_chunk->free_offset=0;
	emem_capture_chunk->g_malloc(EMEM_PACKET_CHUNK_SIZE);
}

/* allocate 'size' amount of memory with an allocation lifetime until the
 * next capture file.
 */
char *
emem_capture_alloc(int size)
{
	char *buf;

	/* round up to 4 byte boundary */
	if(size&0x03){
		size=(size+3)&0xfffffffc;
	}

	/* make sure we dont try to allocate too much */
	g_assert(size<(EMEM_PACKET_CHUNK_SIZE>>2));

	/* oops, we need to allocate more memory to serve this request */
	if(size>emem_capture_chunk->amount_free){
		emem_chunk_t *npc;
		npc=g_malloc(sizeof(emem_chunk_t));
		npc->next=emem_capture_chunk;
		emem_capture_chunk=npc;
		emem_capture_chunk->amount_free=EMEM_PACKET_CHUNK_SIZE;
		emem_capture_chunk->free_offset=0;
		emem_capture_chunk->g_malloc(EMEM_PACKET_CHUNK_SIZE);
	}

	buf=emem_capture_chunk->data+emem_capture_chunk->free_offset;

	emem_capture_chunk->amount_free-=size;
	emem_capture_chunk->free_offset+=size;

	return buf;
}

/* release all allocated memory back to the pool.
 */
void
emem_capture_free_all(void)
{
	emem_chunk_t *tmp;

	for(tmp=emem_capture_chunk;tmp;tmp=tmp->next){
		emem_capture_chunk->amount_free=EMEM_PACKET_CHUNK_SIZE;
		emem_capture_chunk->free_offset=0;
	}
}
		

/* emem.h
 * Definitions for ethereal memory management and garbage collection
 *
 * $Id: emem.h 14436 2005-05-25 23:28:59Z ulfl $
 *
 * Ethereal - Network traffic analyzer
 * By Gerald Combs <gerald@xxxxxxxxxxxx>
 * Copyright 1998 Gerald Combs
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#ifndef __EMEM_H__
#define __EMEM_H__

/* Functions for handling memory allocation and garbage collection with 
 * a packet lifetime scope.
 * These functions are used to allocate memory that will only remain persistent
 * until ethereal starts dissecting the next packet in the list.
 * Everytime ethereal starts decoding the next packet all memory allocated
 * through these functions will be released back to the free pool.
 *
 * These functions are very fast and offer automatic garbage collection.
 */
/* This function needs to be called once when ethereal starts up to initialize
 * the chunk(s) for packet lifetime allocations.
 *
 * You can/should not use these functions to allocate very large buffers.
 */

/* Initialize packet-lifetime memory allocation pool */
void emem_packet_init_chunk(void);
/* Allocate memory with a packet lifetime scope */
char *emem_packet_alloc(int size);
/* release all memory allocated in the previous packet dissector */
void emem_packet_free_all(void);


/* Similar allocation routines but with cature file lifetime.
 * I.e. the allocations are only persistent until the next time the current
 * capture is re-read or a new capture is read.
 */
/* Initialize packet-lifetime memory allocation pool */
void emem_capture_init_chunk(void);
/* Allocate memory with a capture lifetime scope */
char *emem_capture_alloc(int size);
/* release all memory allocated in the previous capture */
void emem_capture_free_all(void);


#endif /* emem.h */