1 /* File storage.c.  Contains functions to allocate and free memory.
2    For certain commonly used sizes, memory is never actually freed; rather
3    a linked list of allocated but currently unused segments of those sizes
4    are maintained, and new allocations are taken from these linked lists
5    when possible.  Prior to any allocations, the procedure
6    InitializeStorageManager must be invoked; this merely informs the
7    routines of the degree of the group.  The memory allocation functions
8    are as follows; for each, there is a corresponding function to free
9    memory.  All allocation functions return a pointer to the memory
10    allocated; an allocation failure terminates the program.
11 
12         Function                Struct size or            Array size
13                               array component size
14 
15       allocIntArrayBaseSize      sizeof(Int)              options.maxBaseSize
16       allocIntArrayDegree        sizeof(Int)              degree+2
17       allocBooleanArrayDegree    sizeof(char)             degree+2
18       allocPtrArrayWordSize      sizeof(Permutation *)    options.maxWordLength
19       allocPtrArrayBaseSize      sizeof(Permutation *)    options.maxBaseSize
20       allocPtrArrayDegree        sizeof(Permutation *)    degree+2
21       allocPermutation           sizeof(Permutation)
22       allocPermGroup             sizeof(PermGroup)
23       allocPartitionStack        sizeof(PartitionStack)
24       allocPointSet              sizeof(PointSet)
25       allocWord                  sizeof(Word) */
26 
27 #include <stddef.h>
28 #include <stdlib.h>
29 #include "group.h"
30 #include "errmesg.h"
31 
32 extern GroupOptions options;
33 
CHECK(storag)34 CHECK( storag)
35 
36 static Unsigned degree;
37 
38 /*-------------------------- initializeStorageManager ---------------------*/
39 
40 void initializeStorageManager( Unsigned degreeOfGroup)
41 {
42    degree = degreeOfGroup;
43 }
44 
45 
46 /*-------------------------- allocIntArrayDegree --------------------------*/
47 
allocIntArrayDegree(void)48 UnsignedS *allocIntArrayDegree( void)
49 {
50    UnsignedS *address;
51 
52 #ifdef HIGH_MEM_OPTION
53       address = (UnsignedS *) highMemMalloc( (degree+2) * sizeof(UnsignedS) );
54 #else
55       address = (UnsignedS *) malloc( (degree+2) * sizeof(UnsignedS) );
56 #endif
57       if ( address == NULL )
58          ERROR( "allocIntArrayDegree", "Out of memory");
59 
60    return address;
61 }
62 
63 
64 /*-------------------------- freeIntArrayDegree ---------------------------*/
65 
freeIntArrayDegree(UnsignedS * address)66 void freeIntArrayDegree( UnsignedS *address)
67 {
68    free(address);
69 }
70 
71 
72 /*-------------------------- allocBooleanArrayDegree ----------------------*/
73 
allocBooleanArrayDegree(void)74 char *allocBooleanArrayDegree( void)
75 {
76    char *address;
77 
78       address = (char *) malloc( (degree+2) * sizeof(char) );
79       if ( address == NULL )
80          ERROR( "allocBooleanArrayDegree", "Out of memory");
81 
82    return address;
83 }
84 
85 
86 /*-------------------------- freeBooleanArrayDegree -----------------------*/
87 
freeBooleanArrayDegree(char * address)88 void freeBooleanArrayDegree( char *address)
89 {
90    free(address);
91 }
92 
93 
94 /*-------------------------- allocBooleanArrayBaseSize ----------------------*/
95 
allocBooleanArrayBaseSize(void)96 char *allocBooleanArrayBaseSize( void)
97 {
98    char *address;
99 
100       address = (char *) malloc( (options.maxBaseSize+2) * sizeof(char) );
101       if ( address == NULL )
102          ERROR( "allocBooleanArrayBaseSize", "Out of memory");
103 
104    return address;
105 }
106 
107 
108 /*-------------------------- freeBooleanArrayBaseSize -----------------------*/
109 
freeBooleanArrayBaseSize(char * address)110 void freeBooleanArrayBaseSize( char *address)
111 {
112    free(address);
113 }
114 
115 
116 /*-------------------------- allocPtrArrayDegree --------------------------*/
117 
allocPtrArrayDegree(void)118 void *allocPtrArrayDegree( void)
119 {
120    void *address;
121 
122 #ifdef HIGH_MEM_OPTION
123    address = highMemMalloc( (degree+2) * sizeof (void *) );
124 #else
125    address = malloc( (degree+2) * sizeof (void *) );
126 #endif
127    if ( address == NULL )
128          ERROR( "allocPtrArrayDegree", "Out of memory");
129 
130    return address;
131 }
132 
133 
134 /*-------------------------- freePtrArrayDegree ---------------------------*/
135 
freePtrArrayDegree(void * address)136 void freePtrArrayDegree( void *address)
137 {
138    free(address);
139 }
140 
141 
142 /*-------------------------- allocPtrArrayWordSize ------------------------*/
143 
allocPtrArrayWordSize(void)144 void *allocPtrArrayWordSize( void)
145 {
146    void *address;
147 
148    address = malloc( (options.maxWordLength+2) * sizeof (void *) );
149    if ( address == NULL )
150       ERROR( "allocPtrArrayWordSize", "Out of memory");
151 
152    return address;
153 }
154 
155 
156 /*-------------------------- freePtrArrayWordSize -------------------------*/
157 
freePtrArrayWordSize(void * address)158 void freePtrArrayWordSize( void *address)
159 {
160    free(address);
161 }
162 
163 
164 /*-------------------------- allocPtrArrayBaseSize ------------------------*/
165 
allocPtrArrayBaseSize(void)166 void *allocPtrArrayBaseSize( void)
167 {
168    void *address;
169 
170    address = malloc( (options.maxBaseSize+2) * sizeof (void *) );
171    if ( address == NULL )
172       ERROR( "allocPtrArrayBaseSize", "Out of memory");
173 
174    return address;
175 }
176 
177 
178 /*-------------------------- freePtrArrayBaseSize -------------------------*/
179 
freePtrArrayBaseSize(void * address)180 void freePtrArrayBaseSize( void *address)
181 {
182    free(address);
183 }
184 
185 
186 /*-------------------------- allocIntArrayBaseSize ------------------------*/
187 
allocIntArrayBaseSize(void)188 UnsignedS *allocIntArrayBaseSize( void)
189 {
190    UnsignedS *address;
191 
192    address = (UnsignedS *) malloc( (options.maxBaseSize+2) * sizeof(UnsignedS) );
193    if ( address == NULL )
194       ERROR( "allocIntArrayBaseSize", "Out of memory");
195 
196    return address;
197 }
198 
199 
200 /*-------------------------- freeIntArrayBaseSize -------------------------*/
201 
freeIntArrayBaseSize(UnsignedS * address)202 void freeIntArrayBaseSize( UnsignedS *address)
203 {
204    free(address);
205 }
206 
207 
208 /*-------------------------- allocLongArrayBaseSize ------------------------*/
209 
allocLongArrayBaseSize(void)210 unsigned long *allocLongArrayBaseSize( void)
211 {
212    unsigned long *address;
213 
214    address = (unsigned long *) malloc( (options.maxBaseSize+2) * sizeof(unsigned long) );
215    if ( address == NULL )
216       ERROR( "allocLongArrayBaseSize", "Out of memory");
217 
218    return address;
219 }
220 
221 
222 /*-------------------------- freeLongArrayBaseSize -------------------------*/
223 
freeLongArrayBaseSize(unsigned long * address)224 void freeLongArrayBaseSize( unsigned long *address)
225 {
226    free(address);
227 }
228 
229 
230 /*-------------------------- allocPermutation -----------------------------*/
231 
allocPermutation(void)232 Permutation *allocPermutation( void)
233 {
234    Permutation *address;
235    Unsigned essentialArraySize;
236 
237    address = (Permutation *) malloc( sizeof(Permutation) );
238    if ( address == NULL )
239       ERROR( "allocPermutation", "Out of memory");
240 
241    address->name[0] = '\0';
242    address->image = NULL;
243    address->invImage = NULL;
244    address->invPermutation = NULL;
245    address->word = NULL;
246    address->occurHeader = NULL;
247    essentialArraySize = (options.maxBaseSize+1) / 32 + 1;
248    address->essential = (unsigned long *)
249                         malloc( essentialArraySize * sizeof(unsigned long) );
250    if ( address->essential == NULL )
251       ERROR( "allocPermutation", "Out of memory");
252    return address;
253 }
254 
255 /*-------------------------- freePermutation ------------------------------*/
256 
freePermutation(Permutation * address)257 void freePermutation( Permutation *address)
258 {
259    free(address->essential);
260    free(address);
261 }
262 
263 
264 /*-------------------------- allocPermGroup -------------------------------*/
265 
allocPermGroup(void)266 PermGroup *allocPermGroup( void)
267 {
268    PermGroup *address;
269 
270    address = (PermGroup *) malloc( sizeof(PermGroup) );
271    if ( address == NULL )
272       ERROR( "allocPermGroup", "Out of memory");
273 
274    address->order = NULL;
275    address->base = NULL;
276    address->basicOrbLen = NULL;
277    address->basicOrbit = NULL;
278    address->completeOrbit = NULL;
279    address->orbNumberOfPt = NULL;
280    address->startOfOrbitNo = NULL;
281    address->schreierVec = NULL;
282    address->generator = NULL;
283    address->omega = NULL;
284    address->invOmega = NULL;
285    address->relator = NULL;
286    return address;
287 }
288 
289 
290 /*-------------------------- freePermGroup --------------------------------*/
291 
freePermGroup(PermGroup * address)292 void freePermGroup( PermGroup *address)
293 {
294    Permutation *current;
295 
296    if (address->order)
297       free(address->order);
298    while ( address->generator ) {
299       current = address->generator;
300       address->generator = current->next;
301       freePermutation(current);
302    }
303    freeIntArrayBaseSize(address->base);
304    freeIntArrayBaseSize(address->basicOrbLen);
305    if (address->basicOrbit)
306      freePtrArrayBaseSize(address->basicOrbit);
307    if (address->schreierVec)
308      freePtrArrayBaseSize(address->schreierVec);
309    free(address);
310 }
311 
312 
313 /*-------------------------- allocPartition -------------------------------*/
314 
allocPartition(void)315 Partition *allocPartition( void)
316 {
317    Partition *address;
318 
319    address = (Partition *) malloc( sizeof(Partition) );
320    if ( address == NULL )
321       ERROR( "allocPartition", "Out of memory");
322 
323    return address;
324 }
325 
326 
327 /*-------------------------- freePartition ---------------------------*/
328 
freePartition(Partition * address)329 void freePartition( Partition *address)
330 {
331    free(address);
332 }
333 
334 
335 /*-------------------------- allocPartitionStack --------------------------*/
336 
allocPartitionStack(void)337 PartitionStack *allocPartitionStack( void)
338 {
339    PartitionStack *address;
340 
341    address = (PartitionStack *) malloc( sizeof(PartitionStack) );
342    if ( address == NULL )
343       ERROR( "allocPartitionStack", "Out of memory");
344 
345    return address;
346 }
347 
348 
349 /*-------------------------- freePartitionStack ---------------------------*/
350 
freePartitionStack(PartitionStack * address)351 void freePartitionStack( PartitionStack *address)
352 {
353    free(address);
354 }
355 
356 
357 /*-------------------------- allocRBase -----------------------------------*/
358 
allocRBase(void)359 RBase *allocRBase( void)
360 {
361    RBase *address;
362 
363    address = (RBase *) malloc( sizeof(RBase) );
364    if ( address == NULL )
365       ERROR( "allocRBase", "Out of memory");
366 
367    return address;
368 }
369 
370 
371 /*-------------------------- freeRBase ------------------------------------*/
372 
freeRBase(RBase * address)373 void freeRBase( RBase *address)
374 {
375    free(address);
376 }
377 
378 
379 /*-------------------------- allocRPriorityQueue --------------------------*/
380 
allocRPriorityQueue(void)381 RPriorityQueue *allocRPriorityQueue( void)
382 {
383    RPriorityQueue *address;
384 
385    address = (RPriorityQueue *) malloc( sizeof(RPriorityQueue) );
386    if ( address == NULL )
387       ERROR( "allocRPriorityQueue", "Out of memory");
388 
389    return address;
390 }
391 
392 
393 /*-------------------------- freeRPriorityQueue ---------------------------*/
394 
freeRPriorityQueue(RPriorityQueue * address)395 void freeRPriorityQueue( RPriorityQueue *address)
396 {
397    free(address);
398 }
399 
400 
401 /*-------------------------- allocPointSet --------------------------------*/
402 
allocPointSet(void)403 PointSet *allocPointSet( void)
404 {
405    PointSet *address;
406 
407    address = (PointSet *) malloc( sizeof(PointSet) );
408    if ( address == NULL )
409       ERROR( "allocPointSet", "Out of memory");
410 
411    return address;
412 }
413 
414 
415 /*-------------------------- freePointSet ---------------------------------*/
416 
freePointSet(PointSet * address)417 void freePointSet( PointSet *address)
418 {
419    free(address);
420 }
421 
422 
423 /*-------------------------- allocFactoredInt -----------------------------*/
424 
allocFactoredInt(void)425 FactoredInt *allocFactoredInt( void)
426 {
427    FactoredInt *address;
428 
429    address = (FactoredInt *) malloc( sizeof(FactoredInt) );
430    if ( address == NULL )
431       ERROR( "allocFactoredInt", "Out of memory");
432 
433    return address;
434 }
435 
436 
437 /*-------------------------- freeFactoredInt ------------------------------*/
438 
freeFactoredInt(FactoredInt * address)439 void freeFactoredInt( FactoredInt *address)
440 {
441    free(address);
442 }
443 
444 
445 /*-------------------------- allocWord ------------------------------------*/
446 
allocWord(void)447 Word *allocWord( void)
448 {
449    Word *address;
450 
451    address = (Word *) malloc( sizeof(Word) );
452    if ( address == NULL )
453       ERROR( "allocWord", "Out of memory");
454 
455    return address;
456 }
457 
458 
459 /*-------------------------- freeWord -------------------------------------*/
460 
freeWord(Word * address)461 void freeWord( Word *address)
462 {
463    free(address);
464 }
465 
466 
467 /*-------------------------- allocRefinementArrayDegree -------------------*/
468 
allocRefinementArrayDegree(void)469 Refinement *allocRefinementArrayDegree( void)
470 {
471    Refinement *address;
472 
473    address = (Refinement *) malloc( (degree+2) * sizeof(Refinement) );
474    if ( address == NULL )
475       ERROR( "allocRefinementArrayDegree", "Out of memory");
476 
477    return address;
478 }
479 
480 
481 /*-------------------------- freeRefinementArrayDegree --------------------*/
482 
freeRefinementArrayDegree(Refinement * address)483 void freeRefinementArrayDegree( Refinement *address)
484 {
485    free(address);
486 }
487 
488 /*-------------------------- allocRelator ---------------------------------*/
489 
allocRelator(void)490 Relator *allocRelator( void)
491 {
492    Relator *address;
493 
494    address = (Relator *) malloc( sizeof(Relator) );
495    if ( address == NULL )
496       ERROR( "allocRelator", "Out of memory");
497 
498    address->length = 0;
499    address->rel = NULL;
500    address->fRel = NULL;
501    address->bRel = NULL;
502    return address;
503 }
504 
505 /*-------------------------- freeRelator ----------------------------------*/
506 
freeRelator(Relator * address)507 void freeRelator( Relator *address)
508 {
509    free(address);
510 }
511 
512 
513 /*-------------------------- allocOccurenceOfGen --------------------------*/
514 
allocOccurenceOfGen(void)515 OccurenceOfGen *allocOccurenceOfGen( void)
516 {
517    OccurenceOfGen *address;
518 
519    address = (OccurenceOfGen *) malloc( sizeof(OccurenceOfGen) );
520    if ( address == NULL )
521       ERROR( "allocOccurenceOfGen", "Out of memory");
522 
523    return address;
524 }
525 
526 /*-------------------------- freeOccurenceOfGen ---------------------------*/
527 
freeOccurenceOfGen(OccurenceOfGen * address)528 void freeOccurenceOfGen( OccurenceOfGen *address)
529 {
530    free(address);
531 }
532 
533 
534 /*-------------------------- allocField -------------------------------*/
535 
allocField(void)536 Field *allocField( void)
537 {
538    Field *address;
539 
540    address = (Field *) malloc( sizeof(Field) );
541    if ( address == NULL )
542       ERROR( "allocField", "Out of memory");
543 
544    address->sum = NULL;
545    address->dif = NULL;
546    address->prod = NULL;
547    address->inv = NULL;
548    return address;
549 }
550 
551 
552 /*-------------------------- freeField ---------------------------*/
553 
freeField(Field * address)554 void freeField( Field *address)
555 {
556    free(address);
557 }
558