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
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058 #ifdef _KEEP_RCS_ID
00059 static char *source_file = __FILE__;
00060 static char *rcs_id = "$Source: /scratch/mee/2.4-65/kpro64-pending/be/cg/SCCS/s.bb_map.cxx $ $Revision: 1.2 $";
00061 #endif
00062
00063 #include "defs.h"
00064 #include "errors.h"
00065 #include "mempool.h"
00066 #include "bb.h"
00067 #include "bb_map.h"
00068
00069 #define BB_EXPANSION_FACTOR 2
00070
00071
00072 static BB_MAP free_maps;
00073
00074
00075 #define BB_MAP_next(map) ((map)->values.ptr[0])
00076
00077
00078 void BB_MAP_Init(void)
00079 {
00080 free_maps = NULL;
00081 }
00082
00083
00084 void BB_MAP_Delete(BB_MAP map)
00085 {
00086 DevAssert(!map->deleted, ("BB_MAP 0x%x already deleted", map));
00087
00088 BB_MAP_next(map) = free_maps;
00089 map->deleted = TRUE;
00090 free_maps = map;
00091 }
00092
00093
00094 inline UINT16 sizeof_kind(_BB_MAP_KIND kind)
00095 {
00096 switch (kind) {
00097 case _BB_MAP_PTR:
00098 return sizeof(void *);
00099 case _BB_MAP_I32:
00100 return sizeof(mINT32);
00101 case _BB_MAP_I64:
00102 return sizeof(mINT64);
00103 default:
00104 FmtAssert(FALSE, ("sizeof_kind passed bad <kind>"));
00105 }
00106
00107
00108
00109
00110
00111 return 0;
00112 }
00113
00114
00115 BB_MAP BB_MAP_create_kind(_BB_MAP_KIND kind)
00116 {
00117 BB_MAP result, prev;
00118 UINT32 length = (BB_MAP_idx_max + 1) * BB_EXPANSION_FACTOR;
00119
00120
00121 prev = NULL;
00122 for (result = free_maps; result; result = (BB_MAP) BB_MAP_next(result)) {
00123 if (sizeof_kind(kind) == sizeof_kind(result->kind)) {
00124 if (prev)
00125 BB_MAP_next(prev) = BB_MAP_next(result);
00126 else
00127 free_maps = (BB_MAP) BB_MAP_next(result);
00128 result->deleted = FALSE;
00129 result->kind = kind;
00130 result->gen += 1;
00131 if (result->gen == 0) {
00132
00133 DevWarn("(Performance) BB_MAP gen overflow - zeroing.");
00134 BZERO(result->value_gens, sizeof(mUINT32) * result->length);
00135 BZERO(result->values.ptr, sizeof_kind(kind) * result->length);
00136 }
00137 return result;
00138 }
00139 prev = result;
00140 }
00141
00142
00143 result = TYPE_P_ALLOC(struct bb_map);
00144 result->deleted = FALSE;
00145 result->kind = kind;
00146 result->length = length;
00147 result->gen = 0;
00148 result->value_gens = TYPE_P_ALLOC_N(mUINT32, length);
00149 switch (kind) {
00150 case _BB_MAP_PTR:
00151 result->values.ptr = TYPE_P_ALLOC_N(void *, length);
00152 break;
00153 case _BB_MAP_I32:
00154 result->values.i32 = TYPE_P_ALLOC_N(mINT32, length);
00155 break;
00156 case _BB_MAP_I64:
00157 result->values.i64 = TYPE_P_ALLOC_N(mINT64, length);
00158 break;
00159 default:
00160 FmtAssert(FALSE, ("BB_MAP_create_kind passed bad <kind>"));
00161 }
00162
00163 return result;
00164 }
00165
00166
00167 void BB_MAP_grow(BB_MAP map, BB *bb)
00168 {
00169 INT32 new_length = (BB_MAP_idx_max + 1) * BB_EXPANSION_FACTOR;
00170
00171 if (BB_map_idx(bb) > new_length) {
00172 DevWarn("BB_MAP_idx_max not up to date");
00173 new_length = BB_map_idx(bb) + 1;
00174 }
00175
00176
00177
00178
00179
00180 map->value_gens = TYPE_MEM_POOL_REALLOC_N(mUINT32, &MEM_phase_pool,
00181 map->value_gens, map->length,
00182 new_length);
00183
00184 switch (map->kind) {
00185 case _BB_MAP_PTR:
00186 map->values.ptr =
00187 TYPE_MEM_POOL_REALLOC_N(void *, &MEM_phase_pool, map->values.ptr,
00188 map->length, new_length);
00189 break;
00190 case _BB_MAP_I32:
00191 map->values.i32 =
00192 TYPE_MEM_POOL_REALLOC_N(INT32, &MEM_phase_pool, map->values.i32,
00193 map->length, new_length);
00194 break;
00195 case _BB_MAP_I64:
00196 map->values.i64 =
00197 TYPE_MEM_POOL_REALLOC_N(INT64, &MEM_phase_pool, map->values.i64,
00198 map->length, new_length);
00199 break;
00200 default:
00201 FmtAssert(FALSE, ("BB_MAP_grow passed map with bad <kind>"));
00202 }
00203
00204 map->length = new_length;
00205 }