struct_manager.c

00001 /* File:      struct_manager.c
00002 ** Author(s): Ernie Johnson
00003 ** Contact:   xsb-contact@cs.sunysb.edu
00004 ** 
00005 ** Copyright (C) The Research Foundation of SUNY, 1986, 1993-1998
00006 ** 
00007 ** XSB is free software; you can redistribute it and/or modify it under the
00008 ** terms of the GNU Library General Public License as published by the Free
00009 ** Software Foundation; either version 2 of the License, or (at your option)
00010 ** any later version.
00011 ** 
00012 ** XSB is distributed in the hope that it will be useful, but WITHOUT ANY
00013 ** WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00014 ** FOR A PARTICULAR PURPOSE.  See the GNU Library General Public License for
00015 ** more details.
00016 ** 
00017 ** You should have received a copy of the GNU Library General Public License
00018 ** along with XSB; if not, write to the Free Software Foundation,
00019 ** Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00020 **
00021 ** $Id: struct_manager.c,v 1.19 2006/01/18 19:32:26 dwarren Exp $
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  *  Allocate a new block from the system and place it at the head of
00069  *  the block list in the Structure Manager.
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);  // counted in table-stats
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  *  Return all blocks held by the Structure Manager to the system.
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     //    mem_dealloc(pCurBlock,SM_NewBlockSize(*pSM),TABLE_SPACE);
00120     pCurBlock = pNextBlock;
00121   }
00122 }
00123 
00124 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
00125 
00126 /*
00127  *  Determine whether a given pointer is indeed a reference to a
00128  *  structure maintained by the given Structure Manager.
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     /* Determine whether pointer lies within block
00147        ------------------------------------------- */
00148     if ( (firstStruct <= ptr) && (ptr <= lastStruct) ) {
00149 
00150       /* Determine whether pointer is a valid reference
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  *  Determine whether the given structure maintained by the given
00165  *  Structure Manager is allocated.
00166  */
00167 
00168 xsbBool smIsAllocatedStruct(Structure_Manager smRecord, void *pStruct) {
00169 
00170   void *freeStruct;
00171 
00172   /* Determine whether struct lies w/i unallocated section of 1st block
00173      ------------------------------------------------------------------ */
00174   if ( (SM_NextStruct(smRecord) <= pStruct) &&
00175        (pStruct <= SM_LastStruct(smRecord)) )
00176     return FALSE;
00177 
00178   /* Determine whether struct is on the free list
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  *  Determine whether a given pointer is a reference to an allocated
00192  *  structure maintained by the given Structure Manager.
00193  */
00194 
00195 xsbBool smIsAllocatedStructRef(Structure_Manager smRecord, void *ptr) {
00196 
00197   return ( smIsValidStructRef(smRecord,ptr) &&
00198            smIsAllocatedStruct(smRecord,ptr) );
00199 }

Generated on Wed Jul 26 13:30:42 2006 for XSB by  doxygen 1.4.5