~burner/xsb/debianized-xsb

« back to all changes in this revision

Viewing changes to emu/struct_manager.c

  • Committer: Michael R. Head
  • Date: 2006-09-06 22:11:55 UTC
  • Revision ID: burner@n23-20060906221155-7e398d23438a7ee4
Add the files from the 3.0.1 release package

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* File:      struct_manager.c
 
2
** Author(s): Ernie Johnson
 
3
** Contact:   xsb-contact@cs.sunysb.edu
 
4
** 
 
5
** Copyright (C) The Research Foundation of SUNY, 1986, 1993-1998
 
6
** 
 
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)
 
10
** any later version.
 
11
** 
 
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
 
15
** more details.
 
16
** 
 
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.
 
20
**
 
21
** $Id: struct_manager.c,v 1.19 2006/01/18 19:32:26 dwarren Exp $
 
22
** 
 
23
*/
 
24
 
 
25
 
 
26
#include "xsb_config.h"
 
27
#include "xsb_debug.h"
 
28
 
 
29
#include <stdio.h>
 
30
#include <stdlib.h>
 
31
 
 
32
#include "auxlry.h"
 
33
#include "struct_manager.h"
 
34
#include "cell_xsb.h"
 
35
#include "error_xsb.h"
 
36
#include "debug_xsb.h"
 
37
#include "flags_xsb.h"
 
38
#include "memory_xsb.h"
 
39
 
 
40
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 
41
 
 
42
void smPrint(Structure_Manager smRecord, char *string) {
 
43
 
 
44
  void *pBlock;
 
45
  counter nBlocks;
 
46
 
 
47
  nBlocks = 0;
 
48
  for ( pBlock = SM_CurBlock(smRecord);  IsNonNULL(pBlock);
 
49
        pBlock = SMBlk_NextBlock(pBlock) )
 
50
    nBlocks++;
 
51
 
 
52
  fprintf(stddbg,
 
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));
 
63
}
 
64
 
 
65
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 
66
 
 
67
/*
 
68
 *  Allocate a new block from the system and place it at the head of
 
69
 *  the block list in the Structure Manager.
 
70
 */
 
71
 
 
72
void smAllocateBlock(Structure_Manager *pSM) {
 
73
 
 
74
  void *pNewBlock;
 
75
 
 
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",
 
80
              SM_StructName(*pSM));
 
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,
 
85
                                         SM_StructSize(*pSM),
 
86
                                         SM_StructsPerBlock(*pSM));
 
87
  dbg_smPrint(LOG_STRUCT_MANAGER, *pSM,"after block allocation");
 
88
}
 
89
 
 
90
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 
91
 
 
92
/*
 
93
 *  Return all blocks held by the Structure Manager to the system.
 
94
 */
 
95
 
 
96
void smFreeBlocks(Structure_Manager *pSM) {
 
97
 
 
98
  void *pCurBlock, *pNextBlock;
 
99
 
 
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;
 
105
  }
 
106
  SM_CurBlock(*pSM) = SM_NextStruct(*pSM) = SM_LastStruct(*pSM) = NULL;
 
107
  SM_AllocList(*pSM) = SM_FreeList(*pSM) = NULL;
 
108
}
 
109
 
 
110
void smPrintBlocks(Structure_Manager *pSM) {
 
111
 
 
112
  void *pCurBlock, *pNextBlock;
 
113
 
 
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;
 
121
  }
 
122
}
 
123
 
 
124
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 
125
 
 
126
/*
 
127
 *  Determine whether a given pointer is indeed a reference to a
 
128
 *  structure maintained by the given Structure Manager.
 
129
 */
 
130
 
 
131
xsbBool smIsValidStructRef(Structure_Manager smRecord, void *ptr) {
 
132
 
 
133
  void *pBlock, *firstStruct, *lastStruct;
 
134
  size_t structSize;
 
135
 
 
136
 
 
137
  structSize = SM_StructSize(smRecord);
 
138
 
 
139
  for ( pBlock = SM_CurBlock(smRecord);  IsNonNULL(pBlock);
 
140
        pBlock = SMBlk_NextBlock(pBlock) ) {
 
141
    
 
142
    firstStruct = SMBlk_FirstStruct(pBlock);
 
143
    lastStruct =
 
144
      SMBlk_LastStruct(pBlock,structSize,SM_StructsPerBlock(smRecord));
 
145
 
 
146
    /* Determine whether pointer lies within block
 
147
       ------------------------------------------- */
 
148
    if ( (firstStruct <= ptr) && (ptr <= lastStruct) ) {
 
149
 
 
150
      /* Determine whether pointer is a valid reference
 
151
         ---------------------------------------------- */
 
152
      if ( (((char *)ptr - (char *)firstStruct) MOD structSize) == 0 )
 
153
        return TRUE;
 
154
      else
 
155
        return FALSE;
 
156
    }
 
157
  }
 
158
  return FALSE;
 
159
}
 
160
 
 
161
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 
162
 
 
163
/*
 
164
 *  Determine whether the given structure maintained by the given
 
165
 *  Structure Manager is allocated.
 
166
 */
 
167
 
 
168
xsbBool smIsAllocatedStruct(Structure_Manager smRecord, void *pStruct) {
 
169
 
 
170
  void *freeStruct;
 
171
 
 
172
  /* Determine whether struct lies w/i unallocated section of 1st block
 
173
     ------------------------------------------------------------------ */
 
174
  if ( (SM_NextStruct(smRecord) <= pStruct) &&
 
175
       (pStruct <= SM_LastStruct(smRecord)) )
 
176
    return FALSE;
 
177
 
 
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 )
 
183
      return FALSE;
 
184
 
 
185
  return TRUE;
 
186
}
 
187
 
 
188
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 
189
 
 
190
/*
 
191
 *  Determine whether a given pointer is a reference to an allocated
 
192
 *  structure maintained by the given Structure Manager.
 
193
 */
 
194
 
 
195
xsbBool smIsAllocatedStructRef(Structure_Manager smRecord, void *ptr) {
 
196
 
 
197
  return ( smIsValidStructRef(smRecord,ptr) &&
 
198
           smIsAllocatedStruct(smRecord,ptr) );
 
199
}