/* $Id$
 * simple & fast malloc library
 * Copyright (C) 2001-2003 FhG Fokus
 * 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
 * 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
 * History:
 * --------
 *  2003-05-21  on sparc64 roundto 8 even in debugging mode (so malloc'ed
 *               long longs will be 64 bit aligned) (andrei)
 *  2004-07-19  support for 64 bit (2^64 mem. block) and more info
 *               for the future de-fragmentation support (andrei)

#if !defined(q_malloc_h) && !defined(VQ_MALLOC) && !defined(F_MALLOC)
#define q_malloc_h

/* defs*/
#ifdef __CPU_sparc64
/* 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)
	#define ROUNDTO		sizeof(void*) /* minimum possible ROUNDTO ->heavy 
#else /* DBG_QM_MALLOC */
	#define ROUNDTO		16 /* size we round to, must be = 2^n  and also
							 must be multiple of ROUNDTO!

#define QM_MALLOC_OPTIMIZE_FACTOR 11 /*used below */
								/* size to optimize for,
									(most allocs <= this size),
									must be 2^k */

#define QM_HASH_SIZE ((unsigned long)(QM_MALLOC_OPTIMIZE/ROUNDTO + \

/* hash structure:
 * 0 .... QM_MALLOC_OPTIMIE/ROUNDTO  - small buckets, size increases with
 *                            ROUNDTO from bucket to bucket
 * +1 .... end -  size = 2^k, big buckets */

struct qm_frag{
	unsigned long size;
		struct qm_frag* nxt_free;
		long is_free;
	char* file;
	char* func;
	unsigned long line;
	unsigned long check;

struct qm_frag_end{
	unsigned long check1;
	unsigned long check2;
	unsigned long reserved1;
	unsigned long reserved2;
	unsigned long size;
	struct qm_frag* prev_free;

struct qm_frag_lnk{
	struct qm_frag head;
	struct qm_frag_end tail;
	unsigned long no;

struct qm_block{
	unsigned long size; /* total size */
	unsigned long used; /* alloc'ed size*/
	unsigned long real_used; /* used+malloc overhead*/
	unsigned long max_real_used;
	struct qm_frag* first_frag;
	struct qm_frag_end* last_frag_end;
	struct qm_frag_lnk free_hash[QM_HASH_SIZE];
	/*struct qm_frag_end free_lst_end;*/

struct qm_block* qm_malloc_init(char* address, unsigned int size);

void* qm_malloc(struct qm_block*, unsigned int size, char* file, char* func, 
					unsigned int line);
void* qm_malloc(struct qm_block*, unsigned int size);

void  qm_free(struct qm_block*, void* p, char* file, char* func, 
				unsigned int line);
void  qm_free(struct qm_block*, void* p);
void* qm_realloc(struct qm_block*, void* p, unsigned int size,
				char* file, char* func, unsigned int line);
void* qm_realloc(struct qm_block*, void* p, unsigned int size);

void  qm_status(struct qm_block*);