Ethereal-dev: Re: [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.
From: "Peter Johansson" <Peter.Johansson@xxxxxxxxxxxx>
Date: Fri, 1 Jul 2005 11:50:56 +0200 (CEST)
ronnie sahlberg said: > 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 I had a quick look at the source and have comments on the function emem_capture_alloc: 1) Why is the size parameter to emem_capture_alloc signed? Why is it *only* 32-bit? Should perhaps size_t be used instead? 2) Why is the size rounded to the nearest higher 4-byte boundary. This assumes a 32-bit platform, right? Why not round to an 8 byte boundary instead to make this work on 64-bit platforms too!? I took the liberty of changing the implementation a bit as can be seen in the attached modified_emem.c file. There I have made changes for my questions in 1) and 2) above as well as rewriting it a bit to be a bit (in my personal oppinion) more generic for future enhancements. Regards, Peter
/* 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_init_chunk(emem_chunk_t **chunk) { *chunk=g_malloc(sizeof(emem_chunk_t)); *chunk->next=NULL; *chunk->amount_free=EMEM_PACKET_CHUNK_SIZE; *chunk->free_offset=0; *chunk->g_malloc(*EMEM_PACKET_CHUNK_SIZE); } /* allocate 'size' amount of memory with an allocation lifetime until the * next packet. */ char * emem_alloc(emem_chunk_t **chunk, size_t size) { char *buf; /* round up to an 8 byte boundary */ if(size&0x08){ size=(size+8)&0xfffffff0; } /* 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>chunk->amount_free){ emem_chunk_t *npc; npc=g_malloc(sizeof(emem_chunk_t)); npc->next=(*chunk); (*chunk)=npc; (*chunk)->amount_free=EMEM_PACKET_CHUNK_SIZE; (*chunk)->free_offset=0; (*chunk)->g_malloc(EMEM_PACKET_CHUNK_SIZE); } buf=(*chunk)->data+(*chunk)->free_offset; (*chunk)->amount_free-=size; (*chunk)->free_offset+=size; return buf; } /* release all allocated memory back to the pool. */ void emem_free_all(emem_chunk_t *chunk) { emem_chunk_t *tmp; for(tmp=chunk;tmp;tmp=tmp->next){ chunk->amount_free=EMEM_PACKET_CHUNK_SIZE; chunk->free_offset=0; } } /* 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_init_chunk(&emem_packet_chunk); } /* allocate 'size' amount of memory with an allocation lifetime until the * next packet. */ char * emem_packet_alloc(size_t size) { return emem_alloc(&emem_packet_chunk, size); } /* release all allocated memory back to the pool. */ void emem_packet_free_all(void) { emem_free_all(emem_packet_chunk); } /* 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_init_chunk(&emem_capture_chunk); } /* allocate 'size' amount of memory with an allocation lifetime until the * next capture file. */ char * emem_capture_alloc(size_t size) { return emem_alloc(&emem_capture_chunk, size); } /* release all allocated memory back to the pool. */ void emem_capture_free_all(void) { emem_free_all(emem_capture_chunk); }
- Follow-Ups:
- Re: [Ethereal-dev] Proposal for mew memory allocation API
- From: ronnie sahlberg
- Re: [Ethereal-dev] Proposal for mew memory allocation API
- References:
- [Ethereal-dev] Proposal for mew memory allocation API
- From: ronnie sahlberg
- [Ethereal-dev] Proposal for mew memory allocation API
- Prev by Date: [Ethereal-dev] Proposal for mew memory allocation API
- Next by Date: Re: [Ethereal-dev] Proposal for mew memory allocation API
- Previous by thread: [Ethereal-dev] Proposal for mew memory allocation API
- Next by thread: Re: [Ethereal-dev] Proposal for mew memory allocation API
- Index(es):