mem/f_malloc.h
1c4b78f3
 /* $Id$
  *
09b2f164
  * simple, very fast, malloc library
7dd0b342
  *
84d8e165
  * Copyright (C) 2001-2003 FhG Fokus
7dd0b342
  *
  * This file is part of ser, a free SIP server.
  *
  * ser 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
  *
  * For a license to use the ser software under conditions
  * other than those described here, or to purchase support for this
  * software, please contact iptel.org by e-mail at the following addresses:
  *    info@iptel.org
  *
  * ser 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
1c4b78f3
  */
f72c2aae
 /*
  * History:
  * --------
  *  2003-05-21  on sparc64 roundto 8 even in debugging mode (so malloc'ed
  *               long longs will be 64 bit aligned) (andrei)
46b467df
  *  2004-07-19  support for 64 bit (2^64 mem. block) and more info
  *               for the future de-fragmentation support (andrei)
c082177a
  *  2004-11-10  support for > 4Gb mem., switched to long (andrei)
dc36e273
  *  2007-06-23  added hash bitmap (andrei)
f72c2aae
  */
1c4b78f3
 
7dd0b342
 
1c4b78f3
 #if !defined(f_malloc_h) && !defined(VQ_MALLOC) 
 #define f_malloc_h
 
51dffb0a
 #ifdef DBG_QM_MALLOC
 #define DBG_F_MALLOC
 #endif
1c4b78f3
 
51dffb0a
 #include "meminfo.h"
1c4b78f3
 
 /* defs*/
 
dc36e273
 /* use a bitmap to quickly find free fragments, should speed up
  * especially startup (non-warmed-up malloc) */
 #define F_MALLOC_HASH_BITMAP
 
f72c2aae
 #ifdef DBG_F_MALLOC
245b8b93
 #if defined(__CPU_sparc64) || defined(__CPU_sparc)
f72c2aae
 /* tricky, on sun in 32 bits mode long long must be 64 bits aligned
  * but long can be 32 bits aligned => malloc should return long long
  * aligned memory */
 	#define ROUNDTO		sizeof(long long)
 #else
 	#define ROUNDTO		sizeof(void*) /* size we round to, must be = 2^n, and
1c4b78f3
                       sizeof(fm_frag) must be multiple of ROUNDTO !*/
f72c2aae
 #endif
 #else /* DBG_F_MALLOC */
c082177a
 	#define ROUNDTO 8UL
f72c2aae
 #endif
1c4b78f3
 #define MIN_FRAG_SIZE	ROUNDTO
 
 
 
dca4cad0
 #define F_MALLOC_OPTIMIZE_FACTOR 14UL /*used below */
c082177a
 #define F_MALLOC_OPTIMIZE  (1UL<<F_MALLOC_OPTIMIZE_FACTOR)
1c4b78f3
 								/* size to optimize for,
46b467df
 									(most allocs <= this size),
1c4b78f3
 									must be 2^k */
 
 #define F_HASH_SIZE (F_MALLOC_OPTIMIZE/ROUNDTO + \
46b467df
 		(sizeof(long)*8-F_MALLOC_OPTIMIZE_FACTOR)+1)
1c4b78f3
 
dc36e273
 #ifdef F_MALLOC_HASH_BITMAP
 typedef unsigned long fm_hash_bitmap_t;
 #define FM_HASH_BMP_BITS  (sizeof(fm_hash_bitmap_t)*8)
 #define FM_HASH_BMP_SIZE  \
 	((F_HASH_SIZE+FM_HASH_BMP_BITS-1)/FM_HASH_BMP_BITS)
 #endif
 
1c4b78f3
 /* hash structure:
46b467df
  * 0 .... F_MALLOC_OPTIMIZE/ROUNDTO  - small buckets, size increases with
1c4b78f3
  *                            ROUNDTO from bucket to bucket
  * +1 .... end -  size = 2^k, big buckets */
 
 struct fm_frag{
85f1f3ee
 	unsigned long size;
1c4b78f3
 	union{
 		struct fm_frag* nxt_free;
85f1f3ee
 		long reserved;
1c4b78f3
 	}u;
 #ifdef DBG_F_MALLOC
4168f707
 	const char* file;
 	const char* func;
85f1f3ee
 	unsigned long line;
 	unsigned long check;
1c4b78f3
 #endif
 };
 
46b467df
 struct fm_frag_lnk{
 	struct fm_frag* first;
 	unsigned long no;
 };
1c4b78f3
 
 struct fm_block{
85f1f3ee
 	unsigned long size; /* total size */
51dffb0a
 #if defined(DBG_F_MALLOC) || defined(MALLOC_STATS)
85f1f3ee
 	unsigned long used; /* alloc'ed size*/
 	unsigned long real_used; /* used+malloc overhead*/
 	unsigned long max_real_used;
1c4b78f3
 #endif
 	
 	struct fm_frag* first_frag;
 	struct fm_frag* last_frag;
dc36e273
 #ifdef F_MALLOC_HASH_BITMAP
 	fm_hash_bitmap_t free_bitmap[FM_HASH_BMP_SIZE];
 #endif
46b467df
 	struct fm_frag_lnk free_hash[F_HASH_SIZE];
1c4b78f3
 };
 
 
 
c082177a
 struct fm_block* fm_malloc_init(char* address, unsigned long size);
1c4b78f3
 
 #ifdef DBG_F_MALLOC
c082177a
 void* fm_malloc(struct fm_block*, unsigned long size,
4168f707
 					const char* file, const char* func, unsigned int line);
1c4b78f3
 #else
c082177a
 void* fm_malloc(struct fm_block*, unsigned long size);
1c4b78f3
 #endif
 
 #ifdef DBG_F_MALLOC
4168f707
 void  fm_free(struct fm_block*, void* p, const char* file, const char* func, 
1c4b78f3
 				unsigned int line);
 #else
 void  fm_free(struct fm_block*, void* p);
 #endif
 
9c01c860
 #ifdef DBG_F_MALLOC
c082177a
 void*  fm_realloc(struct fm_block*, void* p, unsigned long size, 
4168f707
 					const char* file, const char* func, unsigned int line);
9c01c860
 #else
c082177a
 void*  fm_realloc(struct fm_block*, void* p, unsigned long size);
9c01c860
 #endif
 
1c4b78f3
 void  fm_status(struct fm_block*);
473f6f2e
 void  fm_info(struct fm_block*, struct mem_info*);
1c4b78f3
 
8799e028
 unsigned long fm_available(struct fm_block*);
1c4b78f3
 
 #endif