1 // Copyright (C) 2000 - 2002 Hewlett-Packard Company
2 //
3 // This program is free software; you can redistribute it and/or modify it
4 // under the term of the GNU Lesser General Public License as published by the
5 // Free Software Foundation; either version 2 of the License, or (at your
6 // option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful, but WITHOUT
9 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
11 // for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public License
14 // along with this program; if not, write to the Free Software Foundation,
15 // Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16 // _________________
17 
18 #include <stdlib.h>
19 #include <stdio.h>
20 
21 #if (! (defined(JUDY1) || defined(JUDYL)))
22 #error:  One of -DJUDY1 or -DJUDYL must be specified.
23 #endif
24 
25 #define	TERMINATOR 999		// terminator for Alloc tables
26 
27 #define BPW sizeof(Word_t)	// define bytes per word
28 
29 #ifdef JUDY1
30 #include "Judy1.h"
31 #else
32 #include "JudyL.h"
33 #endif
34 
35 FILE *fd;
36 
37 // Definitions come from header files Judy1.h and JudyL.h:
38 
39 int AllocSizes[] = ALLOCSIZES;
40 
41 #define	ROUNDUP(BYTES,BPW,OFFSETW) \
42 	((((BYTES) + (BPW) - 1) / (BPW)) + (OFFSETW))
43 
44 
45 // ****************************************************************************
46 // G E N   T A B L E
47 //
48 // Note:  "const" is required for newer compilers.
49 
GenTable(const char * TableName,const char * TableSize,int IndexBytes,int LeafSize,int ValueBytes,int OffsetWords)50 FUNCTION void GenTable(
51     const char * TableName,	// name of table string
52     const char * TableSize,	// dimentioned size string
53     int		 IndexBytes,	// bytes per Index
54     int		 LeafSize,	// number elements in object
55     int		 ValueBytes,	// bytes per Value
56     int		 OffsetWords)	// 1 for LEAFW
57 {
58     int *	 PAllocSizes = AllocSizes;
59     int		 OWord;
60     int		 CurWord;
61     int		 IWord;
62     int		 ii;
63     int		 BytesOfIndex;
64     int		 BytesOfObject;
65     int		 Index;
66     int		 LastWords;
67     int		 Words [1000] = { 0 };
68     int		 Offset[1000] = { 0 };
69     int		 MaxWords;
70 
71     MaxWords  =	ROUNDUP((IndexBytes + ValueBytes) * LeafSize, BPW, OffsetWords);
72     Words[0]  = 0;
73     Offset[0] = 0;
74     CurWord   = TERMINATOR;
75 
76 // Walk through all number of Indexes in table:
77 
78     for (Index = 1; /* null */; ++Index)
79     {
80 
81 // Calculate byte required for next size:
82 
83 	BytesOfIndex  = IndexBytes * Index;
84 	BytesOfObject = (IndexBytes + ValueBytes) * Index;
85 
86 // Round up and calculate words required for next size:
87 
88         OWord =	ROUNDUP(BytesOfObject, BPW, OffsetWords);
89         IWord =	ROUNDUP(BytesOfIndex,  BPW, OffsetWords);
90 
91 // Root-level leaves of population of 1 and 2 do not have the 1 word offset:
92 
93 // Save minimum value of offset:
94 
95         Offset[Index] = IWord;
96 
97 // Round up to next available size of words:
98 
99 	while (OWord > *PAllocSizes) PAllocSizes++;
100 
101         if (Index == LeafSize)
102         {
103 	    CurWord = Words[Index] = OWord;
104             break;
105         }
106 //      end of available sizes ?
107 
108 	if (*PAllocSizes == TERMINATOR)
109         {
110             fprintf(stderr, "BUG, in %sPopToWords, sizes not big enough for object\n", TableName);
111 	    exit(1);
112         }
113 
114 // Save words required and last word:
115 
116         if (*PAllocSizes < MaxWords) { CurWord = Words[Index] = *PAllocSizes; }
117         else                         { CurWord = Words[Index] = MaxWords; }
118 
119     } // for each index
120 
121     LastWords = TERMINATOR;
122 
123 // Round up to largest size in each group of malloc sizes:
124 
125     for (ii = LeafSize; ii > 0; ii--)
126     {
127         if (LastWords > (Words[ii] - ii)) LastWords = Offset[ii];
128         else                              Offset[ii] = LastWords;
129     }
130 
131 // Print the PopToWords[] table:
132 
133     fprintf(fd,"\n//\tobject uses %d words\n", CurWord);
134     fprintf(fd,"//\t%s = %d\n", TableSize, LeafSize);
135 
136     fprintf(fd,"const uint8_t\n");
137     fprintf(fd,"%sPopToWords[%s + 1] =\n", TableName, TableSize);
138     fprintf(fd,"{\n\t 0,");
139 
140     for (ii = 1; ii <= LeafSize; ii++)
141     {
142 
143 // 8 columns per line, starting with 1:
144 
145 	if ((ii % 8) == 1) fprintf(fd,"\n\t");
146 
147 	fprintf(fd,"%2d", Words[ii]);
148 
149 // If not last number place comma:
150 
151 	if (ii != LeafSize) fprintf(fd,", ");
152     }
153     fprintf(fd,"\n};\n");
154 
155 // Print the Offset table if needed:
156 
157     if (! ValueBytes) return;
158 
159     fprintf(fd,"const uint8_t\n");
160     fprintf(fd,"%sOffset[%s + 1] =\n", TableName, TableSize);
161     fprintf(fd,"{\n");
162     fprintf(fd,"\t 0,");
163 
164     for (ii = 1; ii <= LeafSize; ii++)
165     {
166         if ((ii % 8) == 1) fprintf(fd,"\n\t");
167 
168 	fprintf(fd,"%2d", Offset[ii]);
169 
170 	if (ii != LeafSize) fprintf(fd,", ");
171     }
172     fprintf(fd,"\n};\n");
173 
174 } // GenTable()
175 
176 
177 // ****************************************************************************
178 // M A I N
179 
180 #ifdef JU_WMAIN
wmain()181 FUNCTION int wmain()
182 #else
183 FUNCTION int main()
184 #endif
185 {
186     int ii;
187 
188 #ifdef JUDY1
189     char *fname = "Judy1Tables.c";
190 #else
191     char *fname = "JudyLTables.c";
192 #endif
193 
194     if ((fd = fopen(fname, "w")) == NULL){
195 	perror("FATAL ERROR: could not write to Judy[1L]Tables.c file\n");
196 	return (-1);
197     }
198 
199 
200     fprintf(fd,"// @(#) From generation tool: JudyTablesGen.c\n");
201     fprintf(fd,"//\n\n");
202 
203 
204 // ================================ Judy1 =================================
205 #ifdef JUDY1
206 
207     fprintf(fd,"#include \"Judy1.h\"\n");
208 
209     fprintf(fd,"// Leave the malloc() sizes readable in the binary (via "
210 	   "strings(1)):\n");
211     fprintf(fd,"const char * Judy1MallocSizes = \"Judy1MallocSizes =");
212 
213     for (ii = 0; AllocSizes[ii] != TERMINATOR; ii++)
214 	fprintf(fd," %d,", AllocSizes[ii]);
215 
216 #ifndef JU_64BIT
217     fprintf(fd," Leaf1 = %d\";\n\n", ( int ) cJ1_LEAF1_MAXPOP1);
218 #else
219     fprintf(fd,"\";\n\n");			// no Leaf1 in this case.
220 #endif
221 
222 // ================================ 32 bit ================================
223 #ifndef JU_64BIT
224 
225     GenTable("j__1_BranchBJP","cJU_BITSPERSUBEXPB", 8, cJU_BITSPERSUBEXPB,0,0);
226 
227     GenTable("j__1_Leaf1", "cJ1_LEAF1_MAXPOP1", 1, cJ1_LEAF1_MAXPOP1, 0, 0);
228     GenTable("j__1_Leaf2", "cJ1_LEAF2_MAXPOP1", 2, cJ1_LEAF2_MAXPOP1, 0, 0);
229     GenTable("j__1_Leaf3", "cJ1_LEAF3_MAXPOP1", 3, cJ1_LEAF3_MAXPOP1, 0, 0);
230     GenTable("j__1_LeafW", "cJ1_LEAFW_MAXPOP1", 4, cJ1_LEAFW_MAXPOP1, 0, 1);
231 
232 #endif
233 
234 // ================================ 64 bit ================================
235 #ifdef JU_64BIT
236     GenTable("j__1_BranchBJP","cJU_BITSPERSUBEXPB",16, cJU_BITSPERSUBEXPB,0,0);
237 
238     GenTable("j__1_Leaf2", "cJ1_LEAF2_MAXPOP1", 2, cJ1_LEAF2_MAXPOP1, 0, 0);
239     GenTable("j__1_Leaf3", "cJ1_LEAF3_MAXPOP1", 3, cJ1_LEAF3_MAXPOP1, 0, 0);
240     GenTable("j__1_Leaf4", "cJ1_LEAF4_MAXPOP1", 4, cJ1_LEAF4_MAXPOP1, 0, 0);
241     GenTable("j__1_Leaf5", "cJ1_LEAF5_MAXPOP1", 5, cJ1_LEAF5_MAXPOP1, 0, 0);
242     GenTable("j__1_Leaf6", "cJ1_LEAF6_MAXPOP1", 6, cJ1_LEAF6_MAXPOP1, 0, 0);
243     GenTable("j__1_Leaf7", "cJ1_LEAF7_MAXPOP1", 7, cJ1_LEAF7_MAXPOP1, 0, 0);
244     GenTable("j__1_LeafW", "cJ1_LEAFW_MAXPOP1", 8, cJ1_LEAFW_MAXPOP1, 0, 1);
245 #endif
246 #endif // JUDY1
247 
248 
249 // ================================ JudyL =================================
250 #ifdef JUDYL
251 
252     fprintf(fd,"#include \"JudyL.h\"\n");
253 
254     fprintf(fd,"// Leave the malloc() sizes readable in the binary (via "
255 	   "strings(1)):\n");
256     fprintf(fd,"const char * JudyLMallocSizes = \"JudyLMallocSizes =");
257 
258     for (ii = 0; AllocSizes[ii] != TERMINATOR; ii++)
259 	fprintf(fd," %d,", AllocSizes[ii]);
260 
261     fprintf(fd," Leaf1 = %ld\";\n\n", (Word_t)cJL_LEAF1_MAXPOP1);
262 
263 #ifndef JU_64BIT
264 // ================================ 32 bit ================================
265     GenTable("j__L_BranchBJP","cJU_BITSPERSUBEXPB", 8, cJU_BITSPERSUBEXPB, 0,0);
266 
267     GenTable("j__L_Leaf1", "cJL_LEAF1_MAXPOP1",  1, cJL_LEAF1_MAXPOP1, BPW,0);
268     GenTable("j__L_Leaf2", "cJL_LEAF2_MAXPOP1",  2, cJL_LEAF2_MAXPOP1, BPW,0);
269     GenTable("j__L_Leaf3", "cJL_LEAF3_MAXPOP1",  3, cJL_LEAF3_MAXPOP1, BPW,0);
270     GenTable("j__L_LeafW", "cJL_LEAFW_MAXPOP1",  4, cJL_LEAFW_MAXPOP1, BPW,1);
271     GenTable("j__L_LeafV", "cJU_BITSPERSUBEXPL", 4, cJU_BITSPERSUBEXPL,  0,0);
272 #endif // 32 BIT
273 
274 #ifdef JU_64BIT
275 // ================================ 64 bit ================================
276     GenTable("j__L_BranchBJP","cJU_BITSPERSUBEXPB",16, cJU_BITSPERSUBEXPB, 0,0);
277 
278     GenTable("j__L_Leaf1", "cJL_LEAF1_MAXPOP1",  1, cJL_LEAF1_MAXPOP1,  BPW,0);
279     GenTable("j__L_Leaf2", "cJL_LEAF2_MAXPOP1",  2, cJL_LEAF2_MAXPOP1,  BPW,0);
280     GenTable("j__L_Leaf3", "cJL_LEAF3_MAXPOP1",  3, cJL_LEAF3_MAXPOP1,  BPW,0);
281     GenTable("j__L_Leaf4", "cJL_LEAF4_MAXPOP1",  4, cJL_LEAF4_MAXPOP1,  BPW,0);
282     GenTable("j__L_Leaf5", "cJL_LEAF5_MAXPOP1",  5, cJL_LEAF5_MAXPOP1,  BPW,0);
283     GenTable("j__L_Leaf6", "cJL_LEAF6_MAXPOP1",  6, cJL_LEAF6_MAXPOP1,  BPW,0);
284     GenTable("j__L_Leaf7", "cJL_LEAF7_MAXPOP1",  7, cJL_LEAF7_MAXPOP1,  BPW,0);
285     GenTable("j__L_LeafW", "cJL_LEAFW_MAXPOP1",  8, cJL_LEAFW_MAXPOP1,  BPW,1);
286     GenTable("j__L_LeafV", "cJU_BITSPERSUBEXPL", 8, cJU_BITSPERSUBEXPL, 0,0);
287 #endif // 64 BIT
288 
289 #endif // JUDYL
290     fclose(fd);
291 
292     return(0);
293 
294 } // main()
295