f_malloc.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2001-2003 FhG Fokus
00003  *
00004  * This file is part of sip-router, a free SIP server.
00005  *
00006  * Permission to use, copy, modify, and distribute this software for any
00007  * purpose with or without fee is hereby granted, provided that the above
00008  * copyright notice and this permission notice appear in all copies.
00009  *
00010  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
00011  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
00012  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
00013  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00014  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00015  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
00016  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00017  */
00018 
00019 /*
00020  * History:
00021  * --------
00022  *  2003-05-21  on sparc64 roundto 8 even in debugging mode (so malloc'ed
00023  *               long longs will be 64 bit aligned) (andrei)
00024  *  2004-07-19  support for 64 bit (2^64 mem. block) and more info
00025  *               for the future de-fragmentation support (andrei)
00026  *  2004-11-10  support for > 4Gb mem., switched to long (andrei)
00027  *  2007-06-23  added hash bitmap (andrei)
00028  */
00029 
00037 #if !defined(f_malloc_h)
00038 #define f_malloc_h
00039 
00040 #ifdef DBG_QM_MALLOC
00041 #ifndef DBG_F_MALLOC
00042         #define DBG_F_MALLOC
00043 #endif /* DBG_F_MALLOC */
00044 #endif /* DBG_QM_MALLOC */
00045 
00046 #include "meminfo.h"
00047 
00048 
00053 #define F_MALLOC_HASH_BITMAP
00054 
00055 #ifdef DBG_F_MALLOC
00056 #if defined(__CPU_sparc64) || defined(__CPU_sparc)
00057 /* tricky, on sun in 32 bits mode long long must be 64 bits aligned
00058  * but long can be 32 bits aligned => malloc should return long long
00059  * aligned memory */
00060         #define ROUNDTO         sizeof(long long)
00061 #else
00062         #define ROUNDTO         sizeof(void*) /* size we round to, must be = 2^n, and
00063                       sizeof(fm_frag) must be multiple of ROUNDTO !*/
00064 #endif
00065 #else /* DBG_F_MALLOC */
00066         #define ROUNDTO 8UL
00067 #endif
00068 #define MIN_FRAG_SIZE   ROUNDTO
00069 
00070 
00071 
00072 #define F_MALLOC_OPTIMIZE_FACTOR 14UL /* used below */
00073 
00074 #define F_MALLOC_OPTIMIZE  (1UL<<F_MALLOC_OPTIMIZE_FACTOR)
00075 
00076 
00077 #define F_HASH_SIZE (F_MALLOC_OPTIMIZE/ROUNDTO + \
00078                 (sizeof(long)*8-F_MALLOC_OPTIMIZE_FACTOR)+1)
00079 
00080 #ifdef F_MALLOC_HASH_BITMAP
00081 typedef unsigned long fm_hash_bitmap_t;
00082 #define FM_HASH_BMP_BITS  (sizeof(fm_hash_bitmap_t)*8)
00083 #define FM_HASH_BMP_SIZE  \
00084         ((F_HASH_SIZE+FM_HASH_BMP_BITS-1)/FM_HASH_BMP_BITS)
00085 #endif
00086 
00093 struct fm_frag{
00094         unsigned long size;
00095         union{
00096                 struct fm_frag* nxt_free;
00097                 long reserved;
00098         }u;
00099 #ifdef DBG_F_MALLOC
00100         const char* file;
00101         const char* func;
00102         unsigned long line;
00103         unsigned long check;
00104 #endif
00105 };
00106 
00107 struct fm_frag_lnk{
00108         struct fm_frag* first;
00109         unsigned long no;
00110 };
00111 
00116 struct fm_block{
00117         unsigned long size; 
00118 #if defined(DBG_F_MALLOC) || defined(MALLOC_STATS)
00119         unsigned long used; 
00120         unsigned long real_used; 
00121         unsigned long max_real_used;
00122 #endif
00123         
00124         struct fm_frag* first_frag;
00125         struct fm_frag* last_frag;
00126 #ifdef F_MALLOC_HASH_BITMAP
00127         fm_hash_bitmap_t free_bitmap[FM_HASH_BMP_SIZE];
00128 #endif
00129         struct fm_frag_lnk free_hash[F_HASH_SIZE];
00130 };
00131 
00132 
00139 struct fm_block* fm_malloc_init(char* address, unsigned long size);
00140 
00141 
00148 #ifdef DBG_F_MALLOC
00149 void* fm_malloc(struct fm_block* qm, unsigned long size,
00150                                         const char* file, const char* func, unsigned int line);
00151 #else
00152 void* fm_malloc(struct fm_block* qm, unsigned long size);
00153 #endif
00154 
00155 
00163 #ifdef DBG_F_MALLOC
00164 void  fm_free(struct fm_block* qm, void* p, const char* file, const char* func, 
00165                                 unsigned int line);
00166 #else
00167 void  fm_free(struct fm_block* qm, void* p);
00168 #endif
00169 
00170 
00180 #ifdef DBG_F_MALLOC
00181 void*  fm_realloc(struct fm_block* qm, void* p, unsigned long size, 
00182                                         const char* file, const char* func, unsigned int line);
00183 #else
00184 void*  fm_realloc(struct fm_block* qm, void* p, unsigned long size);
00185 #endif
00186 
00187 
00192 void fm_status(struct fm_block* qm);
00193 
00194 
00203 void fm_info(struct fm_block* qm, struct mem_info* info);
00204 
00205 
00212 unsigned long fm_available(struct fm_block* qm);
00213 
00214 
00219 #ifdef DBG_F_MALLOC
00220 void fm_sums(struct fm_block* qm);
00221 #else
00222 #define fm_sums(qm) do{}while(0)
00223 #endif /* DBG_F_MALLOC */
00224 
00225 #endif