f_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
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
00044
00045
00046
00047 #define F_MALLOC_HASH_BITMAP
00048
00049 #ifdef DBG_F_MALLOC
00050 #if defined(__CPU_sparc64) || defined(__CPU_sparc)
00051
00052
00053
00054 #define ROUNDTO sizeof(long long)
00055 #else
00056 #define ROUNDTO sizeof(void*)
00057
00058 #endif
00059 #else
00060 #define ROUNDTO 8UL
00061 #endif
00062 #define MIN_FRAG_SIZE ROUNDTO
00063
00064
00065
00066 #define F_MALLOC_OPTIMIZE_FACTOR 14UL
00067 #define F_MALLOC_OPTIMIZE (1UL<<F_MALLOC_OPTIMIZE_FACTOR)
00068
00069
00070
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
00083
00084
00085
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;
00108 #if defined(DBG_F_MALLOC) || defined(STATISTICS)
00109 unsigned long used;
00110 unsigned long real_used;
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
00184
00185 #endif