00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #define _DFFCTBIN_SOURCE_
00024
00025 #include "setup.h"
00026
00027 #if DEFFACTS_CONSTRUCT && (BLOAD || BLOAD_ONLY || BLOAD_AND_BSAVE) && (! RUN_TIME)
00028
00029 #include <stdio.h>
00030 #define _STDIO_INCLUDED_
00031
00032 #include "memalloc.h"
00033 #include "dffctdef.h"
00034 #include "moduldef.h"
00035 #include "bload.h"
00036 #include "bsave.h"
00037 #include "envrnmnt.h"
00038
00039 #include "dffctbin.h"
00040
00041
00042
00043
00044
00045 #if BLOAD_AND_BSAVE
00046 static void BsaveFind(void *);
00047 static void BsaveExpressions(void *,FILE *);
00048 static void BsaveStorage(void *,FILE *);
00049 static void BsaveBinaryItem(void *,FILE *);
00050 #endif
00051 static void BloadStorage(void *);
00052 static void BloadBinaryItem(void *);
00053 static void UpdateDeffactsModule(void *,void *,long);
00054 static void UpdateDeffacts(void *,void *,long);
00055 static void ClearBload(void *);
00056 static void DeallocateDeffactsBloadData(void *);
00057
00058
00059
00060
00061
00062 globle void DeffactsBinarySetup(
00063 void *theEnv)
00064 {
00065 AllocateEnvironmentData(theEnv,DFFCTBIN_DATA,sizeof(struct deffactsBinaryData),DeallocateDeffactsBloadData);
00066 #if BLOAD_AND_BSAVE
00067 AddBinaryItem(theEnv,"deffacts",0,BsaveFind,BsaveExpressions,
00068 BsaveStorage,BsaveBinaryItem,
00069 BloadStorage,BloadBinaryItem,
00070 ClearBload);
00071 #endif
00072
00073 #if (BLOAD || BLOAD_ONLY)
00074 AddBinaryItem(theEnv,"deffacts",0,NULL,NULL,NULL,NULL,
00075 BloadStorage,BloadBinaryItem,
00076 ClearBload);
00077 #endif
00078 }
00079
00080
00081
00082
00083
00084 static void DeallocateDeffactsBloadData(
00085 void *theEnv)
00086 {
00087 size_t space;
00088
00089 space = DeffactsBinaryData(theEnv)->NumberOfDeffacts * sizeof(struct deffacts);
00090 if (space != 0) genfree(theEnv,(void *) DeffactsBinaryData(theEnv)->DeffactsArray,space);
00091
00092 space = DeffactsBinaryData(theEnv)->NumberOfDeffactsModules * sizeof(struct deffactsModule);
00093 if (space != 0) genfree(theEnv,(void *) DeffactsBinaryData(theEnv)->ModuleArray,space);
00094 }
00095
00096 #if BLOAD_AND_BSAVE
00097
00098
00099
00100
00101
00102
00103 static void BsaveFind(
00104 void *theEnv)
00105 {
00106 struct deffacts *theDeffacts;
00107 struct defmodule *theModule;
00108
00109
00110
00111
00112
00113
00114
00115 SaveBloadCount(theEnv,DeffactsBinaryData(theEnv)->NumberOfDeffactsModules);
00116 SaveBloadCount(theEnv,DeffactsBinaryData(theEnv)->NumberOfDeffacts);
00117
00118
00119
00120
00121
00122
00123 DeffactsBinaryData(theEnv)->NumberOfDeffacts = 0;
00124 DeffactsBinaryData(theEnv)->NumberOfDeffactsModules = 0;
00125
00126
00127
00128
00129
00130 for (theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,NULL);
00131 theModule != NULL;
00132 theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,theModule))
00133 {
00134
00135
00136
00137
00138
00139
00140 EnvSetCurrentModule(theEnv,(void *) theModule);
00141 DeffactsBinaryData(theEnv)->NumberOfDeffactsModules++;
00142
00143
00144
00145
00146
00147 for (theDeffacts = (struct deffacts *) EnvGetNextDeffacts(theEnv,NULL);
00148 theDeffacts != NULL;
00149 theDeffacts = (struct deffacts *) EnvGetNextDeffacts(theEnv,theDeffacts))
00150 {
00151
00152
00153
00154
00155 MarkConstructHeaderNeededItems(&theDeffacts->header,DeffactsBinaryData(theEnv)->NumberOfDeffacts++);
00156
00157
00158
00159
00160
00161
00162
00163 ExpressionData(theEnv)->ExpressionCount += ExpressionSize(theDeffacts->assertList);
00164 MarkNeededItems(theEnv,theDeffacts->assertList);
00165 }
00166 }
00167 }
00168
00169
00170
00171
00172
00173 static void BsaveExpressions(
00174 void *theEnv,
00175 FILE *fp)
00176 {
00177 struct deffacts *theDeffacts;
00178 struct defmodule *theModule;
00179
00180
00181
00182
00183
00184 for (theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,NULL);
00185 theModule != NULL;
00186 theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,theModule))
00187 {
00188
00189
00190
00191
00192 EnvSetCurrentModule(theEnv,(void *) theModule);
00193
00194
00195
00196
00197
00198
00199 for (theDeffacts = (struct deffacts *) EnvGetNextDeffacts(theEnv,NULL);
00200 theDeffacts != NULL;
00201 theDeffacts = (struct deffacts *) EnvGetNextDeffacts(theEnv,theDeffacts))
00202 { BsaveExpression(theEnv,theDeffacts->assertList,fp); }
00203 }
00204 }
00205
00206
00207
00208
00209
00210 static void BsaveStorage(
00211 void *theEnv,
00212 FILE *fp)
00213 {
00214 size_t space;
00215
00216
00217
00218
00219
00220
00221
00222
00223 space = sizeof(long) * 2;
00224 GenWrite(&space,sizeof(size_t),fp);
00225 GenWrite(&DeffactsBinaryData(theEnv)->NumberOfDeffacts,sizeof(long int),fp);
00226 GenWrite(&DeffactsBinaryData(theEnv)->NumberOfDeffactsModules,sizeof(long int),fp);
00227 }
00228
00229
00230
00231
00232
00233 static void BsaveBinaryItem(
00234 void *theEnv,
00235 FILE *fp)
00236 {
00237 size_t space;
00238 struct deffacts *theDeffacts;
00239 struct bsaveDeffacts newDeffacts;
00240 struct defmodule *theModule;
00241 struct bsaveDeffactsModule tempDeffactsModule;
00242 struct deffactsModule *theModuleItem;
00243
00244
00245
00246
00247
00248
00249 space = DeffactsBinaryData(theEnv)->NumberOfDeffacts * sizeof(struct bsaveDeffacts) +
00250 (DeffactsBinaryData(theEnv)->NumberOfDeffactsModules * sizeof(struct bsaveDeffactsModule));
00251 GenWrite(&space,sizeof(size_t),fp);
00252
00253
00254
00255
00256
00257 DeffactsBinaryData(theEnv)->NumberOfDeffacts = 0;
00258 for (theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,NULL);
00259 theModule != NULL;
00260 theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,theModule))
00261 {
00262 EnvSetCurrentModule(theEnv,(void *) theModule);
00263
00264 theModuleItem = (struct deffactsModule *) GetModuleItem(theEnv,NULL,DeffactsData(theEnv)->DeffactsModuleIndex);
00265 AssignBsaveDefmdlItemHdrVals(&tempDeffactsModule.header,&theModuleItem->header);
00266 GenWrite(&tempDeffactsModule,(unsigned long) sizeof(struct bsaveDeffactsModule),fp);
00267 }
00268
00269
00270
00271
00272
00273 for (theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,NULL);
00274 theModule != NULL;
00275 theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,theModule))
00276 {
00277 EnvSetCurrentModule(theEnv,(void *) theModule);
00278
00279 for (theDeffacts = (struct deffacts *) EnvGetNextDeffacts(theEnv,NULL);
00280 theDeffacts != NULL;
00281 theDeffacts = (struct deffacts *) EnvGetNextDeffacts(theEnv,theDeffacts))
00282 {
00283 AssignBsaveConstructHeaderVals(&newDeffacts.header,&theDeffacts->header);
00284 if (theDeffacts->assertList != NULL)
00285 {
00286 newDeffacts.assertList = ExpressionData(theEnv)->ExpressionCount;
00287 ExpressionData(theEnv)->ExpressionCount += ExpressionSize(theDeffacts->assertList);
00288 }
00289 else
00290 { newDeffacts.assertList = -1L; }
00291
00292 GenWrite(&newDeffacts,(unsigned long) sizeof(struct bsaveDeffacts),fp);
00293 }
00294 }
00295
00296
00297
00298
00299
00300
00301
00302
00303 RestoreBloadCount(theEnv,&DeffactsBinaryData(theEnv)->NumberOfDeffactsModules);
00304 RestoreBloadCount(theEnv,&DeffactsBinaryData(theEnv)->NumberOfDeffacts);
00305 }
00306
00307 #endif
00308
00309
00310
00311
00312
00313 static void BloadStorage(
00314 void *theEnv)
00315 {
00316 size_t space;
00317
00318
00319
00320
00321
00322
00323 GenReadBinary(theEnv,&space,sizeof(size_t));
00324 GenReadBinary(theEnv,&DeffactsBinaryData(theEnv)->NumberOfDeffacts,sizeof(long int));
00325 GenReadBinary(theEnv,&DeffactsBinaryData(theEnv)->NumberOfDeffactsModules,sizeof(long int));
00326
00327
00328
00329
00330
00331
00332 if (DeffactsBinaryData(theEnv)->NumberOfDeffactsModules == 0)
00333 {
00334 DeffactsBinaryData(theEnv)->DeffactsArray = NULL;
00335 DeffactsBinaryData(theEnv)->ModuleArray = NULL;
00336 return;
00337 }
00338
00339 space = DeffactsBinaryData(theEnv)->NumberOfDeffactsModules * sizeof(struct deffactsModule);
00340 DeffactsBinaryData(theEnv)->ModuleArray = (struct deffactsModule *) genalloc(theEnv,space);
00341
00342
00343
00344
00345
00346
00347 if (DeffactsBinaryData(theEnv)->NumberOfDeffacts == 0)
00348 {
00349 DeffactsBinaryData(theEnv)->DeffactsArray = NULL;
00350 return;
00351 }
00352
00353 space = (DeffactsBinaryData(theEnv)->NumberOfDeffacts * sizeof(struct deffacts));
00354 DeffactsBinaryData(theEnv)->DeffactsArray = (struct deffacts *) genalloc(theEnv,space);
00355 }
00356
00357
00358
00359
00360
00361 static void BloadBinaryItem(
00362 void *theEnv)
00363 {
00364 size_t space;
00365
00366
00367
00368
00369
00370
00371
00372 GenReadBinary(theEnv,&space,sizeof(size_t));
00373
00374
00375
00376
00377
00378
00379 BloadandRefresh(theEnv,DeffactsBinaryData(theEnv)->NumberOfDeffactsModules,
00380 sizeof(struct bsaveDeffactsModule),UpdateDeffactsModule);
00381
00382
00383
00384
00385
00386
00387 BloadandRefresh(theEnv,DeffactsBinaryData(theEnv)->NumberOfDeffacts,
00388 sizeof(struct bsaveDeffacts),UpdateDeffacts);
00389 }
00390
00391
00392
00393
00394
00395 static void UpdateDeffactsModule(
00396 void *theEnv,
00397 void *buf,
00398 long obji)
00399 {
00400 struct bsaveDeffactsModule *bdmPtr;
00401
00402 bdmPtr = (struct bsaveDeffactsModule *) buf;
00403 UpdateDefmoduleItemHeader(theEnv,&bdmPtr->header,&DeffactsBinaryData(theEnv)->ModuleArray[obji].header,
00404 (int) sizeof(struct deffacts),(void *) DeffactsBinaryData(theEnv)->DeffactsArray);
00405 }
00406
00407
00408
00409
00410
00411 static void UpdateDeffacts(
00412 void *theEnv,
00413 void *buf,
00414 long obji)
00415 {
00416 struct bsaveDeffacts *bdp;
00417
00418 bdp = (struct bsaveDeffacts *) buf;
00419 UpdateConstructHeader(theEnv,&bdp->header,&DeffactsBinaryData(theEnv)->DeffactsArray[obji].header,
00420 (int) sizeof(struct deffactsModule),(void *) DeffactsBinaryData(theEnv)->ModuleArray,
00421 (int) sizeof(struct deffacts),(void *) DeffactsBinaryData(theEnv)->DeffactsArray);
00422 DeffactsBinaryData(theEnv)->DeffactsArray[obji].assertList = ExpressionPointer(bdp->assertList);
00423 }
00424
00425
00426
00427
00428
00429 static void ClearBload(
00430 void *theEnv)
00431 {
00432 long i;
00433 size_t space;
00434
00435
00436
00437
00438
00439
00440 for (i = 0; i < DeffactsBinaryData(theEnv)->NumberOfDeffacts; i++)
00441 { UnmarkConstructHeader(theEnv,&DeffactsBinaryData(theEnv)->DeffactsArray[i].header); }
00442
00443
00444
00445
00446
00447 space = DeffactsBinaryData(theEnv)->NumberOfDeffacts * sizeof(struct deffacts);
00448 if (space != 0) genfree(theEnv,(void *) DeffactsBinaryData(theEnv)->DeffactsArray,space);
00449 DeffactsBinaryData(theEnv)->NumberOfDeffacts = 0;
00450
00451
00452
00453
00454
00455 space = DeffactsBinaryData(theEnv)->NumberOfDeffactsModules * sizeof(struct deffactsModule);
00456 if (space != 0) genfree(theEnv,(void *) DeffactsBinaryData(theEnv)->ModuleArray,space);
00457 DeffactsBinaryData(theEnv)->NumberOfDeffactsModules = 0;
00458 }
00459
00460
00461
00462
00463
00464 globle void *BloadDeffactsModuleReference(
00465 void *theEnv,
00466 int theIndex)
00467 {
00468 return ((void *) &DeffactsBinaryData(theEnv)->ModuleArray[theIndex]);
00469 }
00470
00471 #endif
00472
00473