00001
00021 #ifndef __ATTR_H__
00022 #define __ATTR_H__
00023
00024 #ifdef __cplusplus
00025 extern "C" {
00026 #endif
00027
00031
00032 #include "extra.h"
00033
00037
00041
00042 typedef struct Attr_ManStruct_t_ Attr_Man_t;
00043 struct Attr_ManStruct_t_
00044 {
00045
00046 int nAttrSize;
00047 Extra_MmFixed_t * pManMem;
00048 int nAttrs;
00049 void ** pAttrs;
00050 int fUseInt;
00051
00052 void * pManAttr;
00053 void (*pFuncFreeMan) (void *);
00054 void (*pFuncFreeObj) (void *, void *);
00055 };
00056
00057
00058
00059
00060
00061
00065
00069
00081 static inline Attr_Man_t * Attr_ManAlloc( int nAttrSize, int fManMem )
00082 {
00083 Attr_Man_t * p;
00084 p = ALLOC( Attr_Man_t, 1 );
00085 memset( p, 0, sizeof(Attr_Man_t) );
00086 p->nAttrSize = nAttrSize;
00087 if ( fManMem )
00088 p->pManMem = Extra_MmFixedStart( nAttrSize );
00089 return p;
00090 }
00091
00103 static inline Attr_Man_t * Attr_ManStartInt( int nAttrs )
00104 {
00105 Attr_Man_t * p;
00106 p = Attr_ManAlloc( sizeof(int), 0 );
00107 p->nAttrs = nAttrs;
00108 p->pAttrs = (void **)ALLOC( int, nAttrs );
00109 memset( (int *)p->pAttrs, 0, sizeof(int) * nAttrs );
00110 p->fUseInt = 1;
00111 return p;
00112 }
00113
00125 static inline Attr_Man_t * Attr_ManStartPtr( int nAttrs )
00126 {
00127 Attr_Man_t * p;
00128 p = Attr_ManAlloc( sizeof(void *), 0 );
00129 p->nAttrs = nAttrs;
00130 p->pAttrs = ALLOC( void *, nAttrs );
00131 memset( p->pAttrs, 0, sizeof(void *) * nAttrs );
00132 return p;
00133 }
00134
00146 static inline Attr_Man_t * Attr_ManStartPtrMem( int nAttrs, int nAttrSize )
00147 {
00148 Attr_Man_t * p;
00149 int i;
00150 p = Attr_ManAlloc( nAttrSize, 1 );
00151 p->nAttrs = nAttrs;
00152 p->pAttrs = ALLOC( void *, nAttrs );
00153 for ( i = 0; i < p->nAttrs; i++ )
00154 {
00155 p->pAttrs[i] = Extra_MmFixedEntryFetch( p->pManMem );
00156 memset( p->pAttrs[i], 0, nAttrSize );
00157 }
00158 return p;
00159 }
00160
00172 static inline void Attr_ManStop( Attr_Man_t * p )
00173 {
00174
00175 if ( p->pFuncFreeObj )
00176 {
00177 int i;
00178 if ( p->fUseInt )
00179 {
00180 for ( i = 0; i < p->nAttrs; i++ )
00181 if ( ((int *)p->pAttrs)[i] )
00182 p->pFuncFreeObj( p->pManAttr, (void *)((int *)p->pAttrs)[i] );
00183 }
00184 else
00185 {
00186 for ( i = 0; i < p->nAttrs; i++ )
00187 if ( p->pAttrs[i] )
00188 p->pFuncFreeObj( p->pManAttr, p->pAttrs[i] );
00189 }
00190 }
00191
00192 if ( p->pManAttr && p->pFuncFreeMan )
00193 p->pFuncFreeMan( p->pManAttr );
00194
00195 if ( p->pManMem )
00196 Extra_MmFixedStop( p->pManMem);
00197
00198 FREE( p->pAttrs );
00199 free( p );
00200 }
00201
00213 static inline int Attr_ManReadAttrInt( Attr_Man_t * p, int Id )
00214 {
00215 assert( p->fUseInt );
00216 if ( Id >= p->nAttrs )
00217 return 0;
00218 return ((int *)p->pAttrs)[Id];
00219 }
00220
00232 static inline void * Attr_ManReadAttrPtr( Attr_Man_t * p, int Id )
00233 {
00234 assert( !p->fUseInt );
00235 if ( Id >= p->nAttrs )
00236 return NULL;
00237 return p->pAttrs[Id];
00238 }
00239
00251 static inline void Attr_ManWriteAttrInt( Attr_Man_t * p, int Id, int Attr )
00252 {
00253 assert( p->fUseInt );
00254 ((int *)p->pAttrs)[Id] = Attr;
00255 }
00256
00268 static inline void Attr_ManWriteAttrPtr( Attr_Man_t * p, int Id, void * pAttr )
00269 {
00270 assert( !p->fUseInt );
00271 assert( p->pManMem == NULL );
00272 p->pAttrs[Id] = pAttr;
00273 }
00274
00286 static inline int * Attr_ManFetchSpotInt( Attr_Man_t * p, int Id )
00287 {
00288 assert( p->fUseInt );
00289 if ( Id >= p->nAttrs )
00290 {
00291
00292 int i, nAttrsOld = p->nAttrs;
00293
00294 p->nAttrs = p->nAttrs? 2*p->nAttrs : 1024;
00295 p->pAttrs = realloc( p->pAttrs, sizeof(int) * p->nAttrs );
00296
00297 for ( i = nAttrsOld; i < p->nAttrs; i++ )
00298 ((int *)p->pAttrs)[Id] = 0;
00299 }
00300 return ((int *)p->pAttrs) + Id;
00301 }
00302
00314 static inline void ** Attr_ManFetchSpotPtr( Attr_Man_t * p, int Id )
00315 {
00316 assert( !p->fUseInt );
00317 if ( Id >= p->nAttrs )
00318 {
00319
00320 int i, nAttrsOld = p->nAttrs;
00321
00322 p->nAttrs = p->nAttrs? 2*p->nAttrs : 1024;
00323 p->pAttrs = realloc( p->pAttrs, sizeof(void *) * p->nAttrs );
00324
00325 for ( i = nAttrsOld; i < p->nAttrs; i++ )
00326 p->pAttrs[Id] = NULL;
00327 }
00328
00329 if ( p->pManMem && p->pAttrs[Id] != NULL )
00330 {
00331 p->pAttrs[Id] = Extra_MmFixedEntryFetch( p->pManMem );
00332 memset( p->pAttrs[Id], 0, p->nAttrSize );
00333 }
00334 return p->pAttrs + Id;
00335 }
00336
00337
00349 static inline int Attr_ManFetchAttrInt( Attr_Man_t * p, int Id )
00350 {
00351 return *Attr_ManFetchSpotInt( p, Id );
00352 }
00353
00365 static inline void * Attr_ManFetchAttrPtr( Attr_Man_t * p, int Id )
00366 {
00367 return *Attr_ManFetchSpotPtr( p, Id );
00368 }
00369
00381 static inline void Attr_ManSetAttrInt( Attr_Man_t * p, int Id, int Attr )
00382 {
00383 *Attr_ManFetchSpotInt( p, Id ) = Attr;
00384 }
00385
00397 static inline void Attr_ManSetAttrPtr( Attr_Man_t * p, int Id, void * pAttr )
00398 {
00399 assert( p->pManMem == NULL );
00400 *Attr_ManFetchSpotPtr( p, Id ) = pAttr;
00401 }
00402
00403
00404
00405 #ifdef __cplusplus
00406 }
00407 #endif
00408
00409 #endif
00410
00414