1
/* File: struct_manager.c
2
** Author(s): Ernie Johnson
3
** Contact: xsb-contact@cs.sunysb.edu
5
** Copyright (C) The Research Foundation of SUNY, 1986, 1993-1998
7
** XSB is free software; you can redistribute it and/or modify it under the
8
** terms of the GNU Library General Public License as published by the Free
9
** Software Foundation; either version 2 of the License, or (at your option)
12
** XSB is distributed in the hope that it will be useful, but WITHOUT ANY
13
** WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14
** FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for
17
** You should have received a copy of the GNU Library General Public License
18
** along with XSB; if not, write to the Free Software Foundation,
19
** Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21
** $Id: struct_manager.c,v 1.19 2006/01/18 19:32:26 dwarren Exp $
26
#include "xsb_config.h"
27
#include "xsb_debug.h"
33
#include "struct_manager.h"
35
#include "error_xsb.h"
36
#include "debug_xsb.h"
37
#include "flags_xsb.h"
38
#include "memory_xsb.h"
40
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
42
void smPrint(Structure_Manager smRecord, char *string) {
48
for ( pBlock = SM_CurBlock(smRecord); IsNonNULL(pBlock);
49
pBlock = SMBlk_NextBlock(pBlock) )
53
" Structure Manager for %s (%s)\n"
54
"\tCurBlock: %p\t\tTotal Blocks: %u\n"
55
"\tNextStr: %p\t\tFree List: %p\n"
56
"\tLastStr: %p\t\tAlloc List: %p\n"
57
"\tStructs per block: %u\t\tStruct size: %u bytes\n",
58
SM_StructName(smRecord), string,
59
SM_CurBlock(smRecord), nBlocks,
60
SM_NextStruct(smRecord), SM_FreeList(smRecord),
61
SM_LastStruct(smRecord), SM_AllocList(smRecord),
62
SM_StructsPerBlock(smRecord), SM_StructSize(smRecord));
65
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
68
* Allocate a new block from the system and place it at the head of
69
* the block list in the Structure Manager.
72
void smAllocateBlock(Structure_Manager *pSM) {
76
dbg_smPrint(LOG_STRUCT_MANAGER, *pSM,"before block allocation");
77
pNewBlock = mem_alloc(SM_NewBlockSize(*pSM),TABLE_SPACE); // counted in table-stats
78
if ( IsNULL(pNewBlock) )
79
xsb_abort("[smAllocateBlock] Out of memory in allocation of %s block\n",
81
SMBlk_NextBlock(pNewBlock) = SM_CurBlock(*pSM);
82
SM_CurBlock(*pSM) = pNewBlock;
83
SM_NextStruct(*pSM) = SMBlk_FirstStruct(pNewBlock);
84
SM_LastStruct(*pSM) = SMBlk_LastStruct(pNewBlock,
86
SM_StructsPerBlock(*pSM));
87
dbg_smPrint(LOG_STRUCT_MANAGER, *pSM,"after block allocation");
90
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
93
* Return all blocks held by the Structure Manager to the system.
96
void smFreeBlocks(Structure_Manager *pSM) {
98
void *pCurBlock, *pNextBlock;
100
pCurBlock = SM_CurBlock(*pSM);
101
while ( IsNonNULL(pCurBlock) ) {
102
pNextBlock = SMBlk_NextBlock(pCurBlock);
103
mem_dealloc(pCurBlock,SM_NewBlockSize(*pSM),TABLE_SPACE);
104
pCurBlock = pNextBlock;
106
SM_CurBlock(*pSM) = SM_NextStruct(*pSM) = SM_LastStruct(*pSM) = NULL;
107
SM_AllocList(*pSM) = SM_FreeList(*pSM) = NULL;
110
void smPrintBlocks(Structure_Manager *pSM) {
112
void *pCurBlock, *pNextBlock;
114
printf("blocks for SM %p size %d\n",pSM,SM_NewBlockSize(*pSM));
115
pCurBlock = SM_CurBlock(*pSM);
116
while ( IsNonNULL(pCurBlock) ) {
117
printf("Block %p\n",pCurBlock);
118
pNextBlock = SMBlk_NextBlock(pCurBlock);
119
// mem_dealloc(pCurBlock,SM_NewBlockSize(*pSM),TABLE_SPACE);
120
pCurBlock = pNextBlock;
124
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
127
* Determine whether a given pointer is indeed a reference to a
128
* structure maintained by the given Structure Manager.
131
xsbBool smIsValidStructRef(Structure_Manager smRecord, void *ptr) {
133
void *pBlock, *firstStruct, *lastStruct;
137
structSize = SM_StructSize(smRecord);
139
for ( pBlock = SM_CurBlock(smRecord); IsNonNULL(pBlock);
140
pBlock = SMBlk_NextBlock(pBlock) ) {
142
firstStruct = SMBlk_FirstStruct(pBlock);
144
SMBlk_LastStruct(pBlock,structSize,SM_StructsPerBlock(smRecord));
146
/* Determine whether pointer lies within block
147
------------------------------------------- */
148
if ( (firstStruct <= ptr) && (ptr <= lastStruct) ) {
150
/* Determine whether pointer is a valid reference
151
---------------------------------------------- */
152
if ( (((char *)ptr - (char *)firstStruct) MOD structSize) == 0 )
161
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
164
* Determine whether the given structure maintained by the given
165
* Structure Manager is allocated.
168
xsbBool smIsAllocatedStruct(Structure_Manager smRecord, void *pStruct) {
172
/* Determine whether struct lies w/i unallocated section of 1st block
173
------------------------------------------------------------------ */
174
if ( (SM_NextStruct(smRecord) <= pStruct) &&
175
(pStruct <= SM_LastStruct(smRecord)) )
178
/* Determine whether struct is on the free list
179
-------------------------------------------- */
180
for ( freeStruct = SM_FreeList(smRecord); IsNonNULL(freeStruct);
181
freeStruct = SMFL_NextFreeStruct(freeStruct) )
182
if ( freeStruct == pStruct )
188
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
191
* Determine whether a given pointer is a reference to an allocated
192
* structure maintained by the given Structure Manager.
195
xsbBool smIsAllocatedStructRef(Structure_Manager smRecord, void *ptr) {
197
return ( smIsValidStructRef(smRecord,ptr) &&
198
smIsAllocatedStruct(smRecord,ptr) );