Doxygen Source Code Documentation
Main Page Alphabetical List Data Structures File List Data Fields Globals Search
niml_registry.c
Go to the documentation of this file.00001 #include "niml_private.h"
00002
00003 #undef INLINE
00004 #ifdef __GNUC__
00005 # define INLINE inline
00006 #else
00007 # define INLINE
00008 #endif
00009
00010
00011
00012
00013 typedef struct {
00014 char idc[32];
00015 char ipt[32];
00016 size_t vlen ;
00017 int flags ;
00018 void *vpt ;
00019 char *name ;
00020 } registry_entry ;
00021
00022
00023
00024 #undef NIREG_PRIVATE_MALLOC
00025 #define NIREG_PRIVATE_MALLOC (1<<0)
00026
00027 #undef NIREG_isprivate
00028 #define NIREG_isprivate(rr) (((rr)->flags & NIREG_PRIVATE_MALLOC) != 0)
00029
00030 #undef NIREG_free
00031 #define NIREG_free(rr) \
00032 do{ if( !NIREG_isprivate(rr) ) free( (rr)->vpt ) ; \
00033 free((void *)(rr)->name) ; \
00034 free((void *)(rr)) ; \
00035 } while(0)
00036
00037
00038
00039
00040 static Htable *registry_htable_idc = NULL ;
00041 static Htable *registry_htable_ipt = NULL ;
00042
00043
00044
00045
00046 static INLINE void vpt_to_char( void *vpt , char *cpt )
00047 {
00048 sprintf( cpt , "%p" , vpt ) ;
00049 }
00050
00051
00052
00053
00054
00055 static INLINE void * char_to_vpt( char *cpt )
00056 {
00057 void *vpt=NULL ;
00058 if( cpt == NULL ) return NULL ;
00059 sscanf(cpt,"%p",&vpt) ;
00060 return vpt ;
00061 }
00062
00063
00064
00065
00066
00067 static void init_registry(void)
00068 {
00069 if( registry_htable_ipt == NULL ){
00070 registry_htable_idc = new_Htable(131) ;
00071 registry_htable_ipt = new_Htable(131) ;
00072 }
00073 return ;
00074 }
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087 void * NI_registry_malloc( char *idcode , char *name , size_t len )
00088 {
00089 void *vpt ;
00090 int lll ;
00091 registry_entry *rent ;
00092
00093 init_registry() ;
00094
00095 if( idcode == NULL || *idcode == '\0' ) return NULL ;
00096
00097
00098
00099 vpt = findin_Htable( idcode , registry_htable_idc ) ;
00100 if( vpt != NULL ) return NULL ;
00101
00102
00103
00104 lll = (len == 0) ? 4 : len ;
00105 vpt = calloc(1,lll) ;
00106 if( vpt == NULL ) return NULL ;
00107
00108 if( len == 0 ){ char *cpt=(char *)vpt; *cpt = '\0'; }
00109
00110
00111
00112 rent = calloc(1,sizeof(registry_entry)) ;
00113 NI_strncpy( rent->idc , idcode , 32 ) ;
00114 rent->vpt = vpt ;
00115 rent->vlen = len ;
00116 vpt_to_char( vpt , rent->ipt ) ;
00117 if( name == NULL ) name = "\0" ;
00118 rent->name = strdup(name) ;
00119 rent->flags = 0 ;
00120
00121
00122
00123 addto_Htable( rent->idc , (void *)rent , registry_htable_idc ) ;
00124 addto_Htable( rent->ipt , (void *)rent , registry_htable_ipt ) ;
00125
00126 return vpt ;
00127 }
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138 void * NI_registry_add( char *idcode , char *name , void *vpt )
00139 {
00140 void *xpt ;
00141 registry_entry *rent ;
00142
00143 init_registry() ;
00144
00145 if( idcode == NULL || *idcode == '\0' || vpt == NULL ) return NULL ;
00146
00147
00148
00149 xpt = findin_Htable( idcode , registry_htable_idc ) ;
00150 if( xpt != NULL ) return NULL ;
00151
00152
00153
00154 rent = calloc(1,sizeof(registry_entry)) ;
00155 NI_strncpy( rent->idc , idcode , 32 ) ;
00156 rent->vpt = vpt ;
00157 rent->vlen = 0 ;
00158 vpt_to_char( vpt , rent->ipt ) ;
00159 if( name == NULL ) name = "\0" ;
00160 rent->name = strdup(name) ;
00161 rent->flags = NIREG_PRIVATE_MALLOC ;
00162
00163
00164
00165 addto_Htable( rent->idc , (void *)rent , registry_htable_idc ) ;
00166 addto_Htable( rent->ipt , (void *)rent , registry_htable_ipt ) ;
00167
00168 return vpt ;
00169 }
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182 void * NI_registry_realloc( void *vpt , size_t newlen )
00183 {
00184 char ipt[32] ;
00185 void *vpt_new ;
00186 int lll ;
00187 registry_entry *rent ;
00188
00189 if( vpt == NULL || registry_htable_ipt == NULL ) return NULL ;
00190
00191
00192
00193 vpt_to_char( vpt , ipt ) ;
00194 rent = (registry_entry *) findin_Htable( ipt , registry_htable_ipt ) ;
00195 if( rent == NULL ) return NULL ;
00196 if( NIREG_isprivate(rent) ) return NULL ;
00197
00198 lll = (newlen == 0) ? 4 : newlen ;
00199 vpt_new = realloc( vpt , lll ) ;
00200 if( vpt_new == NULL ) return NULL ;
00201 if( vpt_new == vpt ) return vpt ;
00202
00203
00204
00205
00206 removefrom_Htable( ipt , registry_htable_ipt ) ;
00207
00208 rent->vpt = vpt_new ;
00209 rent->vlen = newlen ;
00210 vpt_to_char( vpt , rent->ipt ) ;
00211 addto_Htable( rent->ipt , (void *)rent , registry_htable_ipt ) ;
00212
00213 return vpt_new ;
00214 }
00215
00216
00217
00218
00219
00220
00221 void * NI_registry_replace( void *vpt , void *vpt_new )
00222 {
00223 char ipt[32] ;
00224 registry_entry *rent ;
00225
00226 if( vpt == NULL || vpt_new == NULL ||
00227 registry_htable_ipt == NULL ) return NULL ;
00228
00229 if( vpt == vpt_new ) return vpt ;
00230
00231
00232
00233 vpt_to_char( vpt , ipt ) ;
00234 rent = (registry_entry *) findin_Htable( ipt , registry_htable_ipt ) ;
00235 if( rent == NULL ) return NULL ;
00236
00237 if( !NIREG_isprivate(rent) ) free((void *)vpt) ;
00238
00239
00240
00241
00242 removefrom_Htable( ipt , registry_htable_ipt ) ;
00243
00244 rent->vpt = vpt_new ;
00245 rent->vlen = 0 ;
00246 vpt_to_char( vpt , rent->ipt ) ;
00247 addto_Htable( rent->ipt , (void *)rent , registry_htable_ipt ) ;
00248 rent->flags = NIREG_PRIVATE_MALLOC ;
00249
00250 return vpt_new ;
00251 }
00252
00253
00254
00255
00256 void NI_registry_free( void *vpt )
00257 {
00258 char ipt[32] ;
00259 registry_entry *rent ;
00260
00261 if( vpt == NULL || registry_htable_ipt == NULL ) return ;
00262
00263
00264
00265 vpt_to_char( vpt , ipt ) ;
00266 rent = (registry_entry *) findin_Htable( ipt , registry_htable_ipt ) ;
00267 if( rent == NULL ) return ;
00268
00269 removefrom_Htable( rent->ipt , registry_htable_ipt ) ;
00270 removefrom_Htable( rent->idc , registry_htable_idc ) ;
00271 NIREG_free( rent ) ;
00272 return ;
00273 }
00274
00275
00276
00277
00278 void * NI_registry_idcode_to_ptr( char *idcode )
00279 {
00280 registry_entry *rent ;
00281
00282 rent = (registry_entry *) findin_Htable( idcode , registry_htable_idc ) ;
00283 if( rent == NULL ) return NULL ;
00284 return rent->vpt ;
00285 }
00286
00287
00288
00289
00290
00291
00292
00293 size_t NI_registry_idcode_to_len( char *idcode )
00294 {
00295 registry_entry *rent ;
00296
00297 rent = (registry_entry *) findin_Htable( idcode , registry_htable_idc ) ;
00298 if( rent == NULL ) return 0 ;
00299 return rent->vlen ;
00300 }
00301
00302
00303
00304
00305
00306
00307
00308 size_t NI_registry_ptr_to_len( void *vpt )
00309 {
00310 char ipt[32] ;
00311 registry_entry *rent ;
00312
00313 if( vpt == NULL || registry_htable_ipt == NULL ) return ;
00314
00315 vpt_to_char( vpt , ipt ) ;
00316 rent = (registry_entry *) findin_Htable( ipt , registry_htable_ipt ) ;
00317 if( rent == NULL ) return 0 ;
00318 return rent->vlen ;
00319 }
00320
00321
00322
00323
00324
00325
00326
00327 char * NI_registry_idcode_to_name( char *idcode )
00328 {
00329 registry_entry *rent ;
00330
00331 rent = (registry_entry *) findin_Htable( idcode , registry_htable_idc ) ;
00332 if( rent == NULL ) return NULL ;
00333 return rent->name ;
00334 }
00335
00336
00337
00338
00339
00340
00341 char * NI_registry_ptr_to_idcode( void *vpt )
00342 {
00343 char ipt[32] ;
00344 registry_entry *rent ;
00345
00346 if( vpt == NULL || registry_htable_ipt == NULL ) return ;
00347
00348 vpt_to_char( vpt , ipt ) ;
00349 rent = (registry_entry *) findin_Htable( ipt , registry_htable_ipt ) ;
00350 if( rent == NULL ) return ;
00351 return rent->idc ;
00352 }
00353
00354
00355
00356
00357 char * NI_registry_ptr_to_name( void *vpt )
00358 {
00359 char ipt[32] ;
00360 registry_entry *rent ;
00361
00362 if( vpt == NULL || registry_htable_ipt == NULL ) return ;
00363
00364 vpt_to_char( vpt , ipt ) ;
00365 rent = (registry_entry *) findin_Htable( ipt , registry_htable_ipt ) ;
00366 if( rent == NULL ) return ;
00367 return rent->name ;
00368 }
00369
00370
00371
00372
00373 void NI_registry_idcode_altername( char *idcode , char *newname )
00374 {
00375 registry_entry *rent ;
00376
00377 rent = (registry_entry *) findin_Htable( idcode , registry_htable_idc ) ;
00378 if( rent == NULL ) return ;
00379 free((void *)rent->name) ;
00380 if( newname == NULL ) newname = "\0" ;
00381 rent->name = strdup(newname) ;
00382 return ;
00383 }
00384
00385
00386
00387
00388 void NI_registry_ptr_altername( void *vpt , char *newname )
00389 {
00390 char ipt[32] ;
00391 registry_entry *rent ;
00392
00393 if( vpt == NULL || registry_htable_ipt == NULL ) return ;
00394
00395 vpt_to_char( vpt , ipt ) ;
00396 rent = (registry_entry *) findin_Htable( ipt , registry_htable_ipt ) ;
00397 if( rent == NULL ) return ;
00398 free((void *)rent->name) ;
00399 if( newname == NULL ) newname = "\0" ;
00400 rent->name = strdup(newname) ;
00401 return ;
00402 }