mem/f_malloc.h
47f59303
 /*
84d8e165
  * Copyright (C) 2001-2003 FhG Fokus
7dd0b342
  *
4c4e5766
  * This file is part of Kamailio, a free SIP server.
7dd0b342
  *
e4f42ce1
  * Permission to use, copy, modify, and distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above
  * copyright notice and this permission notice appear in all copies.
7dd0b342
  *
e4f42ce1
  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1c4b78f3
  */
47f59303
 
 /**
  * \file
  * \brief Simple, very fast, malloc library
  * \ingroup mem
  */
 
7dd0b342
 
467a12b7
 #if defined(F_MALLOC)
 
f92113d8
 #if !defined(f_malloc_h)
1c4b78f3
 #define f_malloc_h
 
51dffb0a
 #include "meminfo.h"
1c4b78f3
 
467a12b7
 #ifdef DBG_SR_MEMORY
 #define DBG_F_MALLOC
 #endif
1c4b78f3
 
c20927f7
 /**
  * Use a bitmap to quickly find free fragments, should speed up
  * especially startup (non-warmed-up malloc) 
  */
dc36e273
 #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
 
 
 
c20927f7
 #define F_MALLOC_OPTIMIZE_FACTOR 14UL /* used below */
 /** Size to optimize for, (most allocs <= this size), must be 2^k */
c082177a
 #define F_MALLOC_OPTIMIZE  (1UL<<F_MALLOC_OPTIMIZE_FACTOR)
c20927f7
 
1c4b78f3
 
 #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
 
c20927f7
 /**
  * \name Hash structure
  * - 0 .... F_MALLOC_OPTIMIZE/ROUNDTO  - small buckets, size increases with
  * ROUNDTO from bucket to bucket
  * - +1 .... end -  size = 2^k, big buckets
  */
1c4b78f3
 struct fm_frag{
8cced16d
 	unsigned long size;         /* size of fragment */
 	struct fm_frag* next_free;  /* next free frag in slot */
 	struct fm_frag* prev_free;  /* prev free frag in slot - for faster join/defrag */
 	unsigned int is_free;       /* used to detect if fragment is free (when not 0) */
1c4b78f3
 #ifdef DBG_F_MALLOC
4168f707
 	const char* file;
 	const char* func;
ea189de0
 	const char* mname;
85f1f3ee
 	unsigned long line;
1c4b78f3
 #endif
8cced16d
 	unsigned int check;
1c4b78f3
 };
 
46b467df
 struct fm_frag_lnk{
 	struct fm_frag* first;
 	unsigned long no;
 };
1c4b78f3
 
47f59303
 /**
  * \brief Block of memory for F_MALLOC memory manager
  * \see mem_info
  */
1c4b78f3
 struct fm_block{
aac0c211
 	int type;
d949f97e
 	unsigned long size; /** total size */
 	unsigned long used; /** allocated size*/
 	unsigned long real_used; /** used + malloc overhead */
85f1f3ee
 	unsigned long max_real_used;
aac0c211
 	unsigned long ffrags;
1c4b78f3
 	
 	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
 };
 
 
c20927f7
 /**
  * \brief Initialize memory manager malloc
  * \param address start address for memory block
  * \param size Size of allocation
  * \return return the fm_block
  */
aac0c211
 struct fm_block* fm_malloc_init(char* address, unsigned long size, int type);
1c4b78f3
 
c20927f7
 
 /**
  * \brief Main memory manager allocation function
  * \param qm memory block
  * \param size memory allocation size
  * \return address of allocated memory
  */
1c4b78f3
 #ifdef DBG_F_MALLOC
467a12b7
 void* fm_malloc(void* qmp, unsigned long size,
ea189de0
 					const char* file, const char* func, unsigned int line,
 					const char* mname);
1c4b78f3
 #else
467a12b7
 void* fm_malloc(void* qmp, unsigned long size);
1c4b78f3
 #endif
 
c20927f7
 
 /**
  * \brief Main memory manager free function
  * 
  * Main memory manager free function, provide functionality necessary for pkg_free
  * \param qm memory block
  * \param p freed memory
  */
1c4b78f3
 #ifdef DBG_F_MALLOC
ea189de0
 void fm_free(void* qmp, void* p, const char* file, const char* func,
 				unsigned int line, const char* mname);
1c4b78f3
 #else
467a12b7
 void  fm_free(void* qmp, void* p);
1c4b78f3
 #endif
 
c20927f7
 
 /**
  * \brief Main memory manager realloc function
  * 
  * Main memory manager realloc function, provide functionality for pkg_realloc
  * \param qm memory block
  * \param p reallocated memory block
  * \param size
  * \return reallocated memory block
  */
9c01c860
 #ifdef DBG_F_MALLOC
ea189de0
 void* fm_realloc(void* qmp, void* p, unsigned long size,
 					const char* file, const char* func, unsigned int line, const char *mname);
9c01c860
 #else
467a12b7
 void*  fm_realloc(void* qmp, void* p, unsigned long size);
9c01c860
 #endif
 
c20927f7
 
 /**
  * \brief Report internal memory manager status
  * \param qm memory block
  */
467a12b7
 void fm_status(void* qmp);
c20927f7
 
 
 /**
  * \brief Fills a malloc info structure with info about the block
  *
  * Fills a malloc info structure with info about the block, if a
  * parameter is not supported, it will be filled with 0
  * \param qm memory block
f157bd9c
  * \param info memory information
c20927f7
  */
467a12b7
 void fm_info(void* qmp, struct mem_info* info);
1c4b78f3
 
c20927f7
 
 /**
  * \brief Helper function for available memory report
  * \param qm memory block
  * \return Returns how much free memory is available, on error (not compiled
  * with bookkeeping code) returns (unsigned long)(-1)
  */
467a12b7
 unsigned long fm_available(void* qmp);
1c4b78f3
 
c20927f7
 
 /**
  * \brief Debugging helper, summary and logs all allocated memory blocks
  * \param qm memory block
  */
467a12b7
 void fm_sums(void* qmp);
ea189de0
 void fm_mod_get_stats(void* qm, void **fm_root);
 void fm_mod_free_stats(void *root);
 
 typedef struct _mem_counter{
 	const char *file;
 	const char *func;
 	const char *mname;
 	unsigned long line;
 
 	unsigned long size;
 	int count;
 
 	struct _mem_counter *next;
 } mem_counter;
ed20ee1d
 
1c4b78f3
 #endif
467a12b7
 #endif