q_malloc.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #if !defined(q_malloc_h) && !defined(F_MALLOC)
00034 #define q_malloc_h
00035
00036 #include "meminfo.h"
00037
00038
00039 #ifdef DBG_QM_MALLOC
00040 #if defined(__CPU_sparc64) || defined(__CPU_sparc)
00041
00042
00043
00044 #define ROUNDTO sizeof(long long)
00045 #else
00046 #define ROUNDTO sizeof(void*)
00047
00048 #endif
00049 #else
00050 #define ROUNDTO 16UL
00051
00052
00053
00054 #endif
00055 #define MIN_FRAG_SIZE ROUNDTO
00056
00057
00058
00059 #define QM_MALLOC_OPTIMIZE_FACTOR 14UL
00060 #define QM_MALLOC_OPTIMIZE ((unsigned long)(1UL<<QM_MALLOC_OPTIMIZE_FACTOR))
00061
00062
00063
00064
00065 #define QM_HASH_SIZE ((unsigned long)(QM_MALLOC_OPTIMIZE/ROUNDTO + \
00066 (sizeof(long)*8-QM_MALLOC_OPTIMIZE_FACTOR)+1))
00067
00068
00069
00070
00071
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;
00109 unsigned long used;
00110 unsigned long real_used;
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
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
00179
00180
00181 #endif