q_malloc.h

Go to the documentation of this file.
00001 /* $Id: q_malloc.h 5035 2008-10-06 11:01:24Z henningw $
00002  *
00003  * simple & 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  */
00031 
00032 
00033 #if !defined(q_malloc_h) && !defined(F_MALLOC)
00034 #define q_malloc_h
00035 
00036 #include "meminfo.h"
00037 
00038 /* defs*/
00039 #ifdef DBG_QM_MALLOC
00040 #if defined(__CPU_sparc64) || defined(__CPU_sparc)
00041 /* tricky, on sun in 32 bits mode long long must be 64 bits aligned
00042  * but long can be 32 bits aligned => malloc should return long long
00043  * aligned memory */
00044    #define ROUNDTO      sizeof(long long)
00045 #else
00046    #define ROUNDTO      sizeof(void*) /* minimum possible ROUNDTO ->heavy 
00047                                debugging*/
00048 #endif 
00049 #else /* DBG_QM_MALLOC */
00050    #define ROUNDTO      16UL /* size we round to, must be = 2^n  and also
00051                       sizeof(qm_frag)+sizeof(qm_frag_end)
00052                       must be multiple of ROUNDTO!
00053                      */
00054 #endif
00055 #define MIN_FRAG_SIZE   ROUNDTO
00056 
00057 
00058 
00059 #define QM_MALLOC_OPTIMIZE_FACTOR 14UL /*used below */
00060 #define QM_MALLOC_OPTIMIZE  ((unsigned long)(1UL<<QM_MALLOC_OPTIMIZE_FACTOR))
00061                         /* size to optimize for,
00062                            (most allocs <= this size),
00063                            must be 2^k */
00064 
00065 #define QM_HASH_SIZE ((unsigned long)(QM_MALLOC_OPTIMIZE/ROUNDTO + \
00066       (sizeof(long)*8-QM_MALLOC_OPTIMIZE_FACTOR)+1))
00067 
00068 /* hash structure:
00069  * 0 .... QM_MALLOC_OPTIMIE/ROUNDTO  - small buckets, size increases with
00070  *                            ROUNDTO from bucket to bucket
00071  * +1 .... end -  size = 2^k, big buckets */
00072 
00073 struct qm_frag{
00074    unsigned long size;
00075    union{
00076       struct qm_frag* nxt_free;
00077       long is_free;
00078    }u;
00079 #ifdef DBG_QM_MALLOC
00080    const char* file;
00081    const char* func;
00082    unsigned long line;
00083    unsigned long check;
00084 #endif
00085 };
00086 
00087 struct qm_frag_end{
00088 #ifdef DBG_QM_MALLOC
00089    unsigned long check1;
00090    unsigned long check2;
00091    unsigned long reserved1;
00092    unsigned long reserved2;
00093 #endif
00094    unsigned long size;
00095    struct qm_frag* prev_free;
00096 };
00097 
00098 
00099 
00100 struct qm_frag_lnk{
00101    struct qm_frag head;
00102    struct qm_frag_end tail;
00103    unsigned long no;
00104 };
00105 
00106 
00107 struct qm_block{
00108    unsigned long size; /* total size */
00109    unsigned long used; /* alloc'ed size*/
00110    unsigned long real_used; /* used+malloc overhead*/
00111    unsigned long max_real_used;
00112    
00113    struct qm_frag* first_frag;
00114    struct qm_frag_end* last_frag_end;
00115    
00116    struct qm_frag_lnk free_hash[QM_HASH_SIZE];
00117    /*struct qm_frag_end free_lst_end;*/
00118 };
00119 
00120 
00121 
00122 struct qm_block* qm_malloc_init(char* address, unsigned long size);
00123 
00124 #ifdef DBG_QM_MALLOC
00125 void* qm_malloc(struct qm_block*, unsigned long size, const char* file,
00126                const char* func, unsigned int line);
00127 #else
00128 void* qm_malloc(struct qm_block*, unsigned long size);
00129 #endif
00130 
00131 #ifdef DBG_QM_MALLOC
00132 void  qm_free(struct qm_block*, void* p, const char* file, const char* func, 
00133             unsigned int line);
00134 #else
00135 void  qm_free(struct qm_block*, void* p);
00136 #endif
00137 #ifdef DBG_QM_MALLOC
00138 void* qm_realloc(struct qm_block*, void* p, unsigned long size,
00139                const char* file, const char* func, unsigned int line);
00140 #else
00141 void* qm_realloc(struct qm_block*, void* p, unsigned long size);
00142 #endif
00143 
00144 void  qm_status(struct qm_block*);
00145 void  qm_info(struct qm_block*, struct mem_info*);
00146 
00147 
00148 #ifdef STATISTICS
00149 static inline unsigned long qm_get_size(struct qm_block* qm)
00150 {
00151    return qm->size;
00152 }
00153 static inline unsigned long qm_get_used(struct qm_block* qm)
00154 {
00155    return qm->used;
00156 }
00157 static inline unsigned long qm_get_free(struct qm_block* qm)
00158 {
00159    return qm->size-qm->real_used;
00160 }
00161 static inline unsigned long qm_get_real_used(struct qm_block* qm)
00162 {
00163    return qm->real_used;
00164 }
00165 static inline unsigned long qm_get_max_real_used(struct qm_block* qm)
00166 {
00167    return qm->max_real_used;
00168 }
00169 static inline unsigned long qm_get_frags(struct qm_block* qm)
00170 {
00171    int r;
00172    unsigned long frags;
00173    for(r=0,frags=0;r<QM_HASH_SIZE; r++){
00174       frags+=qm->free_hash[r].no;
00175    }
00176    return frags;
00177 }
00178 #endif /* STATISTICS */
00179 
00180 
00181 #endif

Generated on Thu May 24 08:00:53 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6