1 /*----------------------------------------------------------------------------------------
2   Database ut
3 ----------------------------------------------------------------------------------------*/
4 
5 #include "ddutil.h"
6 
7 union utTempType_ utTemp_;
8 struct utRootType_ utRootData;
9 uint8 utModuleID;
10 struct utSymtabFields utSymtabs;
11 struct utSymFields utSyms;
12 struct utDynarrayFields utDynarrays;
13 struct utSymArrayFields utSymArrays;
14 
15 /*----------------------------------------------------------------------------------------
16   Constructor/Destructor hooks.
17 ----------------------------------------------------------------------------------------*/
18 void(*utSymtabConstructorCallback)(utSymtab);
19 void(*utSymConstructorCallback)(utSym);
20 void(*utDynarrayConstructorCallback)(utDynarray);
21 void(*utDynarrayDestructorCallback)(utDynarray);
22 void(*utSymArrayConstructorCallback)(utSymArray);
23 void(*utSymArrayDestructorCallback)(utSymArray);
24 
25 /*----------------------------------------------------------------------------------------
26   Default constructor wrapper for the database manager.
27 ----------------------------------------------------------------------------------------*/
allocSymtab(void)28 static uint64 allocSymtab(void)
29 {
30     utSymtab Symtab = utSymtabAlloc();
31 
32     return utSymtab2Index(Symtab);
33 }
34 
35 /*----------------------------------------------------------------------------------------
36   Allocate the field arrays of Symtab.
37 ----------------------------------------------------------------------------------------*/
allocSymtabs(void)38 static void allocSymtabs(void)
39 {
40     utSetAllocatedSymtab(2);
41     utSetUsedSymtab(0);
42     utSymtabs.TableIndex = utNewA(uint32, (utAllocatedSymtab()));
43     utSymtabs.NumTable = utNewA(uint32, (utAllocatedSymtab()));
44     utSetUsedSymtabTable(0);
45     utSetAllocatedSymtabTable(2);
46     utSetFreeSymtabTable(0);
47     utSymtabs.Table = utNewA(utSym, utAllocatedSymtabTable());
48     utSymtabs.NumSym = utNewA(uint32, (utAllocatedSymtab()));
49 }
50 
51 /*----------------------------------------------------------------------------------------
52   Realloc the arrays of properties for class Symtab.
53 ----------------------------------------------------------------------------------------*/
reallocSymtabs(uint32 newSize)54 static void reallocSymtabs(
55     uint32 newSize)
56 {
57     utResizeArray(utSymtabs.TableIndex, (newSize));
58     utResizeArray(utSymtabs.NumTable, (newSize));
59     utResizeArray(utSymtabs.NumSym, (newSize));
60     utSetAllocatedSymtab(newSize);
61 }
62 
63 /*----------------------------------------------------------------------------------------
64   Allocate more Symtabs.
65 ----------------------------------------------------------------------------------------*/
utSymtabAllocMore(void)66 void utSymtabAllocMore(void)
67 {
68     reallocSymtabs((uint32)(utAllocatedSymtab() + (utAllocatedSymtab() >> 1)));
69 }
70 
71 /*----------------------------------------------------------------------------------------
72   Compact the Symtab.Table heap to free memory.
73 ----------------------------------------------------------------------------------------*/
utCompactSymtabTables(void)74 void utCompactSymtabTables(void)
75 {
76     uint32 elementSize = sizeof(utSym);
77     uint32 usedHeaderSize = (sizeof(utSymtab) + elementSize - 1)/elementSize;
78     uint32 freeHeaderSize = (sizeof(utSymtab) + sizeof(uint32) + elementSize - 1)/elementSize;
79     utSym *toPtr = utSymtabs.Table;
80     utSym *fromPtr = toPtr;
81     utSymtab Symtab;
82     uint32 size;
83 
84     while(fromPtr < utSymtabs.Table + utUsedSymtabTable()) {
85         Symtab = *(utSymtab *)(void *)fromPtr;
86         if(Symtab != utSymtabNull) {
87             /* Need to move it to toPtr */
88             size = utMax(utSymtabGetNumTable(Symtab) + usedHeaderSize, freeHeaderSize);
89             memmove((void *)toPtr, (void *)fromPtr, size*elementSize);
90             utSymtabSetTableIndex(Symtab, toPtr - utSymtabs.Table + usedHeaderSize);
91             toPtr += size;
92         } else {
93             /* Just skip it */
94             size = *(uint32 *)(void *)(((utSymtab *)(void *)fromPtr) + 1);
95         }
96         fromPtr += size;
97     }
98     utSetUsedSymtabTable(toPtr - utSymtabs.Table);
99     utSetFreeSymtabTable(0);
100 }
101 
102 /*----------------------------------------------------------------------------------------
103   Allocate more memory for the Symtab.Table heap.
104 ----------------------------------------------------------------------------------------*/
allocMoreSymtabTables(uint32 spaceNeeded)105 static void allocMoreSymtabTables(
106     uint32 spaceNeeded)
107 {
108     uint32 freeSpace = utAllocatedSymtabTable() - utUsedSymtabTable();
109 
110     if((utFreeSymtabTable() << 2) > utUsedSymtabTable()) {
111         utCompactSymtabTables();
112         freeSpace = utAllocatedSymtabTable() - utUsedSymtabTable();
113     }
114     if(freeSpace < spaceNeeded) {
115         utSetAllocatedSymtabTable(utAllocatedSymtabTable() + spaceNeeded - freeSpace +
116             (utAllocatedSymtabTable() >> 1));
117         utResizeArray(utSymtabs.Table, utAllocatedSymtabTable());
118     }
119 }
120 
121 /*----------------------------------------------------------------------------------------
122   Allocate memory for a new Symtab.Table array.
123 ----------------------------------------------------------------------------------------*/
utSymtabAllocTables(utSymtab Symtab,uint32 numTables)124 void utSymtabAllocTables(
125     utSymtab Symtab,
126     uint32 numTables)
127 {
128     uint32 freeSpace = utAllocatedSymtabTable() - utUsedSymtabTable();
129     uint32 elementSize = sizeof(utSym);
130     uint32 usedHeaderSize = (sizeof(utSymtab) + elementSize - 1)/elementSize;
131     uint32 freeHeaderSize = (sizeof(utSymtab) + sizeof(uint32) + elementSize - 1)/elementSize;
132     uint32 spaceNeeded = utMax(numTables + usedHeaderSize, freeHeaderSize);
133 
134 #if defined(DD_DEBUG)
135     utAssert(utSymtabGetNumTable(Symtab) == 0);
136 #endif
137     if(numTables == 0) {
138         return;
139     }
140     if(freeSpace < spaceNeeded) {
141         allocMoreSymtabTables(spaceNeeded);
142     }
143     utSymtabSetTableIndex(Symtab, utUsedSymtabTable() + usedHeaderSize);
144     utSymtabSetNumTable(Symtab, numTables);
145     *(utSymtab *)(void *)(utSymtabs.Table + utUsedSymtabTable()) = Symtab;
146     {
147         uint32 xSymtab;
148         for(xSymtab = (uint32)(utSymtabGetTableIndex(Symtab)); xSymtab < utSymtabGetTableIndex(Symtab) + numTables; xSymtab++) {
149             utSymtabs.Table[xSymtab] = utSymNull;
150         }
151     }
152     utSetUsedSymtabTable(utUsedSymtabTable() + spaceNeeded);
153 }
154 
155 /*----------------------------------------------------------------------------------------
156   Wrapper around utSymtabGetTables for the database manager.
157 ----------------------------------------------------------------------------------------*/
getSymtabTables(uint64 objectNumber,uint32 * numValues)158 static void *getSymtabTables(
159     uint64 objectNumber,
160     uint32 *numValues)
161 {
162     utSymtab Symtab = utIndex2Symtab((uint32)objectNumber);
163 
164     *numValues = utSymtabGetNumTable(Symtab);
165     return utSymtabGetTables(Symtab);
166 }
167 
168 /*----------------------------------------------------------------------------------------
169   Wrapper around utSymtabAllocTables for the database manager.
170 ----------------------------------------------------------------------------------------*/
allocSymtabTables(uint64 objectNumber,uint32 numValues)171 static void *allocSymtabTables(
172     uint64 objectNumber,
173     uint32 numValues)
174 {
175     utSymtab Symtab = utIndex2Symtab((uint32)objectNumber);
176 
177     utSymtabSetTableIndex(Symtab, 0);
178     utSymtabSetNumTable(Symtab, 0);
179     if(numValues == 0) {
180         return NULL;
181     }
182     utSymtabAllocTables(Symtab, numValues);
183     return utSymtabGetTables(Symtab);
184 }
185 
186 /*----------------------------------------------------------------------------------------
187   Free memory used by the Symtab.Table array.
188 ----------------------------------------------------------------------------------------*/
utSymtabFreeTables(utSymtab Symtab)189 void utSymtabFreeTables(
190     utSymtab Symtab)
191 {
192     uint32 elementSize = sizeof(utSym);
193     uint32 usedHeaderSize = (sizeof(utSymtab) + elementSize - 1)/elementSize;
194     uint32 freeHeaderSize = (sizeof(utSymtab) + sizeof(uint32) + elementSize - 1)/elementSize;
195     uint32 size = utMax(utSymtabGetNumTable(Symtab) + usedHeaderSize, freeHeaderSize);
196     utSym *dataPtr = utSymtabGetTables(Symtab) - usedHeaderSize;
197 
198     if(utSymtabGetNumTable(Symtab) == 0) {
199         return;
200     }
201     *(utSymtab *)(void *)(dataPtr) = utSymtabNull;
202     *(uint32 *)(void *)(((utSymtab *)(void *)dataPtr) + 1) = size;
203     utSymtabSetNumTable(Symtab, 0);
204     utSetFreeSymtabTable(utFreeSymtabTable() + size);
205 }
206 
207 /*----------------------------------------------------------------------------------------
208   Resize the Symtab.Table array.
209 ----------------------------------------------------------------------------------------*/
utSymtabResizeTables(utSymtab Symtab,uint32 numTables)210 void utSymtabResizeTables(
211     utSymtab Symtab,
212     uint32 numTables)
213 {
214     uint32 freeSpace;
215     uint32 elementSize = sizeof(utSym);
216     uint32 usedHeaderSize = (sizeof(utSymtab) + elementSize - 1)/elementSize;
217     uint32 freeHeaderSize = (sizeof(utSymtab) + sizeof(uint32) + elementSize - 1)/elementSize;
218     uint32 newSize = utMax(numTables + usedHeaderSize, freeHeaderSize);
219     uint32 oldSize = utMax(utSymtabGetNumTable(Symtab) + usedHeaderSize, freeHeaderSize);
220     utSym *dataPtr;
221 
222     if(numTables == 0) {
223         if(utSymtabGetNumTable(Symtab) != 0) {
224             utSymtabFreeTables(Symtab);
225         }
226         return;
227     }
228     if(utSymtabGetNumTable(Symtab) == 0) {
229         utSymtabAllocTables(Symtab, numTables);
230         return;
231     }
232     freeSpace = utAllocatedSymtabTable() - utUsedSymtabTable();
233     if(freeSpace < newSize) {
234         allocMoreSymtabTables(newSize);
235     }
236     dataPtr = utSymtabGetTables(Symtab) - usedHeaderSize;
237     memcpy((void *)(utSymtabs.Table + utUsedSymtabTable()), dataPtr,
238         elementSize*utMin(oldSize, newSize));
239     if(newSize > oldSize) {
240         {
241             uint32 xSymtab;
242             for(xSymtab = (uint32)(utUsedSymtabTable() + oldSize); xSymtab < utUsedSymtabTable() + oldSize + newSize - oldSize; xSymtab++) {
243                 utSymtabs.Table[xSymtab] = utSymNull;
244             }
245         }
246     }
247     *(utSymtab *)(void *)dataPtr = utSymtabNull;
248     *(uint32 *)(void *)(((utSymtab *)(void *)dataPtr) + 1) = oldSize;
249     utSetFreeSymtabTable(utFreeSymtabTable() + oldSize);
250     utSymtabSetTableIndex(Symtab, utUsedSymtabTable() + usedHeaderSize);
251     utSymtabSetNumTable(Symtab, numTables);
252     utSetUsedSymtabTable(utUsedSymtabTable() + newSize);
253 }
254 
255 /*----------------------------------------------------------------------------------------
256   Copy the properties of Symtab.
257 ----------------------------------------------------------------------------------------*/
utSymtabCopyProps(utSymtab oldSymtab,utSymtab newSymtab)258 void utSymtabCopyProps(
259     utSymtab oldSymtab,
260     utSymtab newSymtab)
261 {
262     utSymtabSetNumSym(newSymtab, utSymtabGetNumSym(oldSymtab));
263 }
264 
265 #if defined(DD_DEBUG)
266 /*----------------------------------------------------------------------------------------
267   Write out all the fields of an object.
268 ----------------------------------------------------------------------------------------*/
utShowSymtab(utSymtab Symtab)269 void utShowSymtab(
270     utSymtab Symtab)
271 {
272     utDatabaseShowObject("ut", "Symtab", utSymtab2Index(Symtab));
273 }
274 #endif
275 
276 /*----------------------------------------------------------------------------------------
277   Default constructor wrapper for the database manager.
278 ----------------------------------------------------------------------------------------*/
allocSym(void)279 static uint64 allocSym(void)
280 {
281     utSym Sym = utSymAlloc();
282 
283     return utSym2Index(Sym);
284 }
285 
286 /*----------------------------------------------------------------------------------------
287   Allocate the field arrays of Sym.
288 ----------------------------------------------------------------------------------------*/
allocSyms(void)289 static void allocSyms(void)
290 {
291     utSetAllocatedSym(2);
292     utSetUsedSym(0);
293     utSyms.NameIndex = utNewA(uint32, (utAllocatedSym()));
294     utSyms.NumName = utNewA(uint32, (utAllocatedSym()));
295     utSetUsedSymName(0);
296     utSetAllocatedSymName(2);
297     utSetFreeSymName(0);
298     utSyms.Name = utNewA(char, utAllocatedSymName());
299     utSyms.HashValue = utNewA(uint32, (utAllocatedSym()));
300     utSyms.Next = utNewA(utSym, (utAllocatedSym()));
301 }
302 
303 /*----------------------------------------------------------------------------------------
304   Realloc the arrays of properties for class Sym.
305 ----------------------------------------------------------------------------------------*/
reallocSyms(uint32 newSize)306 static void reallocSyms(
307     uint32 newSize)
308 {
309     utResizeArray(utSyms.NameIndex, (newSize));
310     utResizeArray(utSyms.NumName, (newSize));
311     utResizeArray(utSyms.HashValue, (newSize));
312     utResizeArray(utSyms.Next, (newSize));
313     utSetAllocatedSym(newSize);
314 }
315 
316 /*----------------------------------------------------------------------------------------
317   Allocate more Syms.
318 ----------------------------------------------------------------------------------------*/
utSymAllocMore(void)319 void utSymAllocMore(void)
320 {
321     reallocSyms((uint32)(utAllocatedSym() + (utAllocatedSym() >> 1)));
322 }
323 
324 /*----------------------------------------------------------------------------------------
325   Compact the Sym.Name heap to free memory.
326 ----------------------------------------------------------------------------------------*/
utCompactSymNames(void)327 void utCompactSymNames(void)
328 {
329     uint32 elementSize = sizeof(char);
330     uint32 usedHeaderSize = (sizeof(utSym) + elementSize - 1)/elementSize;
331     uint32 freeHeaderSize = (sizeof(utSym) + sizeof(uint32) + elementSize - 1)/elementSize;
332     char *toPtr = utSyms.Name;
333     char *fromPtr = toPtr;
334     utSym Sym;
335     uint32 size;
336 
337     while(fromPtr < utSyms.Name + utUsedSymName()) {
338         Sym = *(utSym *)(void *)fromPtr;
339         if(Sym != utSymNull) {
340             /* Need to move it to toPtr */
341             size = utMax(utSymGetNumName(Sym) + usedHeaderSize, freeHeaderSize);
342             memmove((void *)toPtr, (void *)fromPtr, size*elementSize);
343             utSymSetNameIndex(Sym, toPtr - utSyms.Name + usedHeaderSize);
344             toPtr += size;
345         } else {
346             /* Just skip it */
347             size = *(uint32 *)(void *)(((utSym *)(void *)fromPtr) + 1);
348         }
349         fromPtr += size;
350     }
351     utSetUsedSymName(toPtr - utSyms.Name);
352     utSetFreeSymName(0);
353 }
354 
355 /*----------------------------------------------------------------------------------------
356   Allocate more memory for the Sym.Name heap.
357 ----------------------------------------------------------------------------------------*/
allocMoreSymNames(uint32 spaceNeeded)358 static void allocMoreSymNames(
359     uint32 spaceNeeded)
360 {
361     uint32 freeSpace = utAllocatedSymName() - utUsedSymName();
362 
363     if((utFreeSymName() << 2) > utUsedSymName()) {
364         utCompactSymNames();
365         freeSpace = utAllocatedSymName() - utUsedSymName();
366     }
367     if(freeSpace < spaceNeeded) {
368         utSetAllocatedSymName(utAllocatedSymName() + spaceNeeded - freeSpace +
369             (utAllocatedSymName() >> 1));
370         utResizeArray(utSyms.Name, utAllocatedSymName());
371     }
372 }
373 
374 /*----------------------------------------------------------------------------------------
375   Allocate memory for a new Sym.Name array.
376 ----------------------------------------------------------------------------------------*/
utSymAllocNames(utSym Sym,uint32 numNames)377 void utSymAllocNames(
378     utSym Sym,
379     uint32 numNames)
380 {
381     uint32 freeSpace = utAllocatedSymName() - utUsedSymName();
382     uint32 elementSize = sizeof(char);
383     uint32 usedHeaderSize = (sizeof(utSym) + elementSize - 1)/elementSize;
384     uint32 freeHeaderSize = (sizeof(utSym) + sizeof(uint32) + elementSize - 1)/elementSize;
385     uint32 spaceNeeded = utMax(numNames + usedHeaderSize, freeHeaderSize);
386 
387 #if defined(DD_DEBUG)
388     utAssert(utSymGetNumName(Sym) == 0);
389 #endif
390     if(numNames == 0) {
391         return;
392     }
393     if(freeSpace < spaceNeeded) {
394         allocMoreSymNames(spaceNeeded);
395     }
396     utSymSetNameIndex(Sym, utUsedSymName() + usedHeaderSize);
397     utSymSetNumName(Sym, numNames);
398     *(utSym *)(void *)(utSyms.Name + utUsedSymName()) = Sym;
399     memset(utSyms.Name + utSymGetNameIndex(Sym), 0, ((numNames))*sizeof(char));
400     utSetUsedSymName(utUsedSymName() + spaceNeeded);
401 }
402 
403 /*----------------------------------------------------------------------------------------
404   Wrapper around utSymGetNames for the database manager.
405 ----------------------------------------------------------------------------------------*/
getSymNames(uint64 objectNumber,uint32 * numValues)406 static void *getSymNames(
407     uint64 objectNumber,
408     uint32 *numValues)
409 {
410     utSym Sym = utIndex2Sym((uint32)objectNumber);
411 
412     *numValues = utSymGetNumName(Sym);
413     return utSymGetNames(Sym);
414 }
415 
416 /*----------------------------------------------------------------------------------------
417   Wrapper around utSymAllocNames for the database manager.
418 ----------------------------------------------------------------------------------------*/
allocSymNames(uint64 objectNumber,uint32 numValues)419 static void *allocSymNames(
420     uint64 objectNumber,
421     uint32 numValues)
422 {
423     utSym Sym = utIndex2Sym((uint32)objectNumber);
424 
425     utSymSetNameIndex(Sym, 0);
426     utSymSetNumName(Sym, 0);
427     if(numValues == 0) {
428         return NULL;
429     }
430     utSymAllocNames(Sym, numValues);
431     return utSymGetNames(Sym);
432 }
433 
434 /*----------------------------------------------------------------------------------------
435   Free memory used by the Sym.Name array.
436 ----------------------------------------------------------------------------------------*/
utSymFreeNames(utSym Sym)437 void utSymFreeNames(
438     utSym Sym)
439 {
440     uint32 elementSize = sizeof(char);
441     uint32 usedHeaderSize = (sizeof(utSym) + elementSize - 1)/elementSize;
442     uint32 freeHeaderSize = (sizeof(utSym) + sizeof(uint32) + elementSize - 1)/elementSize;
443     uint32 size = utMax(utSymGetNumName(Sym) + usedHeaderSize, freeHeaderSize);
444     char *dataPtr = utSymGetNames(Sym) - usedHeaderSize;
445 
446     if(utSymGetNumName(Sym) == 0) {
447         return;
448     }
449     *(utSym *)(void *)(dataPtr) = utSymNull;
450     *(uint32 *)(void *)(((utSym *)(void *)dataPtr) + 1) = size;
451     utSymSetNumName(Sym, 0);
452     utSetFreeSymName(utFreeSymName() + size);
453 }
454 
455 /*----------------------------------------------------------------------------------------
456   Resize the Sym.Name array.
457 ----------------------------------------------------------------------------------------*/
utSymResizeNames(utSym Sym,uint32 numNames)458 void utSymResizeNames(
459     utSym Sym,
460     uint32 numNames)
461 {
462     uint32 freeSpace;
463     uint32 elementSize = sizeof(char);
464     uint32 usedHeaderSize = (sizeof(utSym) + elementSize - 1)/elementSize;
465     uint32 freeHeaderSize = (sizeof(utSym) + sizeof(uint32) + elementSize - 1)/elementSize;
466     uint32 newSize = utMax(numNames + usedHeaderSize, freeHeaderSize);
467     uint32 oldSize = utMax(utSymGetNumName(Sym) + usedHeaderSize, freeHeaderSize);
468     char *dataPtr;
469 
470     if(numNames == 0) {
471         if(utSymGetNumName(Sym) != 0) {
472             utSymFreeNames(Sym);
473         }
474         return;
475     }
476     if(utSymGetNumName(Sym) == 0) {
477         utSymAllocNames(Sym, numNames);
478         return;
479     }
480     freeSpace = utAllocatedSymName() - utUsedSymName();
481     if(freeSpace < newSize) {
482         allocMoreSymNames(newSize);
483     }
484     dataPtr = utSymGetNames(Sym) - usedHeaderSize;
485     memcpy((void *)(utSyms.Name + utUsedSymName()), dataPtr,
486         elementSize*utMin(oldSize, newSize));
487     if(newSize > oldSize) {
488         memset(utSyms.Name + utUsedSymName() + oldSize, 0, ((newSize - oldSize))*sizeof(char));
489     }
490     *(utSym *)(void *)dataPtr = utSymNull;
491     *(uint32 *)(void *)(((utSym *)(void *)dataPtr) + 1) = oldSize;
492     utSetFreeSymName(utFreeSymName() + oldSize);
493     utSymSetNameIndex(Sym, utUsedSymName() + usedHeaderSize);
494     utSymSetNumName(Sym, numNames);
495     utSetUsedSymName(utUsedSymName() + newSize);
496 }
497 
498 /*----------------------------------------------------------------------------------------
499   Copy the properties of Sym.
500 ----------------------------------------------------------------------------------------*/
utSymCopyProps(utSym oldSym,utSym newSym)501 void utSymCopyProps(
502     utSym oldSym,
503     utSym newSym)
504 {
505     utSymSetHashValue(newSym, utSymGetHashValue(oldSym));
506 }
507 
508 #if defined(DD_DEBUG)
509 /*----------------------------------------------------------------------------------------
510   Write out all the fields of an object.
511 ----------------------------------------------------------------------------------------*/
utShowSym(utSym Sym)512 void utShowSym(
513     utSym Sym)
514 {
515     utDatabaseShowObject("ut", "Sym", utSym2Index(Sym));
516 }
517 #endif
518 
519 /*----------------------------------------------------------------------------------------
520   Destroy Dynarray including everything in it. Remove from parents.
521 ----------------------------------------------------------------------------------------*/
utDynarrayDestroy(utDynarray Dynarray)522 void utDynarrayDestroy(
523     utDynarray Dynarray)
524 {
525     if(utDynarrayDestructorCallback != NULL) {
526         utDynarrayDestructorCallback(Dynarray);
527     }
528     utDynarrayFree(Dynarray);
529 }
530 
531 /*----------------------------------------------------------------------------------------
532   Default constructor wrapper for the database manager.
533 ----------------------------------------------------------------------------------------*/
allocDynarray(void)534 static uint64 allocDynarray(void)
535 {
536     utDynarray Dynarray = utDynarrayAlloc();
537 
538     return utDynarray2Index(Dynarray);
539 }
540 
541 /*----------------------------------------------------------------------------------------
542   Destructor wrapper for the database manager.
543 ----------------------------------------------------------------------------------------*/
destroyDynarray(uint64 objectIndex)544 static void destroyDynarray(
545     uint64 objectIndex)
546 {
547     utDynarrayDestroy(utIndex2Dynarray((uint32)objectIndex));
548 }
549 
550 /*----------------------------------------------------------------------------------------
551   Allocate the field arrays of Dynarray.
552 ----------------------------------------------------------------------------------------*/
allocDynarrays(void)553 static void allocDynarrays(void)
554 {
555     utSetAllocatedDynarray(2);
556     utSetUsedDynarray(0);
557     utSetFirstFreeDynarray(utDynarrayNull);
558     utDynarrays.ValueIndex = utNewA(uint32, (utAllocatedDynarray()));
559     utDynarrays.NumValue = utNewA(uint32, (utAllocatedDynarray()));
560     utSetUsedDynarrayValue(0);
561     utSetAllocatedDynarrayValue(2);
562     utSetFreeDynarrayValue(0);
563     utDynarrays.Value = utNewA(uint8, utAllocatedDynarrayValue());
564     utDynarrays.ValueSize = utNewA(uint16, (utAllocatedDynarray()));
565     utDynarrays.UsedValue = utNewA(uint32, (utAllocatedDynarray()));
566     utDynarrays.Size = utNewA(uint32, (utAllocatedDynarray()));
567     utDynarrays.FreeList = utNewA(utDynarray, (utAllocatedDynarray()));
568 }
569 
570 /*----------------------------------------------------------------------------------------
571   Realloc the arrays of properties for class Dynarray.
572 ----------------------------------------------------------------------------------------*/
reallocDynarrays(uint32 newSize)573 static void reallocDynarrays(
574     uint32 newSize)
575 {
576     utResizeArray(utDynarrays.ValueIndex, (newSize));
577     utResizeArray(utDynarrays.NumValue, (newSize));
578     utResizeArray(utDynarrays.ValueSize, (newSize));
579     utResizeArray(utDynarrays.UsedValue, (newSize));
580     utResizeArray(utDynarrays.Size, (newSize));
581     utResizeArray(utDynarrays.FreeList, (newSize));
582     utSetAllocatedDynarray(newSize);
583 }
584 
585 /*----------------------------------------------------------------------------------------
586   Allocate more Dynarrays.
587 ----------------------------------------------------------------------------------------*/
utDynarrayAllocMore(void)588 void utDynarrayAllocMore(void)
589 {
590     reallocDynarrays((uint32)(utAllocatedDynarray() + (utAllocatedDynarray() >> 1)));
591 }
592 
593 /*----------------------------------------------------------------------------------------
594   Compact the Dynarray.Value heap to free memory.
595 ----------------------------------------------------------------------------------------*/
utCompactDynarrayValues(void)596 void utCompactDynarrayValues(void)
597 {
598     uint32 elementSize = sizeof(uint8);
599     uint32 usedHeaderSize = (sizeof(utDynarray) + elementSize - 1)/elementSize;
600     uint32 freeHeaderSize = (sizeof(utDynarray) + sizeof(uint32) + elementSize - 1)/elementSize;
601     uint8 *toPtr = utDynarrays.Value;
602     uint8 *fromPtr = toPtr;
603     utDynarray Dynarray;
604     uint32 size;
605 
606     while(fromPtr < utDynarrays.Value + utUsedDynarrayValue()) {
607         Dynarray = *(utDynarray *)(void *)fromPtr;
608         if(Dynarray != utDynarrayNull) {
609             /* Need to move it to toPtr */
610             size = utMax(utDynarrayGetNumValue(Dynarray) + usedHeaderSize, freeHeaderSize);
611             memmove((void *)toPtr, (void *)fromPtr, size*elementSize);
612             utDynarraySetValueIndex(Dynarray, toPtr - utDynarrays.Value + usedHeaderSize);
613             toPtr += size;
614         } else {
615             /* Just skip it */
616             size = *(uint32 *)(void *)(((utDynarray *)(void *)fromPtr) + 1);
617         }
618         fromPtr += size;
619     }
620     utSetUsedDynarrayValue(toPtr - utDynarrays.Value);
621     utSetFreeDynarrayValue(0);
622 }
623 
624 /*----------------------------------------------------------------------------------------
625   Allocate more memory for the Dynarray.Value heap.
626 ----------------------------------------------------------------------------------------*/
allocMoreDynarrayValues(uint32 spaceNeeded)627 static void allocMoreDynarrayValues(
628     uint32 spaceNeeded)
629 {
630     uint32 freeSpace = utAllocatedDynarrayValue() - utUsedDynarrayValue();
631 
632     if((utFreeDynarrayValue() << 2) > utUsedDynarrayValue()) {
633         utCompactDynarrayValues();
634         freeSpace = utAllocatedDynarrayValue() - utUsedDynarrayValue();
635     }
636     if(freeSpace < spaceNeeded) {
637         utSetAllocatedDynarrayValue(utAllocatedDynarrayValue() + spaceNeeded - freeSpace +
638             (utAllocatedDynarrayValue() >> 1));
639         utResizeArray(utDynarrays.Value, utAllocatedDynarrayValue());
640     }
641 }
642 
643 /*----------------------------------------------------------------------------------------
644   Allocate memory for a new Dynarray.Value array.
645 ----------------------------------------------------------------------------------------*/
utDynarrayAllocValues(utDynarray Dynarray,uint32 numValues)646 void utDynarrayAllocValues(
647     utDynarray Dynarray,
648     uint32 numValues)
649 {
650     uint32 freeSpace = utAllocatedDynarrayValue() - utUsedDynarrayValue();
651     uint32 elementSize = sizeof(uint8);
652     uint32 usedHeaderSize = (sizeof(utDynarray) + elementSize - 1)/elementSize;
653     uint32 freeHeaderSize = (sizeof(utDynarray) + sizeof(uint32) + elementSize - 1)/elementSize;
654     uint32 spaceNeeded = utMax(numValues + usedHeaderSize, freeHeaderSize);
655 
656 #if defined(DD_DEBUG)
657     utAssert(utDynarrayGetNumValue(Dynarray) == 0);
658 #endif
659     if(numValues == 0) {
660         return;
661     }
662     if(freeSpace < spaceNeeded) {
663         allocMoreDynarrayValues(spaceNeeded);
664     }
665     utDynarraySetValueIndex(Dynarray, utUsedDynarrayValue() + usedHeaderSize);
666     utDynarraySetNumValue(Dynarray, numValues);
667     *(utDynarray *)(void *)(utDynarrays.Value + utUsedDynarrayValue()) = Dynarray;
668     memset(utDynarrays.Value + utDynarrayGetValueIndex(Dynarray), 0, ((numValues))*sizeof(uint8));
669     utSetUsedDynarrayValue(utUsedDynarrayValue() + spaceNeeded);
670 }
671 
672 /*----------------------------------------------------------------------------------------
673   Wrapper around utDynarrayGetValues for the database manager.
674 ----------------------------------------------------------------------------------------*/
getDynarrayValues(uint64 objectNumber,uint32 * numValues)675 static void *getDynarrayValues(
676     uint64 objectNumber,
677     uint32 *numValues)
678 {
679     utDynarray Dynarray = utIndex2Dynarray((uint32)objectNumber);
680 
681     *numValues = utDynarrayGetNumValue(Dynarray);
682     return utDynarrayGetValues(Dynarray);
683 }
684 
685 /*----------------------------------------------------------------------------------------
686   Wrapper around utDynarrayAllocValues for the database manager.
687 ----------------------------------------------------------------------------------------*/
allocDynarrayValues(uint64 objectNumber,uint32 numValues)688 static void *allocDynarrayValues(
689     uint64 objectNumber,
690     uint32 numValues)
691 {
692     utDynarray Dynarray = utIndex2Dynarray((uint32)objectNumber);
693 
694     utDynarraySetValueIndex(Dynarray, 0);
695     utDynarraySetNumValue(Dynarray, 0);
696     if(numValues == 0) {
697         return NULL;
698     }
699     utDynarrayAllocValues(Dynarray, numValues);
700     return utDynarrayGetValues(Dynarray);
701 }
702 
703 /*----------------------------------------------------------------------------------------
704   Free memory used by the Dynarray.Value array.
705 ----------------------------------------------------------------------------------------*/
utDynarrayFreeValues(utDynarray Dynarray)706 void utDynarrayFreeValues(
707     utDynarray Dynarray)
708 {
709     uint32 elementSize = sizeof(uint8);
710     uint32 usedHeaderSize = (sizeof(utDynarray) + elementSize - 1)/elementSize;
711     uint32 freeHeaderSize = (sizeof(utDynarray) + sizeof(uint32) + elementSize - 1)/elementSize;
712     uint32 size = utMax(utDynarrayGetNumValue(Dynarray) + usedHeaderSize, freeHeaderSize);
713     uint8 *dataPtr = utDynarrayGetValues(Dynarray) - usedHeaderSize;
714 
715     if(utDynarrayGetNumValue(Dynarray) == 0) {
716         return;
717     }
718     *(utDynarray *)(void *)(dataPtr) = utDynarrayNull;
719     *(uint32 *)(void *)(((utDynarray *)(void *)dataPtr) + 1) = size;
720     utDynarraySetNumValue(Dynarray, 0);
721     utSetFreeDynarrayValue(utFreeDynarrayValue() + size);
722 }
723 
724 /*----------------------------------------------------------------------------------------
725   Resize the Dynarray.Value array.
726 ----------------------------------------------------------------------------------------*/
utDynarrayResizeValues(utDynarray Dynarray,uint32 numValues)727 void utDynarrayResizeValues(
728     utDynarray Dynarray,
729     uint32 numValues)
730 {
731     uint32 freeSpace;
732     uint32 elementSize = sizeof(uint8);
733     uint32 usedHeaderSize = (sizeof(utDynarray) + elementSize - 1)/elementSize;
734     uint32 freeHeaderSize = (sizeof(utDynarray) + sizeof(uint32) + elementSize - 1)/elementSize;
735     uint32 newSize = utMax(numValues + usedHeaderSize, freeHeaderSize);
736     uint32 oldSize = utMax(utDynarrayGetNumValue(Dynarray) + usedHeaderSize, freeHeaderSize);
737     uint8 *dataPtr;
738 
739     if(numValues == 0) {
740         if(utDynarrayGetNumValue(Dynarray) != 0) {
741             utDynarrayFreeValues(Dynarray);
742         }
743         return;
744     }
745     if(utDynarrayGetNumValue(Dynarray) == 0) {
746         utDynarrayAllocValues(Dynarray, numValues);
747         return;
748     }
749     freeSpace = utAllocatedDynarrayValue() - utUsedDynarrayValue();
750     if(freeSpace < newSize) {
751         allocMoreDynarrayValues(newSize);
752     }
753     dataPtr = utDynarrayGetValues(Dynarray) - usedHeaderSize;
754     memcpy((void *)(utDynarrays.Value + utUsedDynarrayValue()), dataPtr,
755         elementSize*utMin(oldSize, newSize));
756     if(newSize > oldSize) {
757         memset(utDynarrays.Value + utUsedDynarrayValue() + oldSize, 0, ((newSize - oldSize))*sizeof(uint8));
758     }
759     *(utDynarray *)(void *)dataPtr = utDynarrayNull;
760     *(uint32 *)(void *)(((utDynarray *)(void *)dataPtr) + 1) = oldSize;
761     utSetFreeDynarrayValue(utFreeDynarrayValue() + oldSize);
762     utDynarraySetValueIndex(Dynarray, utUsedDynarrayValue() + usedHeaderSize);
763     utDynarraySetNumValue(Dynarray, numValues);
764     utSetUsedDynarrayValue(utUsedDynarrayValue() + newSize);
765 }
766 
767 /*----------------------------------------------------------------------------------------
768   Copy the properties of Dynarray.
769 ----------------------------------------------------------------------------------------*/
utDynarrayCopyProps(utDynarray oldDynarray,utDynarray newDynarray)770 void utDynarrayCopyProps(
771     utDynarray oldDynarray,
772     utDynarray newDynarray)
773 {
774     utDynarraySetValueSize(newDynarray, utDynarrayGetValueSize(oldDynarray));
775     utDynarraySetUsedValue(newDynarray, utDynarrayGetUsedValue(oldDynarray));
776     utDynarraySetSize(newDynarray, utDynarrayGetSize(oldDynarray));
777 }
778 
779 #if defined(DD_DEBUG)
780 /*----------------------------------------------------------------------------------------
781   Write out all the fields of an object.
782 ----------------------------------------------------------------------------------------*/
utShowDynarray(utDynarray Dynarray)783 void utShowDynarray(
784     utDynarray Dynarray)
785 {
786     utDatabaseShowObject("ut", "Dynarray", utDynarray2Index(Dynarray));
787 }
788 #endif
789 
790 /*----------------------------------------------------------------------------------------
791   Destroy SymArray including everything in it. Remove from parents.
792 ----------------------------------------------------------------------------------------*/
utSymArrayDestroy(utSymArray SymArray)793 void utSymArrayDestroy(
794     utSymArray SymArray)
795 {
796     if(utSymArrayDestructorCallback != NULL) {
797         utSymArrayDestructorCallback(SymArray);
798     }
799     utSymArrayFree(SymArray);
800 }
801 
802 /*----------------------------------------------------------------------------------------
803   Default constructor wrapper for the database manager.
804 ----------------------------------------------------------------------------------------*/
allocSymArray(void)805 static uint64 allocSymArray(void)
806 {
807     utSymArray SymArray = utSymArrayAlloc();
808 
809     return utSymArray2Index(SymArray);
810 }
811 
812 /*----------------------------------------------------------------------------------------
813   Destructor wrapper for the database manager.
814 ----------------------------------------------------------------------------------------*/
destroySymArray(uint64 objectIndex)815 static void destroySymArray(
816     uint64 objectIndex)
817 {
818     utSymArrayDestroy(utIndex2SymArray((uint32)objectIndex));
819 }
820 
821 /*----------------------------------------------------------------------------------------
822   Allocate the field arrays of SymArray.
823 ----------------------------------------------------------------------------------------*/
allocSymArrays(void)824 static void allocSymArrays(void)
825 {
826     utSetAllocatedSymArray(2);
827     utSetUsedSymArray(0);
828     utSetFirstFreeSymArray(utSymArrayNull);
829     utSymArrays.SymIndex = utNewA(uint32, (utAllocatedSymArray()));
830     utSymArrays.NumSym = utNewA(uint32, (utAllocatedSymArray()));
831     utSetUsedSymArraySym(0);
832     utSetAllocatedSymArraySym(2);
833     utSetFreeSymArraySym(0);
834     utSymArrays.Sym = utNewA(utSym, utAllocatedSymArraySym());
835     utSymArrays.UsedSym = utNewA(uint32, (utAllocatedSymArray()));
836     utSymArrays.FreeList = utNewA(utSymArray, (utAllocatedSymArray()));
837 }
838 
839 /*----------------------------------------------------------------------------------------
840   Realloc the arrays of properties for class SymArray.
841 ----------------------------------------------------------------------------------------*/
reallocSymArrays(uint32 newSize)842 static void reallocSymArrays(
843     uint32 newSize)
844 {
845     utResizeArray(utSymArrays.SymIndex, (newSize));
846     utResizeArray(utSymArrays.NumSym, (newSize));
847     utResizeArray(utSymArrays.UsedSym, (newSize));
848     utResizeArray(utSymArrays.FreeList, (newSize));
849     utSetAllocatedSymArray(newSize);
850 }
851 
852 /*----------------------------------------------------------------------------------------
853   Allocate more SymArrays.
854 ----------------------------------------------------------------------------------------*/
utSymArrayAllocMore(void)855 void utSymArrayAllocMore(void)
856 {
857     reallocSymArrays((uint32)(utAllocatedSymArray() + (utAllocatedSymArray() >> 1)));
858 }
859 
860 /*----------------------------------------------------------------------------------------
861   Compact the SymArray.Sym heap to free memory.
862 ----------------------------------------------------------------------------------------*/
utCompactSymArraySyms(void)863 void utCompactSymArraySyms(void)
864 {
865     uint32 elementSize = sizeof(utSym);
866     uint32 usedHeaderSize = (sizeof(utSymArray) + elementSize - 1)/elementSize;
867     uint32 freeHeaderSize = (sizeof(utSymArray) + sizeof(uint32) + elementSize - 1)/elementSize;
868     utSym *toPtr = utSymArrays.Sym;
869     utSym *fromPtr = toPtr;
870     utSymArray SymArray;
871     uint32 size;
872 
873     while(fromPtr < utSymArrays.Sym + utUsedSymArraySym()) {
874         SymArray = *(utSymArray *)(void *)fromPtr;
875         if(SymArray != utSymArrayNull) {
876             /* Need to move it to toPtr */
877             size = utMax(utSymArrayGetNumSym(SymArray) + usedHeaderSize, freeHeaderSize);
878             memmove((void *)toPtr, (void *)fromPtr, size*elementSize);
879             utSymArraySetSymIndex(SymArray, toPtr - utSymArrays.Sym + usedHeaderSize);
880             toPtr += size;
881         } else {
882             /* Just skip it */
883             size = *(uint32 *)(void *)(((utSymArray *)(void *)fromPtr) + 1);
884         }
885         fromPtr += size;
886     }
887     utSetUsedSymArraySym(toPtr - utSymArrays.Sym);
888     utSetFreeSymArraySym(0);
889 }
890 
891 /*----------------------------------------------------------------------------------------
892   Allocate more memory for the SymArray.Sym heap.
893 ----------------------------------------------------------------------------------------*/
allocMoreSymArraySyms(uint32 spaceNeeded)894 static void allocMoreSymArraySyms(
895     uint32 spaceNeeded)
896 {
897     uint32 freeSpace = utAllocatedSymArraySym() - utUsedSymArraySym();
898 
899     if((utFreeSymArraySym() << 2) > utUsedSymArraySym()) {
900         utCompactSymArraySyms();
901         freeSpace = utAllocatedSymArraySym() - utUsedSymArraySym();
902     }
903     if(freeSpace < spaceNeeded) {
904         utSetAllocatedSymArraySym(utAllocatedSymArraySym() + spaceNeeded - freeSpace +
905             (utAllocatedSymArraySym() >> 1));
906         utResizeArray(utSymArrays.Sym, utAllocatedSymArraySym());
907     }
908 }
909 
910 /*----------------------------------------------------------------------------------------
911   Allocate memory for a new SymArray.Sym array.
912 ----------------------------------------------------------------------------------------*/
utSymArrayAllocSyms(utSymArray SymArray,uint32 numSyms)913 void utSymArrayAllocSyms(
914     utSymArray SymArray,
915     uint32 numSyms)
916 {
917     uint32 freeSpace = utAllocatedSymArraySym() - utUsedSymArraySym();
918     uint32 elementSize = sizeof(utSym);
919     uint32 usedHeaderSize = (sizeof(utSymArray) + elementSize - 1)/elementSize;
920     uint32 freeHeaderSize = (sizeof(utSymArray) + sizeof(uint32) + elementSize - 1)/elementSize;
921     uint32 spaceNeeded = utMax(numSyms + usedHeaderSize, freeHeaderSize);
922 
923 #if defined(DD_DEBUG)
924     utAssert(utSymArrayGetNumSym(SymArray) == 0);
925 #endif
926     if(numSyms == 0) {
927         return;
928     }
929     if(freeSpace < spaceNeeded) {
930         allocMoreSymArraySyms(spaceNeeded);
931     }
932     utSymArraySetSymIndex(SymArray, utUsedSymArraySym() + usedHeaderSize);
933     utSymArraySetNumSym(SymArray, numSyms);
934     *(utSymArray *)(void *)(utSymArrays.Sym + utUsedSymArraySym()) = SymArray;
935     {
936         uint32 xSymArray;
937         for(xSymArray = (uint32)(utSymArrayGetSymIndex(SymArray)); xSymArray < utSymArrayGetSymIndex(SymArray) + numSyms; xSymArray++) {
938             utSymArrays.Sym[xSymArray] = utSymNull;
939         }
940     }
941     utSetUsedSymArraySym(utUsedSymArraySym() + spaceNeeded);
942 }
943 
944 /*----------------------------------------------------------------------------------------
945   Wrapper around utSymArrayGetSyms for the database manager.
946 ----------------------------------------------------------------------------------------*/
getSymArraySyms(uint64 objectNumber,uint32 * numValues)947 static void *getSymArraySyms(
948     uint64 objectNumber,
949     uint32 *numValues)
950 {
951     utSymArray SymArray = utIndex2SymArray((uint32)objectNumber);
952 
953     *numValues = utSymArrayGetNumSym(SymArray);
954     return utSymArrayGetSyms(SymArray);
955 }
956 
957 /*----------------------------------------------------------------------------------------
958   Wrapper around utSymArrayAllocSyms for the database manager.
959 ----------------------------------------------------------------------------------------*/
allocSymArraySyms(uint64 objectNumber,uint32 numValues)960 static void *allocSymArraySyms(
961     uint64 objectNumber,
962     uint32 numValues)
963 {
964     utSymArray SymArray = utIndex2SymArray((uint32)objectNumber);
965 
966     utSymArraySetSymIndex(SymArray, 0);
967     utSymArraySetNumSym(SymArray, 0);
968     if(numValues == 0) {
969         return NULL;
970     }
971     utSymArrayAllocSyms(SymArray, numValues);
972     return utSymArrayGetSyms(SymArray);
973 }
974 
975 /*----------------------------------------------------------------------------------------
976   Free memory used by the SymArray.Sym array.
977 ----------------------------------------------------------------------------------------*/
utSymArrayFreeSyms(utSymArray SymArray)978 void utSymArrayFreeSyms(
979     utSymArray SymArray)
980 {
981     uint32 elementSize = sizeof(utSym);
982     uint32 usedHeaderSize = (sizeof(utSymArray) + elementSize - 1)/elementSize;
983     uint32 freeHeaderSize = (sizeof(utSymArray) + sizeof(uint32) + elementSize - 1)/elementSize;
984     uint32 size = utMax(utSymArrayGetNumSym(SymArray) + usedHeaderSize, freeHeaderSize);
985     utSym *dataPtr = utSymArrayGetSyms(SymArray) - usedHeaderSize;
986 
987     if(utSymArrayGetNumSym(SymArray) == 0) {
988         return;
989     }
990     *(utSymArray *)(void *)(dataPtr) = utSymArrayNull;
991     *(uint32 *)(void *)(((utSymArray *)(void *)dataPtr) + 1) = size;
992     utSymArraySetNumSym(SymArray, 0);
993     utSetFreeSymArraySym(utFreeSymArraySym() + size);
994 }
995 
996 /*----------------------------------------------------------------------------------------
997   Resize the SymArray.Sym array.
998 ----------------------------------------------------------------------------------------*/
utSymArrayResizeSyms(utSymArray SymArray,uint32 numSyms)999 void utSymArrayResizeSyms(
1000     utSymArray SymArray,
1001     uint32 numSyms)
1002 {
1003     uint32 freeSpace;
1004     uint32 elementSize = sizeof(utSym);
1005     uint32 usedHeaderSize = (sizeof(utSymArray) + elementSize - 1)/elementSize;
1006     uint32 freeHeaderSize = (sizeof(utSymArray) + sizeof(uint32) + elementSize - 1)/elementSize;
1007     uint32 newSize = utMax(numSyms + usedHeaderSize, freeHeaderSize);
1008     uint32 oldSize = utMax(utSymArrayGetNumSym(SymArray) + usedHeaderSize, freeHeaderSize);
1009     utSym *dataPtr;
1010 
1011     if(numSyms == 0) {
1012         if(utSymArrayGetNumSym(SymArray) != 0) {
1013             utSymArrayFreeSyms(SymArray);
1014         }
1015         return;
1016     }
1017     if(utSymArrayGetNumSym(SymArray) == 0) {
1018         utSymArrayAllocSyms(SymArray, numSyms);
1019         return;
1020     }
1021     freeSpace = utAllocatedSymArraySym() - utUsedSymArraySym();
1022     if(freeSpace < newSize) {
1023         allocMoreSymArraySyms(newSize);
1024     }
1025     dataPtr = utSymArrayGetSyms(SymArray) - usedHeaderSize;
1026     memcpy((void *)(utSymArrays.Sym + utUsedSymArraySym()), dataPtr,
1027         elementSize*utMin(oldSize, newSize));
1028     if(newSize > oldSize) {
1029         {
1030             uint32 xSymArray;
1031             for(xSymArray = (uint32)(utUsedSymArraySym() + oldSize); xSymArray < utUsedSymArraySym() + oldSize + newSize - oldSize; xSymArray++) {
1032                 utSymArrays.Sym[xSymArray] = utSymNull;
1033             }
1034         }
1035     }
1036     *(utSymArray *)(void *)dataPtr = utSymArrayNull;
1037     *(uint32 *)(void *)(((utSymArray *)(void *)dataPtr) + 1) = oldSize;
1038     utSetFreeSymArraySym(utFreeSymArraySym() + oldSize);
1039     utSymArraySetSymIndex(SymArray, utUsedSymArraySym() + usedHeaderSize);
1040     utSymArraySetNumSym(SymArray, numSyms);
1041     utSetUsedSymArraySym(utUsedSymArraySym() + newSize);
1042 }
1043 
1044 /*----------------------------------------------------------------------------------------
1045   Copy the properties of SymArray.
1046 ----------------------------------------------------------------------------------------*/
utSymArrayCopyProps(utSymArray oldSymArray,utSymArray newSymArray)1047 void utSymArrayCopyProps(
1048     utSymArray oldSymArray,
1049     utSymArray newSymArray)
1050 {
1051 }
1052 
1053 /*----------------------------------------------------------------------------------------
1054   Add the indexed Sym to the SymArray.
1055 ----------------------------------------------------------------------------------------*/
utSymArrayInsertSym(utSymArray SymArray,uint32 x,utSym _Sym)1056 void utSymArrayInsertSym(
1057     utSymArray SymArray,
1058     uint32 x,
1059     utSym _Sym)
1060 {
1061 #if defined(DD_DEBUG)
1062     if(SymArray == utSymArrayNull) {
1063         utExit("Non existent SymArray");
1064     }
1065 #endif
1066     utSymArraySetiSym(SymArray, x, _Sym);
1067     utSymArraySetUsedSym(SymArray, utMax(utSymArrayGetUsedSym(SymArray), x + 1));
1068 }
1069 
1070 /*----------------------------------------------------------------------------------------
1071   Add the Sym to the end of the SymArraySym array.
1072 ----------------------------------------------------------------------------------------*/
utSymArrayAppendSym(utSymArray SymArray,utSym _Sym)1073 void utSymArrayAppendSym(
1074     utSymArray SymArray,
1075     utSym _Sym)
1076 {
1077     uint32 usedSym = utSymArrayGetUsedSym(SymArray);
1078 
1079 #if defined(DD_DEBUG)
1080     if(SymArray == utSymArrayNull) {
1081         utExit("Non existent SymArray");
1082     }
1083 #endif
1084     if(usedSym >= utSymArrayGetNumSym(SymArray)) {
1085         utSymArrayResizeSyms(SymArray, usedSym + (usedSym << 1) + 1);
1086     }
1087     utSymArraySetiSym(SymArray, usedSym, _Sym);
1088     utSymArraySetUsedSym(SymArray, usedSym + 1);
1089 }
1090 
1091 #if defined(DD_DEBUG)
1092 /*----------------------------------------------------------------------------------------
1093   Write out all the fields of an object.
1094 ----------------------------------------------------------------------------------------*/
utShowSymArray(utSymArray SymArray)1095 void utShowSymArray(
1096     utSymArray SymArray)
1097 {
1098     utDatabaseShowObject("ut", "SymArray", utSymArray2Index(SymArray));
1099 }
1100 #endif
1101 
1102 /*----------------------------------------------------------------------------------------
1103   Free memory used by the ut database.
1104 ----------------------------------------------------------------------------------------*/
utDatabaseStop(void)1105 void utDatabaseStop(void)
1106 {
1107     utFree(utSymtabs.TableIndex);
1108     utFree(utSymtabs.NumTable);
1109     utFree(utSymtabs.Table);
1110     utFree(utSymtabs.NumSym);
1111     utFree(utSyms.NameIndex);
1112     utFree(utSyms.NumName);
1113     utFree(utSyms.Name);
1114     utFree(utSyms.HashValue);
1115     utFree(utSyms.Next);
1116     utFree(utDynarrays.ValueIndex);
1117     utFree(utDynarrays.NumValue);
1118     utFree(utDynarrays.Value);
1119     utFree(utDynarrays.ValueSize);
1120     utFree(utDynarrays.UsedValue);
1121     utFree(utDynarrays.Size);
1122     utFree(utDynarrays.FreeList);
1123     utFree(utSymArrays.SymIndex);
1124     utFree(utSymArrays.NumSym);
1125     utFree(utSymArrays.Sym);
1126     utFree(utSymArrays.UsedSym);
1127     utFree(utSymArrays.FreeList);
1128     utUnregisterModule(utModuleID);
1129 }
1130 
1131 /*----------------------------------------------------------------------------------------
1132   Allocate memory used by the ut database.
1133 ----------------------------------------------------------------------------------------*/
utDatabaseStart(void)1134 void utDatabaseStart(void)
1135 {
1136     if(!utInitialized()) {
1137         utStart();
1138     }
1139     utRootData.hash = 0x36c29ca6;
1140     utModuleID = utRegisterModule("ut", false, utHash(), 4, 21, 1, sizeof(struct utRootType_),
1141         &utRootData, utDatabaseStart, utDatabaseStop);
1142     utRegisterEnum("FieldType", 12);
1143     utRegisterEntry("UT_BIT", 0);
1144     utRegisterEntry("UT_BOOL", 1);
1145     utRegisterEntry("UT_INT", 2);
1146     utRegisterEntry("UT_UINT", 3);
1147     utRegisterEntry("UT_CHAR", 4);
1148     utRegisterEntry("UT_FLOAT", 5);
1149     utRegisterEntry("UT_DOUBLE", 6);
1150     utRegisterEntry("UT_POINTER", 7);
1151     utRegisterEntry("UT_TYPEDEF", 8);
1152     utRegisterEntry("UT_ENUM", 9);
1153     utRegisterEntry("UT_SYM", 10);
1154     utRegisterEntry("UT_UNION", 11);
1155     utRegisterClass("Symtab", 4, &utRootData.usedSymtab, &utRootData.allocatedSymtab,
1156         NULL, 65535, 4, allocSymtab, NULL);
1157     utRegisterField("TableIndex", &utSymtabs.TableIndex, sizeof(uint32), UT_UINT, NULL);
1158     utSetFieldHidden();
1159     utRegisterField("NumTable", &utSymtabs.NumTable, sizeof(uint32), UT_UINT, NULL);
1160     utSetFieldHidden();
1161     utRegisterField("Table", &utSymtabs.Table, sizeof(utSym), UT_SYM, NULL);
1162     utRegisterArray(&utRootData.usedSymtabTable, &utRootData.allocatedSymtabTable,
1163         getSymtabTables, allocSymtabTables, utCompactSymtabTables);
1164     utRegisterField("NumSym", &utSymtabs.NumSym, sizeof(uint32), UT_UINT, NULL);
1165     utRegisterClass("Sym", 5, &utRootData.usedSym, &utRootData.allocatedSym,
1166         NULL, 65535, 4, allocSym, NULL);
1167     utRegisterField("NameIndex", &utSyms.NameIndex, sizeof(uint32), UT_UINT, NULL);
1168     utSetFieldHidden();
1169     utRegisterField("NumName", &utSyms.NumName, sizeof(uint32), UT_UINT, NULL);
1170     utSetFieldHidden();
1171     utRegisterField("Name", &utSyms.Name, sizeof(char), UT_CHAR, NULL);
1172     utRegisterArray(&utRootData.usedSymName, &utRootData.allocatedSymName,
1173         getSymNames, allocSymNames, utCompactSymNames);
1174     utRegisterField("HashValue", &utSyms.HashValue, sizeof(uint32), UT_UINT, NULL);
1175     utRegisterField("Next", &utSyms.Next, sizeof(utSym), UT_POINTER, "Sym");
1176     utRegisterClass("Dynarray", 7, &utRootData.usedDynarray, &utRootData.allocatedDynarray,
1177         &utRootData.firstFreeDynarray, 15, 4, allocDynarray, destroyDynarray);
1178     utRegisterField("ValueIndex", &utDynarrays.ValueIndex, sizeof(uint32), UT_UINT, NULL);
1179     utSetFieldHidden();
1180     utRegisterField("NumValue", &utDynarrays.NumValue, sizeof(uint32), UT_UINT, NULL);
1181     utSetFieldHidden();
1182     utRegisterField("Value", &utDynarrays.Value, sizeof(uint8), UT_UINT, NULL);
1183     utRegisterArray(&utRootData.usedDynarrayValue, &utRootData.allocatedDynarrayValue,
1184         getDynarrayValues, allocDynarrayValues, utCompactDynarrayValues);
1185     utRegisterField("ValueSize", &utDynarrays.ValueSize, sizeof(uint16), UT_UINT, NULL);
1186     utRegisterField("UsedValue", &utDynarrays.UsedValue, sizeof(uint32), UT_UINT, NULL);
1187     utRegisterField("Size", &utDynarrays.Size, sizeof(uint32), UT_UINT, NULL);
1188     utRegisterField("FreeList", &utDynarrays.FreeList, sizeof(utDynarray), UT_POINTER, "Dynarray");
1189     utSetFieldHidden();
1190     utRegisterClass("SymArray", 5, &utRootData.usedSymArray, &utRootData.allocatedSymArray,
1191         &utRootData.firstFreeSymArray, 20, 4, allocSymArray, destroySymArray);
1192     utRegisterField("SymIndex", &utSymArrays.SymIndex, sizeof(uint32), UT_UINT, NULL);
1193     utSetFieldHidden();
1194     utRegisterField("NumSym", &utSymArrays.NumSym, sizeof(uint32), UT_UINT, NULL);
1195     utSetFieldHidden();
1196     utRegisterField("Sym", &utSymArrays.Sym, sizeof(utSym), UT_POINTER, "Sym");
1197     utRegisterArray(&utRootData.usedSymArraySym, &utRootData.allocatedSymArraySym,
1198         getSymArraySyms, allocSymArraySyms, utCompactSymArraySyms);
1199     utRegisterField("UsedSym", &utSymArrays.UsedSym, sizeof(uint32), UT_UINT, NULL);
1200     utRegisterField("FreeList", &utSymArrays.FreeList, sizeof(utSymArray), UT_POINTER, "SymArray");
1201     utSetFieldHidden();
1202     allocSymtabs();
1203     allocSyms();
1204     allocDynarrays();
1205     allocSymArrays();
1206 }
1207 
1208 #if defined(DD_DEBUG)
1209 #undef utSymtabGetTableIndex
utSymtabGetTableIndex(utSymtab _Symtab)1210 uint32 utSymtabGetTableIndex(
1211     utSymtab _Symtab)
1212 {
1213     return utSymtabs.TableIndex[utSymtab2Index(_Symtab)];
1214 }
1215 
1216 #undef utSymtabSetTableIndex
utSymtabSetTableIndex(utSymtab _Symtab,uint32 value)1217 void utSymtabSetTableIndex(
1218     utSymtab _Symtab,
1219     uint32 value)
1220 {
1221     utSymtabs.TableIndex[utSymtab2Index(_Symtab)] = value;
1222 }
1223 
1224 #undef utSymtabGetNumTable
utSymtabGetNumTable(utSymtab _Symtab)1225 uint32 utSymtabGetNumTable(
1226     utSymtab _Symtab)
1227 {
1228     return utSymtabs.NumTable[utSymtab2Index(_Symtab)];
1229 }
1230 
1231 #undef utSymtabSetNumTable
utSymtabSetNumTable(utSymtab _Symtab,uint32 value)1232 void utSymtabSetNumTable(
1233     utSymtab _Symtab,
1234     uint32 value)
1235 {
1236     utSymtabs.NumTable[utSymtab2Index(_Symtab)] = value;
1237 }
1238 
1239 #undef utSymtabGetiTable
utSymtabGetiTable(utSymtab _Symtab,uint32 x)1240 utSym utSymtabGetiTable(
1241     utSymtab _Symtab,
1242     uint32 x)
1243 {
1244     return (utSymtabs.Table)[utSymtabGetTableIndex(_Symtab) + x];
1245 }
1246 
1247 #undef utSymtabSetiTable
utSymtabSetiTable(utSymtab Symtab,uint32 x,utSym value)1248 void utSymtabSetiTable(
1249     utSymtab Symtab,
1250     uint32 x,
1251     utSym value)
1252 {
1253     utSymtabs.Table[utSymtabGetTableIndex(Symtab) + x] = value;
1254 }
1255 
1256 #undef utSymtabGetTable
utSymtabGetTable(utSymtab Symtab)1257 utSym *utSymtabGetTable(
1258     utSymtab Symtab)
1259 {
1260     return utSymtabs.Table + utSymtabGetTableIndex(Symtab);
1261 }
1262 
1263 #undef utSymtabGetNumSym
utSymtabGetNumSym(utSymtab _Symtab)1264 uint32 utSymtabGetNumSym(
1265     utSymtab _Symtab)
1266 {
1267     return utSymtabs.NumSym[utSymtab2Index(_Symtab)];
1268 }
1269 
1270 #undef utSymtabSetNumSym
utSymtabSetNumSym(utSymtab _Symtab,uint32 value)1271 void utSymtabSetNumSym(
1272     utSymtab _Symtab,
1273     uint32 value)
1274 {
1275     utSymtabs.NumSym[utSymtab2Index(_Symtab)] = value;
1276 }
1277 
1278 #undef utSymGetNameIndex
utSymGetNameIndex(utSym _Sym)1279 uint32 utSymGetNameIndex(
1280     utSym _Sym)
1281 {
1282     return utSyms.NameIndex[utSym2Index(_Sym)];
1283 }
1284 
1285 #undef utSymSetNameIndex
utSymSetNameIndex(utSym _Sym,uint32 value)1286 void utSymSetNameIndex(
1287     utSym _Sym,
1288     uint32 value)
1289 {
1290     utSyms.NameIndex[utSym2Index(_Sym)] = value;
1291 }
1292 
1293 #undef utSymGetNumName
utSymGetNumName(utSym _Sym)1294 uint32 utSymGetNumName(
1295     utSym _Sym)
1296 {
1297     return utSyms.NumName[utSym2Index(_Sym)];
1298 }
1299 
1300 #undef utSymSetNumName
utSymSetNumName(utSym _Sym,uint32 value)1301 void utSymSetNumName(
1302     utSym _Sym,
1303     uint32 value)
1304 {
1305     utSyms.NumName[utSym2Index(_Sym)] = value;
1306 }
1307 
1308 #undef utSymGetiName
utSymGetiName(utSym _Sym,uint32 x)1309 char utSymGetiName(
1310     utSym _Sym,
1311     uint32 x)
1312 {
1313     return (utSyms.Name)[utSymGetNameIndex(_Sym) + x];
1314 }
1315 
1316 #undef utSymSetiName
utSymSetiName(utSym Sym,uint32 x,char value)1317 void utSymSetiName(
1318     utSym Sym,
1319     uint32 x,
1320     char value)
1321 {
1322     utSyms.Name[utSymGetNameIndex(Sym) + x] = value;
1323 }
1324 
1325 #undef utSymGetName
utSymGetName(utSym Sym)1326 char *utSymGetName(
1327     utSym Sym)
1328 {
1329     return utSyms.Name + utSymGetNameIndex(Sym);
1330 }
1331 
1332 #undef utSymGetHashValue
utSymGetHashValue(utSym _Sym)1333 uint32 utSymGetHashValue(
1334     utSym _Sym)
1335 {
1336     return utSyms.HashValue[utSym2Index(_Sym)];
1337 }
1338 
1339 #undef utSymSetHashValue
utSymSetHashValue(utSym _Sym,uint32 value)1340 void utSymSetHashValue(
1341     utSym _Sym,
1342     uint32 value)
1343 {
1344     utSyms.HashValue[utSym2Index(_Sym)] = value;
1345 }
1346 
1347 #undef utSymGetNext
utSymGetNext(utSym _Sym)1348 utSym utSymGetNext(
1349     utSym _Sym)
1350 {
1351     return utSyms.Next[utSym2Index(_Sym)];
1352 }
1353 
1354 #undef utSymSetNext
utSymSetNext(utSym _Sym,utSym value)1355 void utSymSetNext(
1356     utSym _Sym,
1357     utSym value)
1358 {
1359     utSyms.Next[utSym2Index(_Sym)] = value;
1360 }
1361 
1362 #undef utDynarrayGetValueIndex
utDynarrayGetValueIndex(utDynarray _Dynarray)1363 uint32 utDynarrayGetValueIndex(
1364     utDynarray _Dynarray)
1365 {
1366     return utDynarrays.ValueIndex[utDynarray2Index(_Dynarray)];
1367 }
1368 
1369 #undef utDynarraySetValueIndex
utDynarraySetValueIndex(utDynarray _Dynarray,uint32 value)1370 void utDynarraySetValueIndex(
1371     utDynarray _Dynarray,
1372     uint32 value)
1373 {
1374     utDynarrays.ValueIndex[utDynarray2Index(_Dynarray)] = value;
1375 }
1376 
1377 #undef utDynarrayGetNumValue
utDynarrayGetNumValue(utDynarray _Dynarray)1378 uint32 utDynarrayGetNumValue(
1379     utDynarray _Dynarray)
1380 {
1381     return utDynarrays.NumValue[utDynarray2Index(_Dynarray)];
1382 }
1383 
1384 #undef utDynarraySetNumValue
utDynarraySetNumValue(utDynarray _Dynarray,uint32 value)1385 void utDynarraySetNumValue(
1386     utDynarray _Dynarray,
1387     uint32 value)
1388 {
1389     utDynarrays.NumValue[utDynarray2Index(_Dynarray)] = value;
1390 }
1391 
1392 #undef utDynarrayGetiValue
utDynarrayGetiValue(utDynarray _Dynarray,uint32 x)1393 uint8 utDynarrayGetiValue(
1394     utDynarray _Dynarray,
1395     uint32 x)
1396 {
1397     return (utDynarrays.Value)[utDynarrayGetValueIndex(_Dynarray) + x];
1398 }
1399 
1400 #undef utDynarraySetiValue
utDynarraySetiValue(utDynarray Dynarray,uint32 x,uint8 value)1401 void utDynarraySetiValue(
1402     utDynarray Dynarray,
1403     uint32 x,
1404     uint8 value)
1405 {
1406     utDynarrays.Value[utDynarrayGetValueIndex(Dynarray) + x] = value;
1407 }
1408 
1409 #undef utDynarrayGetValue
utDynarrayGetValue(utDynarray Dynarray)1410 uint8 *utDynarrayGetValue(
1411     utDynarray Dynarray)
1412 {
1413     return utDynarrays.Value + utDynarrayGetValueIndex(Dynarray);
1414 }
1415 
1416 #undef utDynarrayGetValueSize
utDynarrayGetValueSize(utDynarray _Dynarray)1417 uint16 utDynarrayGetValueSize(
1418     utDynarray _Dynarray)
1419 {
1420     return utDynarrays.ValueSize[utDynarray2Index(_Dynarray)];
1421 }
1422 
1423 #undef utDynarraySetValueSize
utDynarraySetValueSize(utDynarray _Dynarray,uint16 value)1424 void utDynarraySetValueSize(
1425     utDynarray _Dynarray,
1426     uint16 value)
1427 {
1428     utDynarrays.ValueSize[utDynarray2Index(_Dynarray)] = value;
1429 }
1430 
1431 #undef utDynarrayGetUsedValue
utDynarrayGetUsedValue(utDynarray _Dynarray)1432 uint32 utDynarrayGetUsedValue(
1433     utDynarray _Dynarray)
1434 {
1435     return utDynarrays.UsedValue[utDynarray2Index(_Dynarray)];
1436 }
1437 
1438 #undef utDynarraySetUsedValue
utDynarraySetUsedValue(utDynarray _Dynarray,uint32 value)1439 void utDynarraySetUsedValue(
1440     utDynarray _Dynarray,
1441     uint32 value)
1442 {
1443     utDynarrays.UsedValue[utDynarray2Index(_Dynarray)] = value;
1444 }
1445 
1446 #undef utDynarrayGetSize
utDynarrayGetSize(utDynarray _Dynarray)1447 uint32 utDynarrayGetSize(
1448     utDynarray _Dynarray)
1449 {
1450     return utDynarrays.Size[utDynarray2Index(_Dynarray)];
1451 }
1452 
1453 #undef utDynarraySetSize
utDynarraySetSize(utDynarray _Dynarray,uint32 value)1454 void utDynarraySetSize(
1455     utDynarray _Dynarray,
1456     uint32 value)
1457 {
1458     utDynarrays.Size[utDynarray2Index(_Dynarray)] = value;
1459 }
1460 
1461 #undef utDynarrayGetFreeList
utDynarrayGetFreeList(utDynarray _Dynarray)1462 utDynarray utDynarrayGetFreeList(
1463     utDynarray _Dynarray)
1464 {
1465     return utDynarrays.FreeList[utDynarray2Index(_Dynarray)];
1466 }
1467 
1468 #undef utDynarraySetFreeList
utDynarraySetFreeList(utDynarray _Dynarray,utDynarray value)1469 void utDynarraySetFreeList(
1470     utDynarray _Dynarray,
1471     utDynarray value)
1472 {
1473     utDynarrays.FreeList[utDynarray2Index(_Dynarray)] = value;
1474 }
1475 
1476 #undef utSymArrayGetSymIndex
utSymArrayGetSymIndex(utSymArray _SymArray)1477 uint32 utSymArrayGetSymIndex(
1478     utSymArray _SymArray)
1479 {
1480     return utSymArrays.SymIndex[utSymArray2Index(_SymArray)];
1481 }
1482 
1483 #undef utSymArraySetSymIndex
utSymArraySetSymIndex(utSymArray _SymArray,uint32 value)1484 void utSymArraySetSymIndex(
1485     utSymArray _SymArray,
1486     uint32 value)
1487 {
1488     utSymArrays.SymIndex[utSymArray2Index(_SymArray)] = value;
1489 }
1490 
1491 #undef utSymArrayGetNumSym
utSymArrayGetNumSym(utSymArray _SymArray)1492 uint32 utSymArrayGetNumSym(
1493     utSymArray _SymArray)
1494 {
1495     return utSymArrays.NumSym[utSymArray2Index(_SymArray)];
1496 }
1497 
1498 #undef utSymArraySetNumSym
utSymArraySetNumSym(utSymArray _SymArray,uint32 value)1499 void utSymArraySetNumSym(
1500     utSymArray _SymArray,
1501     uint32 value)
1502 {
1503     utSymArrays.NumSym[utSymArray2Index(_SymArray)] = value;
1504 }
1505 
1506 #undef utSymArrayGetiSym
utSymArrayGetiSym(utSymArray _SymArray,uint32 x)1507 utSym utSymArrayGetiSym(
1508     utSymArray _SymArray,
1509     uint32 x)
1510 {
1511     return (utSymArrays.Sym)[utSymArrayGetSymIndex(_SymArray) + x];
1512 }
1513 
1514 #undef utSymArraySetiSym
utSymArraySetiSym(utSymArray SymArray,uint32 x,utSym value)1515 void utSymArraySetiSym(
1516     utSymArray SymArray,
1517     uint32 x,
1518     utSym value)
1519 {
1520     utSymArrays.Sym[utSymArrayGetSymIndex(SymArray) + x] = value;
1521 }
1522 
1523 #undef utSymArrayGetSym
utSymArrayGetSym(utSymArray SymArray)1524 utSym *utSymArrayGetSym(
1525     utSymArray SymArray)
1526 {
1527     return utSymArrays.Sym + utSymArrayGetSymIndex(SymArray);
1528 }
1529 
1530 #undef utSymArrayGetUsedSym
utSymArrayGetUsedSym(utSymArray _SymArray)1531 uint32 utSymArrayGetUsedSym(
1532     utSymArray _SymArray)
1533 {
1534     return utSymArrays.UsedSym[utSymArray2Index(_SymArray)];
1535 }
1536 
1537 #undef utSymArraySetUsedSym
utSymArraySetUsedSym(utSymArray _SymArray,uint32 value)1538 void utSymArraySetUsedSym(
1539     utSymArray _SymArray,
1540     uint32 value)
1541 {
1542     utSymArrays.UsedSym[utSymArray2Index(_SymArray)] = value;
1543 }
1544 
1545 #undef utSymArrayGetFreeList
utSymArrayGetFreeList(utSymArray _SymArray)1546 utSymArray utSymArrayGetFreeList(
1547     utSymArray _SymArray)
1548 {
1549     return utSymArrays.FreeList[utSymArray2Index(_SymArray)];
1550 }
1551 
1552 #undef utSymArraySetFreeList
utSymArraySetFreeList(utSymArray _SymArray,utSymArray value)1553 void utSymArraySetFreeList(
1554     utSymArray _SymArray,
1555     utSymArray value)
1556 {
1557     utSymArrays.FreeList[utSymArray2Index(_SymArray)] = value;
1558 }
1559 
1560 #endif
1561