1    /*******************************************************/
2    /*      "C" Language Integrated Production System      */
3    /*                                                     */
4    /*             CLIPS Version 6.30  08/16/14            */
5    /*                                                     */
6    /*                                                     */
7    /*******************************************************/
8 
9 /*************************************************************/
10 /* Purpose: Binary Load/Save Functions for Generic Functions */
11 /*                                                           */
12 /* Principal Programmer(s):                                  */
13 /*      Brian L. Dantes                                      */
14 /*                                                           */
15 /* Contributing Programmer(s):                               */
16 /*                                                           */
17 /* Revision History:                                         */
18 /*                                                           */
19 /*      6.30: Removed conditional code for unsupported       */
20 /*            compilers/operating systems (IBM_MCW,          */
21 /*            MAC_MCW, and IBM_TBC).                         */
22 /*                                                           */
23 /*            Changed integer type/precision.                */
24 /*                                                           */
25 /*************************************************************/
26 
27 /* =========================================
28    *****************************************
29                EXTERNAL DEFINITIONS
30    =========================================
31    ***************************************** */
32 #include "setup.h"
33 
34 #if DEFGENERIC_CONSTRUCT && (BLOAD || BLOAD_ONLY || BLOAD_AND_BSAVE)
35 
36 #include "constant.h"
37 #include "envrnmnt.h"
38 #include "memalloc.h"
39 #include "bload.h"
40 #include "bsave.h"
41 
42 #include "cstrcbin.h"
43 
44 #if OBJECT_SYSTEM
45 #include "objbin.h"
46 #endif
47 
48 #include "genrccom.h"
49 #include "modulbin.h"
50 
51 #define _GENRCBIN_SOURCE_
52 #include "genrcbin.h"
53 
54 #include "router.h"
55 
56 /* =========================================
57    *****************************************
58                MACROS AND TYPES
59    =========================================
60    ***************************************** */
61 
62 #define MethodPointer(i) (((i) == -1L) ? NULL : (DEFMETHOD *) &DefgenericBinaryData(theEnv)->MethodArray[i])
63 #define RestrictionPointer(i) (((i) == -1L) ? NULL : (RESTRICTION *) &DefgenericBinaryData(theEnv)->RestrictionArray[i])
64 #define TypePointer(i) (((i) == -1L) ? NULL : (void **) &DefgenericBinaryData(theEnv)->TypeArray[i])
65 
66 typedef struct bsaveRestriction
67   {
68    long types,query;
69    short tcnt;
70   } BSAVE_RESTRICTION;
71 
72 typedef struct bsaveMethod
73   {
74    short index;
75    short restrictionCount,
76        minRestrictions,maxRestrictions,
77        localVarCount;
78    int system;
79    long restrictions,actions;
80   } BSAVE_METHOD;
81 
82 typedef struct bsaveGenericFunc
83   {
84    struct bsaveConstructHeader header;
85    long methods;
86    short mcnt;
87   } BSAVE_GENERIC;
88 
89 typedef struct bsaveGenericModule
90   {
91    struct bsaveDefmoduleItemHeader header;
92   } BSAVE_DEFGENERIC_MODULE;
93 
94 /* =========================================
95    *****************************************
96       INTERNALLY VISIBLE FUNCTION HEADERS
97    =========================================
98    ***************************************** */
99 
100 #if BLOAD_AND_BSAVE
101 
102 static void BsaveGenericsFind(void *);
103 static void MarkDefgenericItems(void *,struct constructHeader *,void *);
104 static void BsaveGenericsExpressions(void *,FILE *);
105 static void BsaveMethodExpressions(void *,struct constructHeader *,void *);
106 static void BsaveRestrictionExpressions(void *,struct constructHeader *,void *);
107 static void BsaveGenerics(void *,FILE *);
108 static void BsaveDefgenericHeader(void *,struct constructHeader *,void *);
109 static void BsaveMethods(void *,struct constructHeader *,void *);
110 static void BsaveMethodRestrictions(void *,struct constructHeader *,void *);
111 static void BsaveRestrictionTypes(void *,struct constructHeader *,void *);
112 static void BsaveStorageGenerics(void *,FILE *);
113 
114 #endif
115 
116 static void BloadStorageGenerics(void *);
117 static void BloadGenerics(void *);
118 static void UpdateGenericModule(void *,void *,long);
119 static void UpdateGeneric(void *,void *,long);
120 static void UpdateMethod(void *,void *,long);
121 static void UpdateRestriction(void *,void *,long);
122 static void UpdateType(void *,void *,long);
123 static void ClearBloadGenerics(void *);
124 static void DeallocateDefgenericBinaryData(void *);
125 
126 /* =========================================
127    *****************************************
128           EXTERNALLY VISIBLE FUNCTIONS
129    =========================================
130    ***************************************** */
131 
132 /***********************************************************
133   NAME         : SetupGenericsBload
134   DESCRIPTION  : Initializes data structures and
135                    routines for binary loads of
136                    generic function constructs
137   INPUTS       : None
138   RETURNS      : Nothing useful
139   SIDE EFFECTS : Routines defined and structures initialized
140   NOTES        : None
141  ***********************************************************/
SetupGenericsBload(void * theEnv)142 globle void SetupGenericsBload(
143   void *theEnv)
144   {
145    AllocateEnvironmentData(theEnv,GENRCBIN_DATA,sizeof(struct defgenericBinaryData),DeallocateDefgenericBinaryData);
146 #if BLOAD_AND_BSAVE
147    AddBinaryItem(theEnv,"generic functions",0,BsaveGenericsFind,BsaveGenericsExpressions,
148                              BsaveStorageGenerics,BsaveGenerics,
149                              BloadStorageGenerics,BloadGenerics,
150                              ClearBloadGenerics);
151 #endif
152 #if BLOAD || BLOAD_ONLY
153    AddBinaryItem(theEnv,"generic functions",0,NULL,NULL,NULL,NULL,
154                              BloadStorageGenerics,BloadGenerics,
155                              ClearBloadGenerics);
156 #endif
157   }
158 
159 /***********************************************************/
160 /* DeallocateDefgenericBinaryData: Deallocates environment */
161 /*    data for the defgeneric binary functionality.        */
162 /***********************************************************/
DeallocateDefgenericBinaryData(void * theEnv)163 static void DeallocateDefgenericBinaryData(
164   void *theEnv)
165   {
166 #if (BLOAD || BLOAD_ONLY || BLOAD_AND_BSAVE) && (! RUN_TIME)
167    size_t space;
168 
169    space = DefgenericBinaryData(theEnv)->GenericCount * sizeof(struct defgeneric);
170    if (space != 0) genfree(theEnv,(void *) DefgenericBinaryData(theEnv)->DefgenericArray,space);
171 
172    space = DefgenericBinaryData(theEnv)->MethodCount * sizeof(struct method);
173    if (space != 0) genfree(theEnv,(void *) DefgenericBinaryData(theEnv)->MethodArray,space);
174 
175    space = DefgenericBinaryData(theEnv)->RestrictionCount * sizeof(struct restriction);
176    if (space != 0) genfree(theEnv,(void *) DefgenericBinaryData(theEnv)->RestrictionArray,space);
177 
178    space = DefgenericBinaryData(theEnv)->TypeCount * sizeof(void *);
179    if (space != 0) genfree(theEnv,(void *) DefgenericBinaryData(theEnv)->TypeArray,space);
180 
181    space =  DefgenericBinaryData(theEnv)->ModuleCount * sizeof(struct defgenericModule);
182    if (space != 0) genfree(theEnv,(void *) DefgenericBinaryData(theEnv)->ModuleArray,space);
183 #endif
184   }
185 
186 /***************************************************
187   NAME         : BloadDefgenericModuleReference
188   DESCRIPTION  : Returns a pointer to the
189                  appropriate defgeneric module
190   INPUTS       : The index of the module
191   RETURNS      : A pointer to the module
192   SIDE EFFECTS : None
193   NOTES        : None
194  ***************************************************/
BloadDefgenericModuleReference(void * theEnv,int theIndex)195 globle void *BloadDefgenericModuleReference(
196   void *theEnv,
197   int theIndex)
198   {
199    return ((void *) &DefgenericBinaryData(theEnv)->ModuleArray[theIndex]);
200   }
201 
202 /* =========================================
203    *****************************************
204           INTERNALLY VISIBLE FUNCTIONS
205    =========================================
206    ***************************************** */
207 
208 #if BLOAD_AND_BSAVE
209 
210 /***************************************************************************
211   NAME         : BsaveGenericsFind
212   DESCRIPTION  : For all generic functions and their
213                    methods, this routine marks all
214                    the needed symbols and system functions.
215                  Also, it also counts the number of
216                    expression structures needed.
217                  Also, counts total number of generics, methods,
218                    restrictions and types.
219   INPUTS       : None
220   RETURNS      : Nothing useful
221   SIDE EFFECTS : ExpressionCount (a global from BSAVE.C) is incremented
222                    for every expression needed
223                  Symbols and system function are marked in their structures
224   NOTES        : Also sets bsaveIndex for each generic function (assumes
225                    generic functions will be bsaved in order of binary list)
226  ***************************************************************************/
BsaveGenericsFind(void * theEnv)227 static void BsaveGenericsFind(
228   void *theEnv)
229   {
230    SaveBloadCount(theEnv,DefgenericBinaryData(theEnv)->ModuleCount);
231    SaveBloadCount(theEnv,DefgenericBinaryData(theEnv)->GenericCount);
232    SaveBloadCount(theEnv,DefgenericBinaryData(theEnv)->MethodCount);
233    SaveBloadCount(theEnv,DefgenericBinaryData(theEnv)->RestrictionCount);
234    SaveBloadCount(theEnv,DefgenericBinaryData(theEnv)->TypeCount);
235 
236    DefgenericBinaryData(theEnv)->GenericCount = 0L;
237    DefgenericBinaryData(theEnv)->MethodCount = 0L;
238    DefgenericBinaryData(theEnv)->RestrictionCount = 0L;
239    DefgenericBinaryData(theEnv)->TypeCount = 0L;
240 
241    DefgenericBinaryData(theEnv)->ModuleCount =
242       DoForAllConstructs(theEnv,MarkDefgenericItems,DefgenericData(theEnv)->DefgenericModuleIndex,
243                                     FALSE,NULL);
244   }
245 
246 /***************************************************
247   NAME         : MarkDefgenericItems
248   DESCRIPTION  : Marks the needed items for
249                  a defgeneric (and methods) bsave
250   INPUTS       : 1) The defgeneric
251                  2) User data buffer (ignored)
252   RETURNS      : Nothing useful
253   SIDE EFFECTS : Needed items marked
254   NOTES        : None
255  ***************************************************/
MarkDefgenericItems(void * theEnv,struct constructHeader * theDefgeneric,void * userBuffer)256 static void MarkDefgenericItems(
257   void *theEnv,
258   struct constructHeader *theDefgeneric,
259   void *userBuffer)
260   {
261 #if MAC_XCD
262 #pragma unused(userBuffer)
263 #endif
264    DEFGENERIC *gfunc = (DEFGENERIC *) theDefgeneric;
265    long i,j;
266    DEFMETHOD *meth;
267    RESTRICTION *rptr;
268 
269    MarkConstructHeaderNeededItems(&gfunc->header,DefgenericBinaryData(theEnv)->GenericCount++);
270    DefgenericBinaryData(theEnv)->MethodCount += (long) gfunc->mcnt;
271    for (i = 0 ; i < gfunc->mcnt ; i++)
272      {
273       meth = &gfunc->methods[i];
274       ExpressionData(theEnv)->ExpressionCount += ExpressionSize(meth->actions);
275       MarkNeededItems(theEnv,meth->actions);
276       DefgenericBinaryData(theEnv)->RestrictionCount += meth->restrictionCount;
277       for (j = 0 ; j < meth->restrictionCount ; j++)
278         {
279          rptr = &meth->restrictions[j];
280          ExpressionData(theEnv)->ExpressionCount += ExpressionSize(rptr->query);
281          MarkNeededItems(theEnv,rptr->query);
282          DefgenericBinaryData(theEnv)->TypeCount += rptr->tcnt;
283         }
284      }
285   }
286 
287 /***************************************************
288   NAME         : BsaveGenericsExpressions
289   DESCRIPTION  : Writes out all expressions needed
290                    by generic functions
291   INPUTS       : The file pointer of the binary file
292   RETURNS      : Nothing useful
293   SIDE EFFECTS : File updated
294   NOTES        : None
295  ***************************************************/
BsaveGenericsExpressions(void * theEnv,FILE * fp)296 static void BsaveGenericsExpressions(
297   void *theEnv,
298   FILE *fp)
299   {
300    /* ================================================================
301       Important to save all expressions for methods before any
302       expressions for restrictions, since methods will be stored first
303       ================================================================ */
304    DoForAllConstructs(theEnv,BsaveMethodExpressions,DefgenericData(theEnv)->DefgenericModuleIndex,
305                       FALSE,(void *) fp);
306 
307    DoForAllConstructs(theEnv,BsaveRestrictionExpressions,DefgenericData(theEnv)->DefgenericModuleIndex,
308                       FALSE,(void *) fp);
309   }
310 
311 /***************************************************
312   NAME         : BsaveMethodExpressions
313   DESCRIPTION  : Saves the needed expressions for
314                  a defgeneric methods bsave
315   INPUTS       : 1) The defgeneric
316                  2) Output data file pointer
317   RETURNS      : Nothing useful
318   SIDE EFFECTS : Method action expressions saved
319   NOTES        : None
320  ***************************************************/
BsaveMethodExpressions(void * theEnv,struct constructHeader * theDefgeneric,void * userBuffer)321 static void BsaveMethodExpressions(
322   void *theEnv,
323   struct constructHeader *theDefgeneric,
324   void *userBuffer)
325   {
326    DEFGENERIC *gfunc = (DEFGENERIC *) theDefgeneric;
327    long i;
328 
329    for (i = 0 ; i < gfunc->mcnt ; i++)
330      BsaveExpression(theEnv,gfunc->methods[i].actions,(FILE *) userBuffer);
331   }
332 
333 /***************************************************
334   NAME         : BsaveRestrictionExpressions
335   DESCRIPTION  : Saves the needed expressions for
336                  a defgeneric method restriction
337                  queries bsave
338   INPUTS       : 1) The defgeneric
339                  2) Output data file pointer
340   RETURNS      : Nothing useful
341   SIDE EFFECTS : Method restriction query
342                  expressions saved
343   NOTES        : None
344  ***************************************************/
BsaveRestrictionExpressions(void * theEnv,struct constructHeader * theDefgeneric,void * userBuffer)345 static void BsaveRestrictionExpressions(
346   void *theEnv,
347   struct constructHeader *theDefgeneric,
348   void *userBuffer)
349   {
350    DEFGENERIC *gfunc = (DEFGENERIC *) theDefgeneric;
351    long i,j;
352    DEFMETHOD *meth;
353 
354    for (i = 0 ; i < gfunc->mcnt ; i++)
355      {
356       meth = &gfunc->methods[i];
357       for (j = 0 ; j < meth->restrictionCount ; j++)
358         BsaveExpression(theEnv,meth->restrictions[j].query,(FILE *) userBuffer);
359      }
360   }
361 
362 /***********************************************************
363   NAME         : BsaveStorageGenerics
364   DESCRIPTION  : Writes out number of each type of structure
365                    required for generics
366                  Space required for counts (unsigned long)
367   INPUTS       : File pointer of binary file
368   RETURNS      : Nothing useful
369   SIDE EFFECTS : Binary file adjusted
370   NOTES        : None
371  ***********************************************************/
BsaveStorageGenerics(void * theEnv,FILE * fp)372 static void BsaveStorageGenerics(
373   void *theEnv,
374   FILE *fp)
375   {
376    size_t space;
377 
378    space = sizeof(long) * 5;
379    GenWrite((void *) &space,sizeof(size_t),fp);
380    GenWrite((void *) &DefgenericBinaryData(theEnv)->ModuleCount,sizeof(long),fp);
381    GenWrite((void *) &DefgenericBinaryData(theEnv)->GenericCount,sizeof(long),fp);
382    GenWrite((void *) &DefgenericBinaryData(theEnv)->MethodCount,sizeof(long),fp);
383    GenWrite((void *) &DefgenericBinaryData(theEnv)->RestrictionCount,sizeof(long),fp);
384    GenWrite((void *) &DefgenericBinaryData(theEnv)->TypeCount,sizeof(long),fp);
385   }
386 
387 /****************************************************************************************
388   NAME         : BsaveGenerics
389   DESCRIPTION  : Writes out generic function in binary format
390                  Space required (unsigned long)
391                  All generic modules (sizeof(DEFGENERIC_MODULE) * Number of generic modules)
392                  All generic headers (sizeof(DEFGENERIC) * Number of generics)
393                  All methods (sizeof(DEFMETHOD) * Number of methods)
394                  All method restrictions (sizeof(RESTRICTION) * Number of restrictions)
395                  All restriction type arrays (sizeof(void *) * # of types)
396   INPUTS       : File pointer of binary file
397   RETURNS      : Nothing useful
398   SIDE EFFECTS : Binary file adjusted
399   NOTES        : None
400  ****************************************************************************************/
BsaveGenerics(void * theEnv,FILE * fp)401 static void BsaveGenerics(
402   void *theEnv,
403   FILE *fp)
404   {
405    struct defmodule *theModule;
406    DEFGENERIC_MODULE *theModuleItem;
407    size_t space;
408    BSAVE_DEFGENERIC_MODULE dummy_generic_module;
409 
410    /* =====================================================================
411       Space is: Sum over all structures(sizeof(structure) * structure-cnt))
412       ===================================================================== */
413    space = ((unsigned long) DefgenericBinaryData(theEnv)->ModuleCount * sizeof(BSAVE_DEFGENERIC_MODULE)) +
414            ((unsigned long) DefgenericBinaryData(theEnv)->GenericCount * sizeof(BSAVE_GENERIC)) +
415            ((unsigned long) DefgenericBinaryData(theEnv)->MethodCount * sizeof(BSAVE_METHOD)) +
416            ((unsigned long) DefgenericBinaryData(theEnv)->RestrictionCount * sizeof(BSAVE_RESTRICTION)) +
417            ((unsigned long) DefgenericBinaryData(theEnv)->TypeCount * sizeof(unsigned long));
418 
419    /* ================================================================
420       Write out the total amount of space required:  modules,headers,
421       methods, restrictions, types
422       ================================================================ */
423    GenWrite((void *) &space,sizeof(size_t),fp);
424 
425    /* ======================================
426       Write out the generic function modules
427       ====================================== */
428    DefgenericBinaryData(theEnv)->GenericCount = 0L;
429    theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,NULL);
430    while (theModule != NULL)
431      {
432       theModuleItem = (DEFGENERIC_MODULE *)
433                       GetModuleItem(theEnv,theModule,FindModuleItem(theEnv,"defgeneric")->moduleIndex);
434       AssignBsaveDefmdlItemHdrVals(&dummy_generic_module.header,
435                                            &theModuleItem->header);
436       GenWrite((void *) &dummy_generic_module,
437                sizeof(BSAVE_DEFGENERIC_MODULE),fp);
438       theModule = (struct defmodule *) EnvGetNextDefmodule(theEnv,(void *) theModule);
439      }
440 
441 
442    /* ======================================
443       Write out the generic function headers
444       ====================================== */
445    DefgenericBinaryData(theEnv)->MethodCount = 0L;
446    DoForAllConstructs(theEnv,BsaveDefgenericHeader,DefgenericData(theEnv)->DefgenericModuleIndex,
447                       FALSE,(void *) fp);
448 
449    /* =====================
450       Write out the methods
451       ===================== */
452    DefgenericBinaryData(theEnv)->RestrictionCount = 0L;
453    DoForAllConstructs(theEnv,BsaveMethods,DefgenericData(theEnv)->DefgenericModuleIndex,
454                       FALSE,(void *) fp);
455 
456    /* =================================
457       Write out the method restrictions
458       ================================= */
459    DefgenericBinaryData(theEnv)->TypeCount = 0L;
460    DoForAllConstructs(theEnv,BsaveMethodRestrictions,DefgenericData(theEnv)->DefgenericModuleIndex,
461                       FALSE,(void *) fp);
462 
463    /* =============================================================
464       Finally, write out the type lists for the method restrictions
465       ============================================================= */
466    DoForAllConstructs(theEnv,BsaveRestrictionTypes,DefgenericData(theEnv)->DefgenericModuleIndex,
467                       FALSE,(void *) fp);
468 
469    RestoreBloadCount(theEnv,&DefgenericBinaryData(theEnv)->ModuleCount);
470    RestoreBloadCount(theEnv,&DefgenericBinaryData(theEnv)->GenericCount);
471    RestoreBloadCount(theEnv,&DefgenericBinaryData(theEnv)->MethodCount);
472    RestoreBloadCount(theEnv,&DefgenericBinaryData(theEnv)->RestrictionCount);
473    RestoreBloadCount(theEnv,&DefgenericBinaryData(theEnv)->TypeCount);
474   }
475 
476 /***************************************************
477   NAME         : BsaveDefgenericHeader
478   DESCRIPTION  : Bsaves a generic function header
479   INPUTS       : 1) The defgeneric
480                  2) Output data file pointer
481   RETURNS      : Nothing useful
482   SIDE EFFECTS : Defgeneric header saved
483   NOTES        : None
484  ***************************************************/
BsaveDefgenericHeader(void * theEnv,struct constructHeader * theDefgeneric,void * userBuffer)485 static void BsaveDefgenericHeader(
486   void *theEnv,
487   struct constructHeader *theDefgeneric,
488   void *userBuffer)
489   {
490    DEFGENERIC *gfunc = (DEFGENERIC *) theDefgeneric;
491    BSAVE_GENERIC dummy_generic;
492 
493    AssignBsaveConstructHeaderVals(&dummy_generic.header,&gfunc->header);
494    dummy_generic.mcnt = gfunc->mcnt;
495    if (gfunc->methods != NULL)
496      {
497       dummy_generic.methods = DefgenericBinaryData(theEnv)->MethodCount;
498       DefgenericBinaryData(theEnv)->MethodCount += (long) gfunc->mcnt;
499      }
500    else
501      dummy_generic.methods = -1L;
502    GenWrite((void *) &dummy_generic,(unsigned long) sizeof(BSAVE_GENERIC),(FILE *) userBuffer);
503   }
504 
505 /***************************************************
506   NAME         : BsaveMethods
507   DESCRIPTION  : Bsaves defgeneric methods
508   INPUTS       : 1) The defgeneric
509                  2) Output data file pointer
510   RETURNS      : Nothing useful
511   SIDE EFFECTS : Defgeneric methods saved
512   NOTES        : None
513  ***************************************************/
BsaveMethods(void * theEnv,struct constructHeader * theDefgeneric,void * userBuffer)514 static void BsaveMethods(
515   void *theEnv,
516   struct constructHeader *theDefgeneric,
517   void *userBuffer)
518   {
519    DEFGENERIC *gfunc = (DEFGENERIC *) theDefgeneric;
520    DEFMETHOD *meth;
521    BSAVE_METHOD dummy_method;
522    long i;
523 
524    for (i = 0 ; i < gfunc->mcnt ; i++)
525      {
526       meth = &gfunc->methods[i];
527       dummy_method.index = meth->index;
528       dummy_method.restrictionCount = meth->restrictionCount;
529       dummy_method.minRestrictions = meth->minRestrictions;
530       dummy_method.maxRestrictions = meth->maxRestrictions;
531       dummy_method.localVarCount = meth->localVarCount;
532       dummy_method.system = meth->system;
533       if (meth->restrictions != NULL)
534         {
535          dummy_method.restrictions = DefgenericBinaryData(theEnv)->RestrictionCount;
536          DefgenericBinaryData(theEnv)->RestrictionCount += meth->restrictionCount;
537         }
538       else
539         dummy_method.restrictions = -1L;
540       if (meth->actions != NULL)
541         {
542          dummy_method.actions = ExpressionData(theEnv)->ExpressionCount;
543          ExpressionData(theEnv)->ExpressionCount += ExpressionSize(meth->actions);
544         }
545       else
546         dummy_method.actions = -1L;
547       GenWrite((void *) &dummy_method,sizeof(BSAVE_METHOD),(FILE *) userBuffer);
548      }
549   }
550 
551 /******************************************************
552   NAME         : BsaveMethodRestrictions
553   DESCRIPTION  : Bsaves defgeneric methods' retrictions
554   INPUTS       : 1) The defgeneric
555                  2) Output data file pointer
556   RETURNS      : Nothing useful
557   SIDE EFFECTS : Defgeneric methods' restrictions saved
558   NOTES        : None
559  ******************************************************/
BsaveMethodRestrictions(void * theEnv,struct constructHeader * theDefgeneric,void * userBuffer)560 static void BsaveMethodRestrictions(
561   void *theEnv,
562   struct constructHeader *theDefgeneric,
563   void *userBuffer)
564   {
565    DEFGENERIC *gfunc = (DEFGENERIC *) theDefgeneric;
566    BSAVE_RESTRICTION dummy_restriction;
567    RESTRICTION *rptr;
568    short i,j;
569 
570    for (i = 0 ; i < gfunc->mcnt ; i++)
571      {
572       for (j = 0 ; j < gfunc->methods[i].restrictionCount ; j++)
573         {
574          rptr = &gfunc->methods[i].restrictions[j];
575          dummy_restriction.tcnt = rptr->tcnt;
576          if (rptr->types != NULL)
577            {
578             dummy_restriction.types = DefgenericBinaryData(theEnv)->TypeCount;
579             DefgenericBinaryData(theEnv)->TypeCount += rptr->tcnt;
580            }
581          else
582            dummy_restriction.types = -1L;
583          if (rptr->query != NULL)
584            {
585             dummy_restriction.query = ExpressionData(theEnv)->ExpressionCount;
586             ExpressionData(theEnv)->ExpressionCount += ExpressionSize(rptr->query);
587            }
588          else
589            dummy_restriction.query = -1L;
590          GenWrite((void *) &dummy_restriction,
591                   sizeof(BSAVE_RESTRICTION),(FILE *) userBuffer);
592         }
593      }
594   }
595 
596 /*************************************************************
597   NAME         : BsaveRestrictionTypes
598   DESCRIPTION  : Bsaves defgeneric methods' retrictions' types
599   INPUTS       : 1) The defgeneric
600                  2) Output data file pointer
601   RETURNS      : Nothing useful
602   SIDE EFFECTS : Defgeneric methods' restrictions' types saved
603   NOTES        : None
604  *************************************************************/
BsaveRestrictionTypes(void * theEnv,struct constructHeader * theDefgeneric,void * userBuffer)605 static void BsaveRestrictionTypes(
606   void *theEnv,
607   struct constructHeader *theDefgeneric,
608   void *userBuffer)
609   {
610    DEFGENERIC *gfunc = (DEFGENERIC *) theDefgeneric;
611    long dummy_type;
612    RESTRICTION *rptr;
613    short i,j,k;
614 #if MAC_XCD
615 #pragma unused(theEnv)
616 #endif
617 
618    for (i = 0 ; i < gfunc->mcnt ; i++)
619      {
620       for (j = 0 ; j < gfunc->methods[i].restrictionCount ; j++)
621         {
622          rptr = &gfunc->methods[i].restrictions[j];
623          for (k = 0 ; k < rptr->tcnt ; k++)
624            {
625 #if OBJECT_SYSTEM
626             dummy_type = DefclassIndex(rptr->types[k]);
627 #else
628             dummy_type = (long) ((INTEGER_HN *) rptr->types[k])->contents;
629 #endif
630             GenWrite(&dummy_type,sizeof(long),(FILE *) userBuffer);
631            }
632         }
633      }
634   }
635 
636 #endif
637 
638 /***********************************************************************
639   NAME         : BloadStorageGenerics
640   DESCRIPTION  : This routine space required for generic function
641                    structures and allocates space for them
642   INPUTS       : Nothing
643   RETURNS      : Nothing useful
644   SIDE EFFECTS : Arrays allocated and set
645   NOTES        : This routine makes no attempt to reset any pointers
646                    within the structures
647  ***********************************************************************/
BloadStorageGenerics(void * theEnv)648 static void BloadStorageGenerics(
649   void *theEnv)
650   {
651    size_t space;
652    long counts[5];
653 
654    GenReadBinary(theEnv,(void *) &space,sizeof(size_t));
655    if (space == 0L)
656      return;
657    GenReadBinary(theEnv,(void *) counts,space);
658    DefgenericBinaryData(theEnv)->ModuleCount = counts[0];
659    DefgenericBinaryData(theEnv)->GenericCount = counts[1];
660    DefgenericBinaryData(theEnv)->MethodCount = counts[2];
661    DefgenericBinaryData(theEnv)->RestrictionCount = counts[3];
662    DefgenericBinaryData(theEnv)->TypeCount = counts[4];
663    if (DefgenericBinaryData(theEnv)->ModuleCount != 0L)
664      {
665       space = (sizeof(DEFGENERIC_MODULE) * DefgenericBinaryData(theEnv)->ModuleCount);
666       DefgenericBinaryData(theEnv)->ModuleArray = (DEFGENERIC_MODULE *) genalloc(theEnv,space);
667      }
668    else
669      return;
670    if (DefgenericBinaryData(theEnv)->GenericCount != 0L)
671      {
672       space = (sizeof(DEFGENERIC) * DefgenericBinaryData(theEnv)->GenericCount);
673       DefgenericBinaryData(theEnv)->DefgenericArray = (DEFGENERIC *) genalloc(theEnv,space);
674      }
675    else
676      return;
677    if (DefgenericBinaryData(theEnv)->MethodCount != 0L)
678      {
679       space = (sizeof(DEFMETHOD) * DefgenericBinaryData(theEnv)->MethodCount);
680       DefgenericBinaryData(theEnv)->MethodArray = (DEFMETHOD *) genalloc(theEnv,space);
681      }
682    else
683      return;
684    if (DefgenericBinaryData(theEnv)->RestrictionCount != 0L)
685      {
686       space = (sizeof(RESTRICTION) * DefgenericBinaryData(theEnv)->RestrictionCount);
687       DefgenericBinaryData(theEnv)->RestrictionArray = (RESTRICTION *) genalloc(theEnv,space);
688      }
689    else
690      return;
691    if (DefgenericBinaryData(theEnv)->TypeCount != 0L)
692      {
693       space = (sizeof(void *) * DefgenericBinaryData(theEnv)->TypeCount);
694       DefgenericBinaryData(theEnv)->TypeArray = (void * *) genalloc(theEnv,space);
695      }
696   }
697 
698 /*********************************************************************
699   NAME         : BloadGenerics
700   DESCRIPTION  : This routine reads generic function information from
701                  a binary file in four chunks:
702                  Generic-header array
703                  Method array
704                  Method restrictions array
705                  Restriction types array
706 
707                  This routine moves through the generic function
708                    binary arrays updating pointers
709   INPUTS       : None
710   RETURNS      : Nothing useful
711   SIDE EFFECTS : Pointers reset from array indices
712   NOTES        : Assumes all loading is finished
713  ********************************************************************/
BloadGenerics(void * theEnv)714 static void BloadGenerics(
715   void *theEnv)
716   {
717    size_t space;
718 
719    GenReadBinary(theEnv,(void *) &space,sizeof(size_t));
720    if (DefgenericBinaryData(theEnv)->ModuleCount == 0L)
721      return;
722    BloadandRefresh(theEnv,DefgenericBinaryData(theEnv)->ModuleCount,sizeof(BSAVE_DEFGENERIC_MODULE),UpdateGenericModule);
723    if (DefgenericBinaryData(theEnv)->GenericCount == 0L)
724      return;
725    BloadandRefresh(theEnv,DefgenericBinaryData(theEnv)->GenericCount,sizeof(BSAVE_GENERIC),UpdateGeneric);
726    BloadandRefresh(theEnv,DefgenericBinaryData(theEnv)->MethodCount,sizeof(BSAVE_METHOD),UpdateMethod);
727    BloadandRefresh(theEnv,DefgenericBinaryData(theEnv)->RestrictionCount,sizeof(BSAVE_RESTRICTION),UpdateRestriction);
728    BloadandRefresh(theEnv,DefgenericBinaryData(theEnv)->TypeCount,sizeof(long),UpdateType);
729   }
730 
731 /*********************************************
732   Bload update routines for generic structures
733  *********************************************/
UpdateGenericModule(void * theEnv,void * buf,long obji)734 static void UpdateGenericModule(
735   void *theEnv,
736   void *buf,
737   long obji)
738   {
739    BSAVE_DEFGENERIC_MODULE *bdptr;
740 
741    bdptr = (BSAVE_DEFGENERIC_MODULE *) buf;
742    UpdateDefmoduleItemHeader(theEnv,&bdptr->header,&DefgenericBinaryData(theEnv)->ModuleArray[obji].header,
743                              (int) sizeof(DEFGENERIC),(void *) DefgenericBinaryData(theEnv)->DefgenericArray);
744   }
745 
UpdateGeneric(void * theEnv,void * buf,long obji)746 static void UpdateGeneric(
747   void *theEnv,
748   void *buf,
749   long obji)
750   {
751    BSAVE_GENERIC *bgp;
752    DEFGENERIC *gp;
753 
754    bgp = (BSAVE_GENERIC *) buf;
755    gp = (DEFGENERIC *) &DefgenericBinaryData(theEnv)->DefgenericArray[obji];
756 
757    UpdateConstructHeader(theEnv,&bgp->header,&gp->header,
758                          (int) sizeof(DEFGENERIC_MODULE),(void *) DefgenericBinaryData(theEnv)->ModuleArray,
759                          (int) sizeof(DEFGENERIC),(void *) DefgenericBinaryData(theEnv)->DefgenericArray);
760    DefgenericBinaryData(theEnv)->DefgenericArray[obji].busy = 0;
761 #if DEBUGGING_FUNCTIONS
762    DefgenericBinaryData(theEnv)->DefgenericArray[obji].trace = DefgenericData(theEnv)->WatchGenerics;
763 #endif
764    DefgenericBinaryData(theEnv)->DefgenericArray[obji].methods = MethodPointer(bgp->methods);
765    DefgenericBinaryData(theEnv)->DefgenericArray[obji].mcnt = bgp->mcnt;
766    DefgenericBinaryData(theEnv)->DefgenericArray[obji].new_index = 0;
767   }
768 
UpdateMethod(void * theEnv,void * buf,long obji)769 static void UpdateMethod(
770   void *theEnv,
771   void *buf,
772   long obji)
773   {
774    BSAVE_METHOD *bmth;
775 
776    bmth = (BSAVE_METHOD *) buf;
777    DefgenericBinaryData(theEnv)->MethodArray[obji].index = bmth->index;
778    DefgenericBinaryData(theEnv)->MethodArray[obji].busy = 0;
779 #if DEBUGGING_FUNCTIONS
780    DefgenericBinaryData(theEnv)->MethodArray[obji].trace = DefgenericData(theEnv)->WatchMethods;
781 #endif
782    DefgenericBinaryData(theEnv)->MethodArray[obji].restrictionCount = bmth->restrictionCount;
783    DefgenericBinaryData(theEnv)->MethodArray[obji].minRestrictions = bmth->minRestrictions;
784    DefgenericBinaryData(theEnv)->MethodArray[obji].maxRestrictions = bmth->maxRestrictions;
785    DefgenericBinaryData(theEnv)->MethodArray[obji].localVarCount = bmth->localVarCount;
786    DefgenericBinaryData(theEnv)->MethodArray[obji].system = bmth->system;
787    DefgenericBinaryData(theEnv)->MethodArray[obji].restrictions = RestrictionPointer(bmth->restrictions);
788    DefgenericBinaryData(theEnv)->MethodArray[obji].actions = ExpressionPointer(bmth->actions);
789    DefgenericBinaryData(theEnv)->MethodArray[obji].ppForm = NULL;
790    DefgenericBinaryData(theEnv)->MethodArray[obji].usrData = NULL;
791   }
792 
UpdateRestriction(void * theEnv,void * buf,long obji)793 static void UpdateRestriction(
794   void *theEnv,
795   void *buf,
796   long obji)
797   {
798    BSAVE_RESTRICTION *brp;
799 
800    brp = (BSAVE_RESTRICTION *) buf;
801    DefgenericBinaryData(theEnv)->RestrictionArray[obji].tcnt = brp->tcnt;
802    DefgenericBinaryData(theEnv)->RestrictionArray[obji].types = TypePointer(brp->types);
803    DefgenericBinaryData(theEnv)->RestrictionArray[obji].query = ExpressionPointer(brp->query);
804   }
805 
UpdateType(void * theEnv,void * buf,long obji)806 static void UpdateType(
807   void *theEnv,
808   void *buf,
809   long obji)
810   {
811 #if OBJECT_SYSTEM
812    DefgenericBinaryData(theEnv)->TypeArray[obji] = (void *) DefclassPointer(* (long *) buf);
813 #else
814    if ((* (long *) buf) > (long) INSTANCE_TYPE_CODE)
815      {
816       PrintWarningID(theEnv,"GENRCBIN",1,FALSE);
817       EnvPrintRouter(theEnv,WWARNING,"COOL not installed!  User-defined class\n");
818       EnvPrintRouter(theEnv,WWARNING,"  in method restriction substituted with OBJECT.\n");
819       DefgenericBinaryData(theEnv)->TypeArray[obji] = (void *) EnvAddLong(theEnv,(long long) OBJECT_TYPE_CODE);
820      }
821    else
822      DefgenericBinaryData(theEnv)->TypeArray[obji] = (void *) EnvAddLong(theEnv,* (long *) buf);
823    IncrementIntegerCount((INTEGER_HN *) DefgenericBinaryData(theEnv)->TypeArray[obji]);
824 #endif
825   }
826 
827 /***************************************************************
828   NAME         : ClearBloadGenerics
829   DESCRIPTION  : Release all binary-loaded generic function
830                    structure arrays
831                  Resets generic function list to NULL
832   INPUTS       : None
833   RETURNS      : Nothing useful
834   SIDE EFFECTS : Memory cleared
835   NOTES        : Generic function name symbol counts decremented
836  ***************************************************************/
ClearBloadGenerics(void * theEnv)837 static void ClearBloadGenerics(
838   void *theEnv)
839   {
840    register long i;
841    size_t space;
842 
843    space = (sizeof(DEFGENERIC_MODULE) * DefgenericBinaryData(theEnv)->ModuleCount);
844    if (space == 0L)
845      return;
846    genfree(theEnv,(void *) DefgenericBinaryData(theEnv)->ModuleArray,space);
847    DefgenericBinaryData(theEnv)->ModuleArray = NULL;
848    DefgenericBinaryData(theEnv)->ModuleCount = 0L;
849 
850    for (i = 0 ; i < DefgenericBinaryData(theEnv)->GenericCount ; i++)
851      UnmarkConstructHeader(theEnv,&DefgenericBinaryData(theEnv)->DefgenericArray[i].header);
852 
853    space = (sizeof(DEFGENERIC) * DefgenericBinaryData(theEnv)->GenericCount);
854    if (space == 0L)
855      return;
856    genfree(theEnv,(void *) DefgenericBinaryData(theEnv)->DefgenericArray,space);
857    DefgenericBinaryData(theEnv)->DefgenericArray = NULL;
858    DefgenericBinaryData(theEnv)->GenericCount = 0L;
859 
860    space = (sizeof(DEFMETHOD) * DefgenericBinaryData(theEnv)->MethodCount);
861    if (space == 0L)
862      return;
863    genfree(theEnv,(void *) DefgenericBinaryData(theEnv)->MethodArray,space);
864    DefgenericBinaryData(theEnv)->MethodArray = NULL;
865    DefgenericBinaryData(theEnv)->MethodCount = 0L;
866 
867    space = (sizeof(RESTRICTION) * DefgenericBinaryData(theEnv)->RestrictionCount);
868    if (space == 0L)
869      return;
870    genfree(theEnv,(void *) DefgenericBinaryData(theEnv)->RestrictionArray,space);
871    DefgenericBinaryData(theEnv)->RestrictionArray = NULL;
872    DefgenericBinaryData(theEnv)->RestrictionCount = 0L;
873 
874 #if ! OBJECT_SYSTEM
875    for (i = 0 ; i < DefgenericBinaryData(theEnv)->TypeCount ; i++)
876      DecrementIntegerCount(theEnv,(INTEGER_HN *) DefgenericBinaryData(theEnv)->TypeArray[i]);
877 #endif
878    space = (sizeof(void *) * DefgenericBinaryData(theEnv)->TypeCount);
879    if (space == 0L)
880      return;
881    genfree(theEnv,(void *) DefgenericBinaryData(theEnv)->TypeArray,space);
882    DefgenericBinaryData(theEnv)->TypeArray = NULL;
883    DefgenericBinaryData(theEnv)->TypeCount = 0L;
884   }
885 
886 #endif
887 
888