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 #include "xsb_config.h"
00027 #include "xsb_debug.h"
00028
00029 #include <stdio.h>
00030 #include <stdlib.h>
00031
00032 #include "auxlry.h"
00033 #include "struct_manager.h"
00034 #include "cell_xsb.h"
00035 #include "error_xsb.h"
00036 #include "debug_xsb.h"
00037 #include "flags_xsb.h"
00038 #include "memory_xsb.h"
00039
00040
00041
00042 void smPrint(Structure_Manager smRecord, char *string) {
00043
00044 void *pBlock;
00045 counter nBlocks;
00046
00047 nBlocks = 0;
00048 for ( pBlock = SM_CurBlock(smRecord); IsNonNULL(pBlock);
00049 pBlock = SMBlk_NextBlock(pBlock) )
00050 nBlocks++;
00051
00052 fprintf(stddbg,
00053 " Structure Manager for %s (%s)\n"
00054 "\tCurBlock: %p\t\tTotal Blocks: %u\n"
00055 "\tNextStr: %p\t\tFree List: %p\n"
00056 "\tLastStr: %p\t\tAlloc List: %p\n"
00057 "\tStructs per block: %u\t\tStruct size: %u bytes\n",
00058 SM_StructName(smRecord), string,
00059 SM_CurBlock(smRecord), nBlocks,
00060 SM_NextStruct(smRecord), SM_FreeList(smRecord),
00061 SM_LastStruct(smRecord), SM_AllocList(smRecord),
00062 SM_StructsPerBlock(smRecord), SM_StructSize(smRecord));
00063 }
00064
00065
00066
00067
00068
00069
00070
00071
00072 void smAllocateBlock(Structure_Manager *pSM) {
00073
00074 void *pNewBlock;
00075
00076 dbg_smPrint(LOG_STRUCT_MANAGER, *pSM,"before block allocation");
00077 pNewBlock = mem_alloc(SM_NewBlockSize(*pSM),TABLE_SPACE);
00078 if ( IsNULL(pNewBlock) )
00079 xsb_abort("[smAllocateBlock] Out of memory in allocation of %s block\n",
00080 SM_StructName(*pSM));
00081 SMBlk_NextBlock(pNewBlock) = SM_CurBlock(*pSM);
00082 SM_CurBlock(*pSM) = pNewBlock;
00083 SM_NextStruct(*pSM) = SMBlk_FirstStruct(pNewBlock);
00084 SM_LastStruct(*pSM) = SMBlk_LastStruct(pNewBlock,
00085 SM_StructSize(*pSM),
00086 SM_StructsPerBlock(*pSM));
00087 dbg_smPrint(LOG_STRUCT_MANAGER, *pSM,"after block allocation");
00088 }
00089
00090
00091
00092
00093
00094
00095
00096 void smFreeBlocks(Structure_Manager *pSM) {
00097
00098 void *pCurBlock, *pNextBlock;
00099
00100 pCurBlock = SM_CurBlock(*pSM);
00101 while ( IsNonNULL(pCurBlock) ) {
00102 pNextBlock = SMBlk_NextBlock(pCurBlock);
00103 mem_dealloc(pCurBlock,SM_NewBlockSize(*pSM),TABLE_SPACE);
00104 pCurBlock = pNextBlock;
00105 }
00106 SM_CurBlock(*pSM) = SM_NextStruct(*pSM) = SM_LastStruct(*pSM) = NULL;
00107 SM_AllocList(*pSM) = SM_FreeList(*pSM) = NULL;
00108 }
00109
00110 void smPrintBlocks(Structure_Manager *pSM) {
00111
00112 void *pCurBlock, *pNextBlock;
00113
00114 printf("blocks for SM %p size %d\n",pSM,SM_NewBlockSize(*pSM));
00115 pCurBlock = SM_CurBlock(*pSM);
00116 while ( IsNonNULL(pCurBlock) ) {
00117 printf("Block %p\n",pCurBlock);
00118 pNextBlock = SMBlk_NextBlock(pCurBlock);
00119
00120 pCurBlock = pNextBlock;
00121 }
00122 }
00123
00124
00125
00126
00127
00128
00129
00130
00131 xsbBool smIsValidStructRef(Structure_Manager smRecord, void *ptr) {
00132
00133 void *pBlock, *firstStruct, *lastStruct;
00134 size_t structSize;
00135
00136
00137 structSize = SM_StructSize(smRecord);
00138
00139 for ( pBlock = SM_CurBlock(smRecord); IsNonNULL(pBlock);
00140 pBlock = SMBlk_NextBlock(pBlock) ) {
00141
00142 firstStruct = SMBlk_FirstStruct(pBlock);
00143 lastStruct =
00144 SMBlk_LastStruct(pBlock,structSize,SM_StructsPerBlock(smRecord));
00145
00146
00147
00148 if ( (firstStruct <= ptr) && (ptr <= lastStruct) ) {
00149
00150
00151
00152 if ( (((char *)ptr - (char *)firstStruct) MOD structSize) == 0 )
00153 return TRUE;
00154 else
00155 return FALSE;
00156 }
00157 }
00158 return FALSE;
00159 }
00160
00161
00162
00163
00164
00165
00166
00167
00168 xsbBool smIsAllocatedStruct(Structure_Manager smRecord, void *pStruct) {
00169
00170 void *freeStruct;
00171
00172
00173
00174 if ( (SM_NextStruct(smRecord) <= pStruct) &&
00175 (pStruct <= SM_LastStruct(smRecord)) )
00176 return FALSE;
00177
00178
00179
00180 for ( freeStruct = SM_FreeList(smRecord); IsNonNULL(freeStruct);
00181 freeStruct = SMFL_NextFreeStruct(freeStruct) )
00182 if ( freeStruct == pStruct )
00183 return FALSE;
00184
00185 return TRUE;
00186 }
00187
00188
00189
00190
00191
00192
00193
00194
00195 xsbBool smIsAllocatedStructRef(Structure_Manager smRecord, void *ptr) {
00196
00197 return ( smIsValidStructRef(smRecord,ptr) &&
00198 smIsAllocatedStruct(smRecord,ptr) );
00199 }