f_malloc.h

Go to the documentation of this file.
00001 /* $Id: f_malloc.h 5720 2009-03-18 12:55:53Z henningw $
00002  *
00003  * simple, very fast, malloc library
00004  *
00005  * Copyright (C) 2001-2003 FhG Fokus
00006  *
00007  * This file is part of Kamailio, a free SIP server.
00008  *
00009  * Kamailio is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version
00013  *
00014  * Kamailio is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License 
00020  * along with this program; if not, write to the Free Software 
00021  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022  *
00023  * History:
00024  * --------
00025  *  2003-05-21  on sparc64 roundto 8 even in debugging mode (so malloc'ed
00026  *               long longs will be 64 bit aligned) (andrei)
00027  *  2004-07-19  support for 64 bit (2^64 mem. block) and more info
00028  *               for the future de-fragmentation support (andrei)
00029  *  2004-11-10  support for > 4Gb mem., switched to long (andrei)
00030  *  2007-06-23  added hash bitmap (andrei)
00031  */
00032 
00033 
00034 #if !defined(f_malloc_h)
00035 #define f_malloc_h
00036 
00037 #ifdef DBG_QM_MALLOC
00038 #define DBG_F_MALLOC
00039 #endif
00040 
00041 #include "meminfo.h"
00042 
00043 /* defs*/
00044 
00045 /* use a bitmap to quickly find free fragments, should speed up
00046  * especially startup (non-warmed-up malloc) */
00047 #define F_MALLOC_HASH_BITMAP
00048 
00049 #ifdef DBG_F_MALLOC
00050 #if defined(__CPU_sparc64) || defined(__CPU_sparc)
00051 /* tricky, on sun in 32 bits mode long long must be 64 bits aligned
00052  * but long can be 32 bits aligned => malloc should return long long
00053  * aligned memory */
00054    #define ROUNDTO      sizeof(long long)
00055 #else
00056    #define ROUNDTO      sizeof(void*) /* size we round to, must be = 2^n, and
00057                       sizeof(fm_frag) must be multiple of ROUNDTO !*/
00058 #endif
00059 #else /* DBG_F_MALLOC */
00060    #define ROUNDTO 8UL
00061 #endif
00062 #define MIN_FRAG_SIZE   ROUNDTO
00063 
00064 
00065 
00066 #define F_MALLOC_OPTIMIZE_FACTOR 14UL /*used below */
00067 #define F_MALLOC_OPTIMIZE  (1UL<<F_MALLOC_OPTIMIZE_FACTOR)
00068                         /* size to optimize for,
00069                            (most allocs <= this size),
00070                            must be 2^k */
00071 
00072 #define F_HASH_SIZE (F_MALLOC_OPTIMIZE/ROUNDTO + \
00073       (sizeof(long)*8-F_MALLOC_OPTIMIZE_FACTOR)+1)
00074 
00075 #ifdef F_MALLOC_HASH_BITMAP
00076 typedef unsigned long fm_hash_bitmap_t;
00077 #define FM_HASH_BMP_BITS  (sizeof(fm_hash_bitmap_t)*8)
00078 #define FM_HASH_BMP_SIZE  \
00079    ((F_HASH_SIZE+FM_HASH_BMP_BITS-1)/FM_HASH_BMP_BITS)
00080 #endif
00081 
00082 /* hash structure:
00083  * 0 .... F_MALLOC_OPTIMIZE/ROUNDTO  - small buckets, size increases with
00084  *                            ROUNDTO from bucket to bucket
00085  * +1 .... end -  size = 2^k, big buckets */
00086 
00087 struct fm_frag{
00088    unsigned long size;
00089    union{
00090       struct fm_frag* nxt_free;
00091       long reserved;
00092    }u;
00093 #ifdef DBG_F_MALLOC
00094    const char* file;
00095    const char* func;
00096    unsigned long line;
00097    unsigned long check;
00098 #endif
00099 };
00100 
00101 struct fm_frag_lnk{
00102    struct fm_frag* first;
00103    unsigned long no;
00104 };
00105 
00106 struct fm_block{
00107    unsigned long size; /* total size */
00108 #if defined(DBG_F_MALLOC) || defined(STATISTICS)
00109    unsigned long used; /* alloc'ed size*/
00110    unsigned long real_used; /* used+malloc overhead*/
00111    unsigned long max_real_used;
00112 #endif
00113    
00114    struct fm_frag* first_frag;
00115    struct fm_frag* last_frag;
00116 #ifdef F_MALLOC_HASH_BITMAP
00117    fm_hash_bitmap_t free_bitmap[FM_HASH_BMP_SIZE];
00118 #endif
00119    
00120    struct fm_frag_lnk free_hash[F_HASH_SIZE];
00121 };
00122 
00123 
00124 
00125 struct fm_block* fm_malloc_init(char* address, unsigned long size);
00126 
00127 #ifdef DBG_F_MALLOC
00128 void* fm_malloc(struct fm_block*, unsigned long size,
00129                const char* file, const char* func, unsigned int line);
00130 #else
00131 void* fm_malloc(struct fm_block*, unsigned long size);
00132 #endif
00133 
00134 #ifdef DBG_F_MALLOC
00135 void  fm_free(struct fm_block*, void* p, const char* file, const char* func, 
00136             unsigned int line);
00137 #else
00138 void  fm_free(struct fm_block*, void* p);
00139 #endif
00140 
00141 #ifdef DBG_F_MALLOC
00142 void*  fm_realloc(struct fm_block*, void* p, unsigned long size, 
00143                const char* file, const char* func, unsigned int line);
00144 #else
00145 void*  fm_realloc(struct fm_block*, void* p, unsigned long size);
00146 #endif
00147 
00148 void  fm_status(struct fm_block*);
00149 void  fm_info(struct fm_block*, struct mem_info*);
00150 
00151 unsigned long fm_available(struct fm_block*);
00152 
00153 #ifdef STATISTICS
00154 static inline unsigned long fm_get_size(struct fm_block* qm)
00155 {
00156    return qm->size;
00157 }
00158 static inline unsigned long fm_get_used(struct fm_block* qm)
00159 {
00160    return qm->used;
00161 }
00162 static inline unsigned long fm_get_free(struct fm_block* qm)
00163 {
00164    return qm->size-qm->real_used;
00165 }
00166 static inline unsigned long fm_get_real_used(struct fm_block* qm)
00167 {
00168    return qm->real_used;
00169 }
00170 static inline unsigned long fm_get_max_real_used(struct fm_block* qm)
00171 {
00172    return qm->max_real_used;
00173 }
00174 static inline unsigned long fm_get_frags(struct fm_block* qm)
00175 {
00176    unsigned long frags;
00177    unsigned int r;
00178    for(r=0,frags=0;r<F_HASH_SIZE; r++){
00179       frags+=qm->free_hash[r].no;
00180    }
00181    return frags;
00182 }
00183 #endif /*STATISTICS*/
00184 
00185 #endif

Generated on Wed May 23 06:00:45 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6