1 |
|
|
/* $OpenBSD: dba.c,v 1.7 2017/02/09 18:26:17 schwarze Exp $ */ |
2 |
|
|
/* |
3 |
|
|
* Copyright (c) 2016, 2017 Ingo Schwarze <schwarze@openbsd.org> |
4 |
|
|
* |
5 |
|
|
* Permission to use, copy, modify, and distribute this software for any |
6 |
|
|
* purpose with or without fee is hereby granted, provided that the above |
7 |
|
|
* copyright notice and this permission notice appear in all copies. |
8 |
|
|
* |
9 |
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
10 |
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
11 |
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
12 |
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
13 |
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
14 |
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
15 |
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
16 |
|
|
* |
17 |
|
|
* Allocation-based version of the mandoc database, for read-write access. |
18 |
|
|
* The interface is defined in "dba.h". |
19 |
|
|
*/ |
20 |
|
|
#include <sys/types.h> |
21 |
|
|
#include <endian.h> |
22 |
|
|
#include <errno.h> |
23 |
|
|
#include <stddef.h> |
24 |
|
|
#include <stdint.h> |
25 |
|
|
#include <stdlib.h> |
26 |
|
|
#include <string.h> |
27 |
|
|
#include <unistd.h> |
28 |
|
|
|
29 |
|
|
#include "mandoc_aux.h" |
30 |
|
|
#include "mandoc_ohash.h" |
31 |
|
|
#include "mansearch.h" |
32 |
|
|
#include "dba_write.h" |
33 |
|
|
#include "dba_array.h" |
34 |
|
|
#include "dba.h" |
35 |
|
|
|
36 |
|
|
struct macro_entry { |
37 |
|
|
struct dba_array *pages; |
38 |
|
|
char value[]; |
39 |
|
|
}; |
40 |
|
|
|
41 |
|
|
static void *prepend(const char *, char); |
42 |
|
|
static void dba_pages_write(struct dba_array *); |
43 |
|
|
static int compare_names(const void *, const void *); |
44 |
|
|
static int compare_strings(const void *, const void *); |
45 |
|
|
|
46 |
|
|
static struct macro_entry |
47 |
|
|
*get_macro_entry(struct ohash *, const char *, int32_t); |
48 |
|
|
static void dba_macros_write(struct dba_array *); |
49 |
|
|
static void dba_macro_write(struct ohash *); |
50 |
|
|
static int compare_entries(const void *, const void *); |
51 |
|
|
|
52 |
|
|
|
53 |
|
|
/*** top-level functions **********************************************/ |
54 |
|
|
|
55 |
|
|
struct dba * |
56 |
|
|
dba_new(int32_t npages) |
57 |
|
|
{ |
58 |
|
|
struct dba *dba; |
59 |
|
|
struct ohash *macro; |
60 |
|
|
int32_t im; |
61 |
|
|
|
62 |
|
192 |
dba = mandoc_malloc(sizeof(*dba)); |
63 |
|
96 |
dba->pages = dba_array_new(npages, DBA_GROW); |
64 |
|
96 |
dba->macros = dba_array_new(MACRO_MAX, 0); |
65 |
✓✓ |
7104 |
for (im = 0; im < MACRO_MAX; im++) { |
66 |
|
3456 |
macro = mandoc_malloc(sizeof(*macro)); |
67 |
|
3456 |
mandoc_ohash_init(macro, 4, |
68 |
|
|
offsetof(struct macro_entry, value)); |
69 |
|
3456 |
dba_array_set(dba->macros, im, macro); |
70 |
|
|
} |
71 |
|
96 |
return dba; |
72 |
|
|
} |
73 |
|
|
|
74 |
|
|
void |
75 |
|
|
dba_free(struct dba *dba) |
76 |
|
|
{ |
77 |
|
|
struct dba_array *page; |
78 |
|
|
struct ohash *macro; |
79 |
|
|
struct macro_entry *entry; |
80 |
|
192 |
unsigned int slot; |
81 |
|
|
|
82 |
✓✓ |
7104 |
dba_array_FOREACH(dba->macros, macro) { |
83 |
✓✓ |
272626 |
for (entry = ohash_first(macro, &slot); entry != NULL; |
84 |
|
132857 |
entry = ohash_next(macro, &slot)) { |
85 |
|
132857 |
dba_array_free(entry->pages); |
86 |
|
132857 |
free(entry); |
87 |
|
|
} |
88 |
|
3456 |
ohash_delete(macro); |
89 |
|
3456 |
free(macro); |
90 |
|
|
} |
91 |
|
96 |
dba_array_free(dba->macros); |
92 |
|
|
|
93 |
|
96 |
dba_array_undel(dba->pages); |
94 |
✓✓ |
30426 |
dba_array_FOREACH(dba->pages, page) { |
95 |
|
15117 |
dba_array_free(dba_array_get(page, DBP_NAME)); |
96 |
|
15117 |
dba_array_free(dba_array_get(page, DBP_SECT)); |
97 |
|
15117 |
dba_array_free(dba_array_get(page, DBP_ARCH)); |
98 |
|
15117 |
free(dba_array_get(page, DBP_DESC)); |
99 |
|
15117 |
dba_array_free(dba_array_get(page, DBP_FILE)); |
100 |
|
15117 |
dba_array_free(page); |
101 |
|
|
} |
102 |
|
96 |
dba_array_free(dba->pages); |
103 |
|
|
|
104 |
|
96 |
free(dba); |
105 |
|
96 |
} |
106 |
|
|
|
107 |
|
|
/* |
108 |
|
|
* Write the complete mandoc database to disk; the format is: |
109 |
|
|
* - One integer each for magic and version. |
110 |
|
|
* - One pointer each to the macros table and to the final magic. |
111 |
|
|
* - The pages table. |
112 |
|
|
* - The macros table. |
113 |
|
|
* - And at the very end, the magic integer again. |
114 |
|
|
*/ |
115 |
|
|
int |
116 |
|
|
dba_write(const char *fname, struct dba *dba) |
117 |
|
|
{ |
118 |
|
|
int save_errno; |
119 |
|
|
int32_t pos_end, pos_macros, pos_macros_ptr; |
120 |
|
|
|
121 |
✗✓ |
182 |
if (dba_open(fname) == -1) |
122 |
|
|
return -1; |
123 |
|
91 |
dba_int_write(MANDOCDB_MAGIC); |
124 |
|
91 |
dba_int_write(MANDOCDB_VERSION); |
125 |
|
91 |
pos_macros_ptr = dba_skip(1, 2); |
126 |
|
91 |
dba_pages_write(dba->pages); |
127 |
|
91 |
pos_macros = dba_tell(); |
128 |
|
91 |
dba_macros_write(dba->macros); |
129 |
|
91 |
pos_end = dba_tell(); |
130 |
|
91 |
dba_int_write(MANDOCDB_MAGIC); |
131 |
|
91 |
dba_seek(pos_macros_ptr); |
132 |
|
91 |
dba_int_write(pos_macros); |
133 |
|
91 |
dba_int_write(pos_end); |
134 |
✗✓ |
91 |
if (dba_close() == -1) { |
135 |
|
|
save_errno = errno; |
136 |
|
|
unlink(fname); |
137 |
|
|
errno = save_errno; |
138 |
|
|
return -1; |
139 |
|
|
} |
140 |
|
91 |
return 0; |
141 |
|
91 |
} |
142 |
|
|
|
143 |
|
|
|
144 |
|
|
/*** functions for handling pages *************************************/ |
145 |
|
|
|
146 |
|
|
/* |
147 |
|
|
* Create a new page and append it to the pages table. |
148 |
|
|
*/ |
149 |
|
|
struct dba_array * |
150 |
|
|
dba_page_new(struct dba_array *pages, const char *arch, |
151 |
|
|
const char *desc, const char *file, enum form form) |
152 |
|
|
{ |
153 |
|
|
struct dba_array *page, *entry; |
154 |
|
|
|
155 |
|
30234 |
page = dba_array_new(DBP_MAX, 0); |
156 |
|
15117 |
entry = dba_array_new(1, DBA_STR | DBA_GROW); |
157 |
|
15117 |
dba_array_add(page, entry); |
158 |
|
15117 |
entry = dba_array_new(1, DBA_STR | DBA_GROW); |
159 |
|
15117 |
dba_array_add(page, entry); |
160 |
✓✓✓✓
|
26895 |
if (arch != NULL && *arch != '\0') { |
161 |
|
1247 |
entry = dba_array_new(1, DBA_STR | DBA_GROW); |
162 |
|
1247 |
dba_array_add(entry, (void *)arch); |
163 |
|
1247 |
} else |
164 |
|
|
entry = NULL; |
165 |
|
15117 |
dba_array_add(page, entry); |
166 |
|
15117 |
dba_array_add(page, mandoc_strdup(desc)); |
167 |
|
15117 |
entry = dba_array_new(1, DBA_STR | DBA_GROW); |
168 |
|
15117 |
dba_array_add(entry, prepend(file, form)); |
169 |
|
15117 |
dba_array_add(page, entry); |
170 |
|
15117 |
dba_array_add(pages, page); |
171 |
|
15117 |
return page; |
172 |
|
|
} |
173 |
|
|
|
174 |
|
|
/* |
175 |
|
|
* Add a section, architecture, or file name to an existing page. |
176 |
|
|
* Passing the NULL pointer for the architecture makes the page MI. |
177 |
|
|
* In that case, any earlier or later architectures are ignored. |
178 |
|
|
*/ |
179 |
|
|
void |
180 |
|
|
dba_page_add(struct dba_array *page, int32_t ie, const char *str) |
181 |
|
|
{ |
182 |
|
|
struct dba_array *entries; |
183 |
|
|
char *entry; |
184 |
|
|
|
185 |
|
139206 |
entries = dba_array_get(page, ie); |
186 |
✓✓ |
69603 |
if (ie == DBP_ARCH) { |
187 |
✓✓ |
12128 |
if (entries == NULL) |
188 |
|
10876 |
return; |
189 |
✓✗✗✓
|
2504 |
if (str == NULL || *str == '\0') { |
190 |
|
|
dba_array_free(entries); |
191 |
|
|
dba_array_set(page, DBP_ARCH, NULL); |
192 |
|
|
return; |
193 |
|
|
} |
194 |
|
|
} |
195 |
✗✓ |
58727 |
if (*str == '\0') |
196 |
|
|
return; |
197 |
✓✓ |
131279 |
dba_array_FOREACH(entries, entry) { |
198 |
✓✓✓✓
|
66019 |
if (ie == DBP_FILE && *entry < ' ') |
199 |
|
12709 |
entry++; |
200 |
✓✓ |
50895 |
if (strcmp(entry, str) == 0) |
201 |
|
37070 |
return; |
202 |
|
|
} |
203 |
|
21657 |
dba_array_add(entries, (void *)str); |
204 |
|
91260 |
} |
205 |
|
|
|
206 |
|
|
/* |
207 |
|
|
* Add an additional name to an existing page. |
208 |
|
|
*/ |
209 |
|
|
void |
210 |
|
|
dba_page_alias(struct dba_array *page, const char *name, uint64_t mask) |
211 |
|
|
{ |
212 |
|
|
struct dba_array *entries; |
213 |
|
|
char *entry; |
214 |
|
|
char maskbyte; |
215 |
|
|
|
216 |
✗✓ |
80972 |
if (*name == '\0') |
217 |
|
|
return; |
218 |
|
40486 |
maskbyte = mask & NAME_MASK; |
219 |
|
40486 |
entries = dba_array_get(page, DBP_NAME); |
220 |
✓✓ |
264833 |
dba_array_FOREACH(entries, entry) { |
221 |
✓✓ |
195979 |
if (strcmp(entry + 1, name) == 0) { |
222 |
|
12118 |
*entry |= maskbyte; |
223 |
|
12118 |
return; |
224 |
|
|
} |
225 |
|
|
} |
226 |
|
28368 |
dba_array_add(entries, prepend(name, maskbyte)); |
227 |
|
68854 |
} |
228 |
|
|
|
229 |
|
|
/* |
230 |
|
|
* Return a pointer to a temporary copy of instr with inbyte prepended. |
231 |
|
|
*/ |
232 |
|
|
static void * |
233 |
|
|
prepend(const char *instr, char inbyte) |
234 |
|
|
{ |
235 |
|
|
static char *outstr = NULL; |
236 |
|
|
static size_t outlen = 0; |
237 |
|
|
size_t newlen; |
238 |
|
|
|
239 |
|
86970 |
newlen = strlen(instr) + 1; |
240 |
✓✓ |
43485 |
if (newlen > outlen) { |
241 |
|
182 |
outstr = mandoc_realloc(outstr, newlen + 1); |
242 |
|
182 |
outlen = newlen; |
243 |
|
182 |
} |
244 |
|
43485 |
*outstr = inbyte; |
245 |
|
43485 |
memcpy(outstr + 1, instr, newlen); |
246 |
|
43485 |
return outstr; |
247 |
|
|
} |
248 |
|
|
|
249 |
|
|
/* |
250 |
|
|
* Write the pages table to disk; the format is: |
251 |
|
|
* - One integer containing the number of pages. |
252 |
|
|
* - For each page, five pointers to the names, sections, |
253 |
|
|
* architectures, description, and file names of the page. |
254 |
|
|
* MI pages write 0 instead of the architecture pointer. |
255 |
|
|
* - One list each for names, sections, architectures, descriptions and |
256 |
|
|
* file names. The description for each page ends with a NUL byte. |
257 |
|
|
* For all the other lists, each string ends with a NUL byte, |
258 |
|
|
* and the last string for a page ends with two NUL bytes. |
259 |
|
|
* - To assure alignment of following integers, |
260 |
|
|
* the end is padded with NUL bytes up to a multiple of four bytes. |
261 |
|
|
*/ |
262 |
|
|
static void |
263 |
|
|
dba_pages_write(struct dba_array *pages) |
264 |
|
|
{ |
265 |
|
|
struct dba_array *page, *entry; |
266 |
|
|
int32_t pos_pages, pos_end; |
267 |
|
|
|
268 |
|
182 |
pos_pages = dba_array_writelen(pages, 5); |
269 |
✓✓ |
30416 |
dba_array_FOREACH(pages, page) { |
270 |
|
15117 |
dba_array_setpos(page, DBP_NAME, dba_tell()); |
271 |
|
15117 |
entry = dba_array_get(page, DBP_NAME); |
272 |
|
15117 |
dba_array_sort(entry, compare_names); |
273 |
|
15117 |
dba_array_writelst(entry); |
274 |
|
|
} |
275 |
✓✓ |
30416 |
dba_array_FOREACH(pages, page) { |
276 |
|
15117 |
dba_array_setpos(page, DBP_SECT, dba_tell()); |
277 |
|
15117 |
entry = dba_array_get(page, DBP_SECT); |
278 |
|
15117 |
dba_array_sort(entry, compare_strings); |
279 |
|
15117 |
dba_array_writelst(entry); |
280 |
|
|
} |
281 |
✓✓ |
15299 |
dba_array_FOREACH(pages, page) { |
282 |
✓✓ |
15117 |
if ((entry = dba_array_get(page, DBP_ARCH)) != NULL) { |
283 |
|
1247 |
dba_array_setpos(page, DBP_ARCH, dba_tell()); |
284 |
|
1247 |
dba_array_sort(entry, compare_strings); |
285 |
|
1247 |
dba_array_writelst(entry); |
286 |
|
1247 |
} else |
287 |
|
13870 |
dba_array_setpos(page, DBP_ARCH, 0); |
288 |
|
|
} |
289 |
✓✓ |
30416 |
dba_array_FOREACH(pages, page) { |
290 |
|
15117 |
dba_array_setpos(page, DBP_DESC, dba_tell()); |
291 |
|
15117 |
dba_str_write(dba_array_get(page, DBP_DESC)); |
292 |
|
|
} |
293 |
✓✓ |
30416 |
dba_array_FOREACH(pages, page) { |
294 |
|
15117 |
dba_array_setpos(page, DBP_FILE, dba_tell()); |
295 |
|
15117 |
dba_array_writelst(dba_array_get(page, DBP_FILE)); |
296 |
|
|
} |
297 |
|
91 |
pos_end = dba_align(); |
298 |
|
91 |
dba_seek(pos_pages); |
299 |
✓✓ |
30416 |
dba_array_FOREACH(pages, page) |
300 |
|
15117 |
dba_array_writepos(page); |
301 |
|
91 |
dba_seek(pos_end); |
302 |
|
91 |
} |
303 |
|
|
|
304 |
|
|
static int |
305 |
|
|
compare_names(const void *vp1, const void *vp2) |
306 |
|
|
{ |
307 |
|
|
const char *cp1, *cp2; |
308 |
|
|
int diff; |
309 |
|
|
|
310 |
|
115100 |
cp1 = *(const char * const *)vp1; |
311 |
|
57550 |
cp2 = *(const char * const *)vp2; |
312 |
✓✓ |
172650 |
return (diff = *cp2 - *cp1) ? diff : |
313 |
|
46088 |
strcasecmp(cp1 + 1, cp2 + 1); |
314 |
|
|
} |
315 |
|
|
|
316 |
|
|
static int |
317 |
|
|
compare_strings(const void *vp1, const void *vp2) |
318 |
|
|
{ |
319 |
|
|
const char *cp1, *cp2; |
320 |
|
|
|
321 |
|
1212 |
cp1 = *(const char * const *)vp1; |
322 |
|
606 |
cp2 = *(const char * const *)vp2; |
323 |
|
606 |
return strcmp(cp1, cp2); |
324 |
|
|
} |
325 |
|
|
|
326 |
|
|
/*** functions for handling macros ************************************/ |
327 |
|
|
|
328 |
|
|
/* |
329 |
|
|
* In the hash table for a single macro, look up an entry by |
330 |
|
|
* the macro value or add an empty one if it doesn't exist yet. |
331 |
|
|
*/ |
332 |
|
|
static struct macro_entry * |
333 |
|
|
get_macro_entry(struct ohash *macro, const char *value, int32_t np) |
334 |
|
|
{ |
335 |
|
|
struct macro_entry *entry; |
336 |
|
|
size_t len; |
337 |
|
|
unsigned int slot; |
338 |
|
|
|
339 |
|
557982 |
slot = ohash_qlookup(macro, value); |
340 |
✓✓ |
278991 |
if ((entry = ohash_find(macro, slot)) == NULL) { |
341 |
|
132857 |
len = strlen(value) + 1; |
342 |
|
132857 |
entry = mandoc_malloc(sizeof(*entry) + len); |
343 |
|
132857 |
memcpy(&entry->value, value, len); |
344 |
|
132857 |
entry->pages = dba_array_new(np, DBA_GROW); |
345 |
|
132857 |
ohash_insert(macro, slot, entry); |
346 |
|
132857 |
} |
347 |
|
278991 |
return entry; |
348 |
|
|
} |
349 |
|
|
|
350 |
|
|
/* |
351 |
|
|
* In addition to get_macro_entry(), add multiple page references, |
352 |
|
|
* converting them from the on-disk format (byte offsets in the file) |
353 |
|
|
* to page pointers in memory. |
354 |
|
|
*/ |
355 |
|
|
void |
356 |
|
|
dba_macro_new(struct dba *dba, int32_t im, const char *value, |
357 |
|
|
const int32_t *pp) |
358 |
|
|
{ |
359 |
|
|
struct macro_entry *entry; |
360 |
|
|
const int32_t *ip; |
361 |
|
|
int32_t np; |
362 |
|
|
|
363 |
|
|
np = 0; |
364 |
✓✓ |
29017 |
for (ip = pp; *ip; ip++) |
365 |
|
7457 |
np++; |
366 |
|
|
|
367 |
|
4701 |
entry = get_macro_entry(dba_array_get(dba->macros, im), value, np); |
368 |
✓✓ |
24316 |
for (ip = pp; *ip; ip++) |
369 |
|
14914 |
dba_array_add(entry->pages, dba_array_get(dba->pages, |
370 |
|
7457 |
be32toh(*ip) / 5 / sizeof(*ip) - 1)); |
371 |
|
4701 |
} |
372 |
|
|
|
373 |
|
|
/* |
374 |
|
|
* In addition to get_macro_entry(), add one page reference, |
375 |
|
|
* directly taking the in-memory page pointer as an argument. |
376 |
|
|
*/ |
377 |
|
|
void |
378 |
|
|
dba_macro_add(struct dba_array *macros, int32_t im, const char *value, |
379 |
|
|
struct dba_array *page) |
380 |
|
|
{ |
381 |
|
|
struct macro_entry *entry; |
382 |
|
|
|
383 |
✓✓ |
548604 |
if (*value == '\0') |
384 |
|
12 |
return; |
385 |
|
274290 |
entry = get_macro_entry(dba_array_get(macros, im), value, 1); |
386 |
|
274290 |
dba_array_add(entry->pages, page); |
387 |
|
548592 |
} |
388 |
|
|
|
389 |
|
|
/* |
390 |
|
|
* Write the macros table to disk; the format is: |
391 |
|
|
* - The number of macro tables (actually, MACRO_MAX). |
392 |
|
|
* - That number of pointers to the individual macro tables. |
393 |
|
|
* - The individual macro tables. |
394 |
|
|
*/ |
395 |
|
|
static void |
396 |
|
|
dba_macros_write(struct dba_array *macros) |
397 |
|
|
{ |
398 |
|
|
struct ohash *macro; |
399 |
|
|
int32_t im, pos_macros, pos_end; |
400 |
|
|
|
401 |
|
182 |
pos_macros = dba_array_writelen(macros, 1); |
402 |
|
|
im = 0; |
403 |
✓✓ |
6734 |
dba_array_FOREACH(macros, macro) { |
404 |
|
3276 |
dba_array_setpos(macros, im++, dba_tell()); |
405 |
|
3276 |
dba_macro_write(macro); |
406 |
|
|
} |
407 |
|
91 |
pos_end = dba_tell(); |
408 |
|
91 |
dba_seek(pos_macros); |
409 |
|
91 |
dba_array_writepos(macros); |
410 |
|
91 |
dba_seek(pos_end); |
411 |
|
91 |
} |
412 |
|
|
|
413 |
|
|
/* |
414 |
|
|
* Write one individual macro table to disk; the format is: |
415 |
|
|
* - The number of entries in the table. |
416 |
|
|
* - For each entry, two pointers, the first one to the value |
417 |
|
|
* and the second one to the list of pages. |
418 |
|
|
* - A list of values, each ending in a NUL byte. |
419 |
|
|
* - To assure alignment of following integers, |
420 |
|
|
* padding with NUL bytes up to a multiple of four bytes. |
421 |
|
|
* - A list of pointers to pages, each list ending in a 0 integer. |
422 |
|
|
*/ |
423 |
|
|
static void |
424 |
|
|
dba_macro_write(struct ohash *macro) |
425 |
|
|
{ |
426 |
|
|
struct macro_entry **entries, *entry; |
427 |
|
|
struct dba_array *page; |
428 |
|
|
int32_t *kpos, *dpos; |
429 |
|
6552 |
unsigned int ie, ne, slot; |
430 |
|
|
int use; |
431 |
|
|
int32_t addr, pos_macro, pos_end; |
432 |
|
|
|
433 |
|
|
/* Temporary storage for filtering and sorting. */ |
434 |
|
|
|
435 |
|
3276 |
ne = ohash_entries(macro); |
436 |
|
3276 |
entries = mandoc_reallocarray(NULL, ne, sizeof(*entries)); |
437 |
|
3276 |
kpos = mandoc_reallocarray(NULL, ne, sizeof(*kpos)); |
438 |
|
3276 |
dpos = mandoc_reallocarray(NULL, ne, sizeof(*dpos)); |
439 |
|
|
|
440 |
|
|
/* Build a list of non-empty entries and sort it. */ |
441 |
|
|
|
442 |
|
|
ne = 0; |
443 |
✓✓ |
272266 |
for (entry = ohash_first(macro, &slot); entry != NULL; |
444 |
|
132857 |
entry = ohash_next(macro, &slot)) { |
445 |
|
|
use = 0; |
446 |
✓✓ |
547461 |
dba_array_FOREACH(entry->pages, page) |
447 |
✗✓ |
281747 |
if (dba_array_getpos(page)) |
448 |
|
281747 |
use = 1; |
449 |
✓✗ |
132857 |
if (use) |
450 |
|
132857 |
entries[ne++] = entry; |
451 |
|
|
} |
452 |
|
3276 |
qsort(entries, ne, sizeof(*entries), compare_entries); |
453 |
|
|
|
454 |
|
|
/* Number of entries, and space for the pointer pairs. */ |
455 |
|
|
|
456 |
|
3276 |
dba_int_write(ne); |
457 |
|
3276 |
pos_macro = dba_skip(2, ne); |
458 |
|
|
|
459 |
|
|
/* String table. */ |
460 |
|
|
|
461 |
✓✓ |
272266 |
for (ie = 0; ie < ne; ie++) { |
462 |
|
132857 |
kpos[ie] = dba_tell(); |
463 |
|
132857 |
dba_str_write(entries[ie]->value); |
464 |
|
|
} |
465 |
|
3276 |
dba_align(); |
466 |
|
|
|
467 |
|
|
/* Pages table. */ |
468 |
|
|
|
469 |
✓✓ |
408399 |
for (ie = 0; ie < ne; ie++) { |
470 |
|
268990 |
dpos[ie] = dba_tell(); |
471 |
✓✓ |
547461 |
dba_array_FOREACH(entries[ie]->pages, page) |
472 |
✗✓ |
281747 |
if ((addr = dba_array_getpos(page))) |
473 |
|
281747 |
dba_int_write(addr); |
474 |
|
132857 |
dba_int_write(0); |
475 |
|
|
} |
476 |
|
|
pos_end = dba_tell(); |
477 |
|
|
|
478 |
|
|
/* Fill in the pointer pairs. */ |
479 |
|
|
|
480 |
|
3276 |
dba_seek(pos_macro); |
481 |
✓✓ |
272266 |
for (ie = 0; ie < ne; ie++) { |
482 |
|
132857 |
dba_int_write(kpos[ie]); |
483 |
|
132857 |
dba_int_write(dpos[ie]); |
484 |
|
|
} |
485 |
|
3276 |
dba_seek(pos_end); |
486 |
|
|
|
487 |
|
3276 |
free(entries); |
488 |
|
3276 |
free(kpos); |
489 |
|
3276 |
free(dpos); |
490 |
|
3276 |
} |
491 |
|
|
|
492 |
|
|
static int |
493 |
|
|
compare_entries(const void *vp1, const void *vp2) |
494 |
|
|
{ |
495 |
|
|
const struct macro_entry *ep1, *ep2; |
496 |
|
|
|
497 |
|
2955122 |
ep1 = *(const struct macro_entry * const *)vp1; |
498 |
|
1477561 |
ep2 = *(const struct macro_entry * const *)vp2; |
499 |
|
1477561 |
return strcmp(ep1->value, ep2->value); |
500 |
|
|
} |