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 #ifndef STRUCTURE_MANAGER
00027
00028 #define STRUCTURE_MANAGER
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
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150 #define PRIVATE_SM 1
00151 #define SHARED_SM 0
00152
00153
00154
00155
00156
00157 #ifdef MULTI_THREAD
00158 #define SET_TRIE_ALLOCATION_TYPE_TIP(pTIF) \
00159 if (isPrivateTIF(pTIF)) { \
00160 smBTN = private_smTableBTN; \
00161 smBTHT = private_smTableBTHT; \
00162 threads_current_sm = PRIVATE_SM; \
00163 } else { \
00164 smBTN = &smTableBTN; \
00165 smBTHT = &smTableBTHT; \
00166 threads_current_sm = SHARED_SM; \
00167 }
00168
00169 #define SET_TRIE_ALLOCATION_TYPE_PSC(pPSC) \
00170 if (get_shared(pPSC)) { \
00171 smBTN = &smTableBTN; \
00172 smBTHT = &smTableBTHT; \
00173 threads_current_sm = SHARED_SM; \
00174 } else { \
00175 smBTN = private_smTableBTN; \
00176 smBTHT = private_smTableBTHT; \
00177 threads_current_sm = PRIVATE_SM; \
00178 }
00179
00180 #define SET_TRIE_ALLOCATION_TYPE_SF(pSF) \
00181 if (!(pSF->sf_type & SHARED_PRIVATE_MASK)) { \
00182 smBTN = private_smTableBTN; \
00183 smBTHT = private_smTableBTHT; \
00184 threads_current_sm = PRIVATE_SM; \
00185 } else { \
00186 smBTN = &smTableBTN; \
00187 smBTHT = &smTableBTHT; \
00188 threads_current_sm = SHARED_SM; \
00189 }
00190 #else
00191 #define SET_TRIE_ALLOCATION_TYPE_TIP(pTIF)
00192 #define SET_TRIE_ALLOCATION_TYPE_SF(pSF)
00193 #define SET_TRIE_ALLOCATION_TYPE_PSC(pPSC)
00194 #endif
00195
00196 typedef struct Structure_Manager {
00197 struct {
00198 void *pBlock;
00199 void *pNextStruct;
00200 void *pLastStruct;
00201 } cur_block;
00202 struct {
00203 size_t size;
00204 counter num;
00205 char *name;
00206 } struct_desc;
00207 struct {
00208 void *alloc;
00209 void *dealloc;
00210 } struct_lists;
00211 } Structure_Manager;
00212 typedef struct Structure_Manager *SMptr;
00213
00214
00215
00216
00217 #define SM_CurBlock(SM) ((SM).cur_block.pBlock)
00218 #define SM_NextStruct(SM) ((SM).cur_block.pNextStruct)
00219 #define SM_LastStruct(SM) ((SM).cur_block.pLastStruct)
00220 #define SM_StructSize(SM) ((SM).struct_desc.size)
00221 #define SM_StructsPerBlock(SM) ((SM).struct_desc.num)
00222 #define SM_StructName(SM) ((SM).struct_desc.name)
00223 #define SM_AllocList(SM) ((SM).struct_lists.alloc)
00224 #define SM_FreeList(SM) ((SM).struct_lists.dealloc)
00225
00226 #define SM_NewBlockSize(SM) \
00227 ( sizeof(void *) + SM_StructSize(SM) * SM_StructsPerBlock(SM) )
00228
00229 #define SM_CurBlockIsDepleted(SM) \
00230 ( IsNULL(SM_CurBlock(SM)) || (SM_NextStruct(SM) > SM_LastStruct(SM)) )
00231
00232 #define SM_NumStructsLeftInBlock(SM) \
00233 ( ! SM_CurBlockIsDepleted(SM) \
00234 ? ( ((char *)SM_LastStruct(SM) - (char *)SM_NextStruct(SM)) \
00235 / SM_StructSize(SM) + 1 ) \
00236 : 0 )
00237
00238
00239 #define SM_AllocateFree(SM,pNewStruct) \
00240 pNewStruct = SM_FreeList(SM); \
00241 SM_FreeList(SM) = SMFL_NextFreeStruct(SM_FreeList(SM))
00242
00243 #define SM_AllocateFromBlock(SM,pNewStruct) \
00244 pNewStruct = SM_NextStruct(SM); \
00245 SM_NextStruct(SM) = SMBlk_NextStruct(SM_NextStruct(SM),SM_StructSize(SM))
00246
00247
00248
00249 extern void smPrint(Structure_Manager, char *);
00250 extern void smAllocateBlock(Structure_Manager *);
00251 extern void smFreeBlocks(Structure_Manager *);
00252 extern xsbBool smIsValidStructRef(Structure_Manager, void *);
00253 extern xsbBool smIsAllocatedStruct(Structure_Manager, void *);
00254 extern xsbBool smIsAllocatedStructRef(Structure_Manager, void *);
00255
00256
00257
00258
00259
00260 #define SMFL_NextFreeStruct(pFreeStruct) ( *(void **)(pFreeStruct) )
00261
00262
00263
00264
00265 #define SMBlk_NextBlock(pBlock) ( *(void **)(pBlock) )
00266
00267 #define SMBlk_FirstStruct(pBlock) ( (char *)pBlock + sizeof(void *) )
00268
00269 #define SMBlk_LastStruct(pBlock,StructSize,StructsPerBlock) \
00270 ( SMBlk_FirstStruct(pBlock) + StructSize * (StructsPerBlock - 1) )
00271
00272 #define SMBlk_NextStruct(pStruct,StructSize) ( (char *)pStruct + StructSize )
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282 #define SM_InitDecl(StructType,StructsPerBlock,NameString) { \
00283 {NULL, NULL, NULL}, \
00284 {sizeof(StructType), StructsPerBlock, NameString}, \
00285 {NULL, NULL} \
00286 }
00287
00288 #define SM_InitDeclDyna(StructPtr,StructType,StructsPerBlock,NameString) \
00289 (StructPtr->cur_block).pBlock = NULL; \
00290 (StructPtr->cur_block).pNextStruct = NULL; \
00291 (StructPtr->cur_block).pLastStruct = NULL; \
00292 (StructPtr->struct_desc).size = sizeof(StructType); \
00293 (StructPtr->struct_desc).num = StructsPerBlock; \
00294 (StructPtr->struct_desc).name = NameString; \
00295 (StructPtr->struct_lists).alloc = NULL; \
00296 (StructPtr->struct_lists).dealloc = NULL;
00297
00298
00299
00300
00301
00302
00303
00304
00305 #define SM_AllocateSharedStruct(SM,pStruct) { \
00306 SYS_MUTEX_LOCK( MUTEX_SM ); \
00307 if ( IsNonNULL(SM_FreeList(SM)) ) { \
00308 SM_AllocateFree(SM,pStruct); \
00309 } \
00310 else { \
00311 if ( SM_CurBlockIsDepleted(SM) ) { \
00312 smAllocateBlock(&SM); \
00313 } \
00314 SM_AllocateFromBlock(SM,pStruct); \
00315 } \
00316 SYS_MUTEX_UNLOCK( MUTEX_SM ); \
00317 }
00318
00319 #define SM_AllocateStruct(SM,pStruct) { \
00320 \
00321 if ( IsNonNULL(SM_FreeList(SM)) ) { \
00322 SM_AllocateFree(SM,pStruct); \
00323 } \
00324 else { \
00325 if ( SM_CurBlockIsDepleted(SM) ) \
00326 smAllocateBlock(&SM); \
00327 SM_AllocateFromBlock(SM,pStruct); \
00328 } \
00329 }
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343 #ifdef MULTI_THREAD
00344 #define SM_DeallocateSharedStructList(SM,pHead,pTail) { \
00345 void *pStruct = pHead; \
00346 while (pStruct != pTail) { \
00347 *(((int *)pStruct)+1) = FREE_TRIE_NODE_MARK; \
00348 pStruct = *(void **)pStruct; \
00349 } \
00350 *(((int *)pStruct)+1) = FREE_TRIE_NODE_MARK; \
00351 SYS_MUTEX_LOCK( MUTEX_SM ); \
00352 SMFL_NextFreeStruct(pTail) = SM_FreeList(SM); \
00353 SM_FreeList(SM) = pHead; \
00354 SYS_MUTEX_UNLOCK( MUTEX_SM ); \
00355 }
00356 #else
00357 #define SM_DeallocateSharedStructList(SM,pHead,pTail) \
00358 SM_DeallocateStructList(SM,pHead,pTail)
00359 #endif
00360
00361 #define SM_DeallocateStructList(SM,pHead,pTail) { \
00362 void *pStruct = pHead; \
00363 while (pStruct != pTail) { \
00364 *(((int *)pStruct)+1) = FREE_TRIE_NODE_MARK; \
00365 pStruct = *(void **)pStruct; \
00366 } \
00367 *(((int *)pStruct)+1) = FREE_TRIE_NODE_MARK; \
00368 SMFL_NextFreeStruct(pTail) = SM_FreeList(SM); \
00369 SM_FreeList(SM) = pHead; \
00370 }
00371
00372 #define SM_DeallocateSharedStruct(SM,pStruct) \
00373 SM_DeallocateSharedStructList(SM,pStruct,pStruct)
00374
00375 #define SM_DeallocateStruct(SM,pStruct) \
00376 SM_DeallocateStructList(SM,pStruct,pStruct)
00377
00378 #ifdef MULTI_THREAD
00379 #define SM_DeallocatePossSharedStruct(SM,pStruct) \
00380 if (threads_current_sm == PRIVATE_SM) { \
00381 SM_DeallocateStruct(SM,pStruct); \
00382 } else { \
00383 SM_DeallocateSharedStruct(SM,pStruct); \
00384 }
00385 #else
00386 #define SM_DeallocatePossSharedStruct(SM,pStruct) \
00387 SM_DeallocateStruct(SM,pStruct)
00388 #endif
00389
00390
00391
00392
00393
00394
00395
00396
00397 #define SM_CurrentCapacity(SM,NumBlocks,NumStructs) { \
00398 \
00399 void *pBlock; \
00400 \
00401 NumBlocks = 0; \
00402 for ( pBlock = SM_CurBlock(SM); IsNonNULL(pBlock); \
00403 pBlock = SMBlk_NextBlock(pBlock) ) \
00404 NumBlocks++; \
00405 NumStructs = NumBlocks * SM_StructsPerBlock(SM); \
00406 }
00407
00408
00409
00410
00411
00412
00413
00414 #define SM_CountFreeStructs(SM,NumFreeStructs) { \
00415 \
00416 void *pStruct; \
00417 \
00418 if ( IsNonNULL(SM_CurBlock(SM)) ) { \
00419 NumFreeStructs = SM_NumStructsLeftInBlock(SM); \
00420 for ( pStruct = SM_FreeList(SM); IsNonNULL(pStruct); \
00421 pStruct = SMFL_NextFreeStruct(pStruct) ) \
00422 NumFreeStructs++; \
00423 } \
00424 else \
00425 NumFreeStructs = 0; \
00426 }
00427
00428
00429
00430
00431
00432
00433
00434 #define SM_RawUsage(SM,UsageInBytes) { \
00435 \
00436 void *pBlock; \
00437 \
00438 UsageInBytes = 0; \
00439 for ( pBlock = SM_CurBlock(SM); IsNonNULL(pBlock); \
00440 pBlock = SMBlk_NextBlock(pBlock) ) \
00441 UsageInBytes = UsageInBytes + SM_NewBlockSize(SM); \
00442 }
00443
00444
00445
00446
00447
00448
00449
00450 #define SM_ReleaseResources(SM) smFreeBlocks(&SM)
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460 #define SM_AddToAllocList_SL(SM,pRecord,LinkFieldMacro) { \
00461 LinkFieldMacro(pRecord) = SM_AllocList(SM); \
00462 SM_AllocList(SM) = pRecord; \
00463 }
00464
00465 #define SM_AddToAllocList_DL(SM,pRecord,PrevFieldMacro,NextFieldMacro) { \
00466 PrevFieldMacro(pRecord) = NULL; \
00467 NextFieldMacro(pRecord) = SM_AllocList(SM); \
00468 SM_AllocList(SM) = pRecord; \
00469 if ( IsNonNULL(NextFieldMacro(pRecord)) ) \
00470 PrevFieldMacro(NextFieldMacro(pRecord)) = pRecord; \
00471 }
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481 #define SM_RemoveFromAllocList_SL(SM,pRecord,LinkFieldMacro,RecordType) { \
00482 \
00483 RecordType pCur, pPrev; \
00484 \
00485 for ( pPrev = NULL, pCur = SM_AllocList(SM); \
00486 IsNonNULL(pCur); \
00487 pPrev = pCur, pCur = LinkFieldMacro(pCur) ) \
00488 if ( pCur == pRecord ) \
00489 break; \
00490 if ( IsNonNULL(pCur) ) { \
00491 if ( IsNonNULL(pPrev) ) \
00492 LinkFieldMacro(pPrev) = LinkFieldMacro(pCur); \
00493 else \
00494 SM_AllocList(SM) = LinkFieldMacro(pCur); \
00495 LinkFieldMacro(pCur) = NULL; \
00496 } \
00497 else \
00498 xsb_warn("Record not present in given Structure Manager: %s", \
00499 SM_StructName(SM)); \
00500 }
00501
00502 #define SM_RemoveFromAllocList_DL(SM,pRecord,PrevFieldMacro,NextFieldMacro) { \
00503 if ( IsNonNULL(PrevFieldMacro(pRecord)) ) \
00504 NextFieldMacro(PrevFieldMacro(pRecord)) = NextFieldMacro(pRecord); \
00505 else { \
00506 if ( SM_AllocList(SM) == pRecord ) \
00507 SM_AllocList(SM) = NextFieldMacro(pRecord); \
00508 else { \
00509 xsb_abort("Record not present in given Structure Manager: %s", \
00510 SM_StructName(SM)); \
00511 } \
00512 } \
00513 if ( IsNonNULL(NextFieldMacro(pRecord)) ) \
00514 PrevFieldMacro(NextFieldMacro(pRecord)) = PrevFieldMacro(pRecord); \
00515 NextFieldMacro(pRecord) = PrevFieldMacro(pRecord) = NULL; \
00516 }
00517
00518
00519
00520 #endif