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