cdbw.c revision 1.4 1 1.4 joerg /* $NetBSD: cdbw.c,v 1.4 2012/06/03 21:02:50 joerg Exp $ */
2 1.1 joerg /*-
3 1.1 joerg * Copyright (c) 2009, 2010 The NetBSD Foundation, Inc.
4 1.1 joerg * All rights reserved.
5 1.1 joerg *
6 1.1 joerg * This code is derived from software contributed to The NetBSD Foundation
7 1.1 joerg * by Joerg Sonnenberger.
8 1.1 joerg *
9 1.1 joerg * Redistribution and use in source and binary forms, with or without
10 1.1 joerg * modification, are permitted provided that the following conditions
11 1.1 joerg * are met:
12 1.1 joerg *
13 1.1 joerg * 1. Redistributions of source code must retain the above copyright
14 1.1 joerg * notice, this list of conditions and the following disclaimer.
15 1.1 joerg * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 joerg * notice, this list of conditions and the following disclaimer in
17 1.1 joerg * the documentation and/or other materials provided with the
18 1.1 joerg * distribution.
19 1.1 joerg *
20 1.1 joerg * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 1.1 joerg * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 1.1 joerg * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 1.1 joerg * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 1.1 joerg * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 joerg * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 1.1 joerg * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 1.1 joerg * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 1.1 joerg * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 1.1 joerg * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 1.1 joerg * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 joerg * SUCH DAMAGE.
32 1.1 joerg */
33 1.1 joerg
34 1.1 joerg #if HAVE_NBTOOL_CONFIG_H
35 1.1 joerg #include "nbtool_config.h"
36 1.1 joerg #endif
37 1.1 joerg
38 1.1 joerg #include <sys/cdefs.h>
39 1.4 joerg __RCSID("$NetBSD: cdbw.c,v 1.4 2012/06/03 21:02:50 joerg Exp $");
40 1.1 joerg
41 1.1 joerg #include "namespace.h"
42 1.1 joerg
43 1.4 joerg #if !HAVE_NBTOOL_CONFIG_H || HAVE_SYS_ENDIAN_H
44 1.1 joerg #include <sys/endian.h>
45 1.4 joerg #endif
46 1.1 joerg #include <sys/queue.h>
47 1.1 joerg #include <cdbw.h>
48 1.1 joerg #include <stdlib.h>
49 1.1 joerg #include <string.h>
50 1.1 joerg #include <unistd.h>
51 1.1 joerg
52 1.1 joerg #ifdef __weak_alias
53 1.1 joerg __weak_alias(cdbw_close,_cdbw_close)
54 1.1 joerg __weak_alias(cdbw_open,_cdbw_open)
55 1.1 joerg __weak_alias(cdbw_output,_cdbw_output)
56 1.1 joerg __weak_alias(cdbw_put,_cdbw_put)
57 1.1 joerg __weak_alias(cdbw_put_data,_cdbw_put_data)
58 1.1 joerg __weak_alias(cdbw_put_key,_cdbw_put_key)
59 1.1 joerg #endif
60 1.1 joerg
61 1.1 joerg struct key_hash {
62 1.1 joerg SLIST_ENTRY(key_hash) link;
63 1.1 joerg uint32_t hashes[3];
64 1.1 joerg uint32_t idx;
65 1.1 joerg void *key;
66 1.1 joerg size_t keylen;
67 1.1 joerg };
68 1.1 joerg
69 1.1 joerg SLIST_HEAD(key_hash_head, key_hash);
70 1.1 joerg
71 1.1 joerg struct cdbw {
72 1.1 joerg size_t data_counter;
73 1.1 joerg size_t data_allocated;
74 1.1 joerg size_t data_size;
75 1.1 joerg size_t *data_len;
76 1.1 joerg void **data_ptr;
77 1.1 joerg
78 1.1 joerg size_t hash_size;
79 1.1 joerg struct key_hash_head *hash;
80 1.1 joerg size_t key_counter;
81 1.1 joerg };
82 1.1 joerg
83 1.1 joerg /* Max. data counter that allows the index size to be 32bit. */
84 1.1 joerg static const uint32_t max_data_counter = 0xccccccccU;
85 1.1 joerg
86 1.1 joerg struct cdbw *
87 1.1 joerg cdbw_open(void)
88 1.1 joerg {
89 1.1 joerg struct cdbw *cdbw;
90 1.1 joerg size_t i;
91 1.1 joerg
92 1.1 joerg cdbw = calloc(sizeof(*cdbw), 1);
93 1.1 joerg if (cdbw == NULL)
94 1.1 joerg return NULL;
95 1.1 joerg
96 1.1 joerg cdbw->hash_size = 1024;
97 1.1 joerg cdbw->hash = calloc(cdbw->hash_size, sizeof(*cdbw->hash));
98 1.1 joerg if (cdbw->hash == NULL) {
99 1.1 joerg free(cdbw);
100 1.1 joerg return NULL;
101 1.1 joerg }
102 1.1 joerg
103 1.1 joerg for (i = 0; i < cdbw->hash_size; ++i)
104 1.1 joerg SLIST_INIT(cdbw->hash + i);
105 1.1 joerg
106 1.1 joerg return cdbw;
107 1.1 joerg }
108 1.1 joerg
109 1.1 joerg int
110 1.1 joerg cdbw_put(struct cdbw *cdbw, const void *key, size_t keylen,
111 1.1 joerg const void *data, size_t datalen)
112 1.1 joerg {
113 1.1 joerg uint32_t idx;
114 1.1 joerg int rv;
115 1.1 joerg
116 1.1 joerg rv = cdbw_put_data(cdbw, data, datalen, &idx);
117 1.1 joerg if (rv)
118 1.1 joerg return rv;
119 1.1 joerg rv = cdbw_put_key(cdbw, key, keylen, idx);
120 1.1 joerg if (rv) {
121 1.1 joerg --cdbw->data_counter;
122 1.1 joerg free(cdbw->data_ptr[cdbw->data_counter]);
123 1.1 joerg cdbw->data_size -= datalen;
124 1.1 joerg return rv;
125 1.1 joerg }
126 1.1 joerg return 0;
127 1.1 joerg }
128 1.1 joerg
129 1.1 joerg int
130 1.1 joerg cdbw_put_data(struct cdbw *cdbw, const void *data, size_t datalen,
131 1.1 joerg uint32_t *idx)
132 1.1 joerg {
133 1.1 joerg
134 1.1 joerg if (cdbw->data_counter == max_data_counter)
135 1.1 joerg return -1;
136 1.1 joerg
137 1.1 joerg if (cdbw->data_size + datalen < cdbw->data_size ||
138 1.1 joerg cdbw->data_size + datalen > 0xffffffffU)
139 1.1 joerg return -1; /* Overflow */
140 1.1 joerg
141 1.1 joerg if (cdbw->data_allocated == cdbw->data_counter) {
142 1.1 joerg void **new_data_ptr;
143 1.1 joerg size_t *new_data_len;
144 1.1 joerg size_t new_allocated;
145 1.1 joerg
146 1.1 joerg if (cdbw->data_allocated == 0)
147 1.1 joerg new_allocated = 256;
148 1.1 joerg else
149 1.1 joerg new_allocated = cdbw->data_allocated * 2;
150 1.1 joerg
151 1.1 joerg new_data_ptr = realloc(cdbw->data_ptr,
152 1.1 joerg sizeof(*cdbw->data_ptr) * new_allocated);
153 1.1 joerg if (new_data_ptr == NULL)
154 1.1 joerg return -1;
155 1.1 joerg cdbw->data_ptr = new_data_ptr;
156 1.1 joerg
157 1.1 joerg new_data_len = realloc(cdbw->data_len,
158 1.1 joerg sizeof(*cdbw->data_len) * new_allocated);
159 1.1 joerg if (new_data_len == NULL)
160 1.1 joerg return -1;
161 1.1 joerg cdbw->data_len = new_data_len;
162 1.1 joerg
163 1.1 joerg cdbw->data_allocated = new_allocated;
164 1.1 joerg }
165 1.1 joerg
166 1.1 joerg cdbw->data_ptr[cdbw->data_counter] = malloc(datalen);
167 1.1 joerg if (cdbw->data_ptr[cdbw->data_counter] == NULL)
168 1.1 joerg return -1;
169 1.1 joerg memcpy(cdbw->data_ptr[cdbw->data_counter], data, datalen);
170 1.1 joerg cdbw->data_len[cdbw->data_counter] = datalen;
171 1.1 joerg cdbw->data_size += datalen;
172 1.3 joerg *idx = cdbw->data_counter++;
173 1.1 joerg return 0;
174 1.1 joerg }
175 1.1 joerg
176 1.1 joerg int
177 1.1 joerg cdbw_put_key(struct cdbw *cdbw, const void *key, size_t keylen, uint32_t idx)
178 1.1 joerg {
179 1.1 joerg uint32_t hashes[3];
180 1.1 joerg struct key_hash_head *head, *head2, *new_head;
181 1.1 joerg struct key_hash *key_hash;
182 1.1 joerg size_t new_hash_size, i;
183 1.1 joerg
184 1.1 joerg if (idx >= cdbw->data_counter ||
185 1.1 joerg cdbw->key_counter == max_data_counter)
186 1.1 joerg return -1;
187 1.1 joerg
188 1.1 joerg mi_vector_hash(key, keylen, 0, hashes);
189 1.1 joerg
190 1.1 joerg head = cdbw->hash + (hashes[0] & (cdbw->hash_size - 1));
191 1.1 joerg SLIST_FOREACH(key_hash, head, link) {
192 1.1 joerg if (key_hash->keylen != keylen)
193 1.1 joerg continue;
194 1.1 joerg if (key_hash->hashes[0] != hashes[0])
195 1.1 joerg continue;
196 1.1 joerg if (key_hash->hashes[1] != hashes[1])
197 1.1 joerg continue;
198 1.1 joerg if (key_hash->hashes[2] != hashes[2])
199 1.1 joerg continue;
200 1.1 joerg if (memcmp(key, key_hash->key, keylen))
201 1.1 joerg continue;
202 1.1 joerg return -1;
203 1.1 joerg }
204 1.1 joerg key_hash = malloc(sizeof(*key_hash));
205 1.1 joerg if (key_hash == NULL)
206 1.1 joerg return -1;
207 1.1 joerg key_hash->key = malloc(keylen);
208 1.1 joerg if (key_hash->key == NULL) {
209 1.1 joerg free(key_hash);
210 1.1 joerg return -1;
211 1.1 joerg }
212 1.1 joerg memcpy(key_hash->key, key, keylen);
213 1.1 joerg key_hash->hashes[0] = hashes[0];
214 1.1 joerg key_hash->hashes[1] = hashes[1];
215 1.1 joerg key_hash->hashes[2] = hashes[2];
216 1.1 joerg key_hash->keylen = keylen;
217 1.1 joerg key_hash->idx = idx;
218 1.1 joerg SLIST_INSERT_HEAD(head, key_hash, link);
219 1.1 joerg ++cdbw->key_counter;
220 1.1 joerg
221 1.1 joerg if (cdbw->key_counter <= cdbw->hash_size)
222 1.1 joerg return 0;
223 1.1 joerg
224 1.1 joerg /* Try to resize the hash table, but ignore errors. */
225 1.1 joerg new_hash_size = cdbw->hash_size * 2;
226 1.1 joerg new_head = calloc(sizeof(*new_head), new_hash_size);
227 1.1 joerg if (new_head == NULL)
228 1.1 joerg return 0;
229 1.1 joerg
230 1.1 joerg head = &cdbw->hash[hashes[0] & (cdbw->hash_size - 1)];
231 1.1 joerg for (i = 0; i < new_hash_size; ++i)
232 1.1 joerg SLIST_INIT(new_head + i);
233 1.1 joerg
234 1.1 joerg for (i = 0; i < cdbw->hash_size; ++i) {
235 1.1 joerg head = cdbw->hash + i;
236 1.1 joerg
237 1.1 joerg while ((key_hash = SLIST_FIRST(head)) != NULL) {
238 1.1 joerg SLIST_REMOVE_HEAD(head, link);
239 1.1 joerg head2 = new_head +
240 1.1 joerg (key_hash->hashes[0] & (new_hash_size - 1));
241 1.1 joerg SLIST_INSERT_HEAD(head2, key_hash, link);
242 1.1 joerg }
243 1.1 joerg }
244 1.1 joerg free(cdbw->hash);
245 1.1 joerg cdbw->hash_size = new_hash_size;
246 1.1 joerg cdbw->hash = new_head;
247 1.1 joerg
248 1.1 joerg return 0;
249 1.1 joerg }
250 1.1 joerg
251 1.1 joerg void
252 1.1 joerg cdbw_close(struct cdbw *cdbw)
253 1.1 joerg {
254 1.1 joerg struct key_hash_head *head;
255 1.1 joerg struct key_hash *key_hash;
256 1.1 joerg size_t i;
257 1.1 joerg
258 1.1 joerg for (i = 0; i < cdbw->hash_size; ++i) {
259 1.1 joerg head = cdbw->hash + i;
260 1.1 joerg while ((key_hash = SLIST_FIRST(head)) != NULL) {
261 1.1 joerg SLIST_REMOVE_HEAD(head, link);
262 1.1 joerg free(key_hash->key);
263 1.1 joerg free(key_hash);
264 1.1 joerg }
265 1.1 joerg }
266 1.1 joerg
267 1.1 joerg for (i = 0; i < cdbw->data_counter; ++i)
268 1.1 joerg free(cdbw->data_ptr[i]);
269 1.1 joerg free(cdbw->data_ptr);
270 1.1 joerg free(cdbw->data_len);
271 1.1 joerg free(cdbw->hash);
272 1.1 joerg free(cdbw);
273 1.1 joerg }
274 1.1 joerg
275 1.4 joerg uint32_t
276 1.4 joerg cdbw_stable_seeder(void)
277 1.4 joerg {
278 1.4 joerg return 0;
279 1.4 joerg }
280 1.4 joerg
281 1.1 joerg #define unused 0xffffffffU
282 1.1 joerg
283 1.1 joerg struct vertex {
284 1.1 joerg uint32_t l_edge, m_edge, r_edge;
285 1.1 joerg };
286 1.1 joerg
287 1.1 joerg struct edge {
288 1.1 joerg uint32_t idx;
289 1.1 joerg
290 1.1 joerg uint32_t left, middle, right;
291 1.1 joerg uint32_t l_prev, m_prev, l_next;
292 1.1 joerg uint32_t r_prev, m_next, r_next;
293 1.1 joerg };
294 1.1 joerg
295 1.1 joerg struct state {
296 1.1 joerg uint32_t data_entries;
297 1.1 joerg uint32_t entries;
298 1.1 joerg uint32_t keys;
299 1.1 joerg uint32_t seed;
300 1.1 joerg
301 1.1 joerg uint32_t *g;
302 1.1 joerg char *visited;
303 1.1 joerg
304 1.1 joerg struct vertex *verts;
305 1.1 joerg struct edge *edges;
306 1.1 joerg uint32_t output_index;
307 1.1 joerg uint32_t *output_order;
308 1.1 joerg };
309 1.1 joerg
310 1.1 joerg static void
311 1.1 joerg remove_vertex(struct state *state, struct vertex *v)
312 1.1 joerg {
313 1.1 joerg struct edge *e;
314 1.1 joerg struct vertex *vl, *vm, *vr;
315 1.1 joerg
316 1.1 joerg if (v->l_edge != unused && v->m_edge != unused)
317 1.1 joerg return;
318 1.1 joerg if (v->l_edge != unused && v->r_edge != unused)
319 1.1 joerg return;
320 1.1 joerg if (v->m_edge != unused && v->r_edge != unused)
321 1.1 joerg return;
322 1.1 joerg if (v->l_edge == unused && v->m_edge == unused && v->r_edge == unused)
323 1.1 joerg return;
324 1.1 joerg
325 1.1 joerg if (v->l_edge != unused) {
326 1.1 joerg e = &state->edges[v->l_edge];
327 1.1 joerg if (e->l_next != unused)
328 1.1 joerg return;
329 1.1 joerg } else if (v->m_edge != unused) {
330 1.1 joerg e = &state->edges[v->m_edge];
331 1.1 joerg if (e->m_next != unused)
332 1.1 joerg return;
333 1.1 joerg } else {
334 1.1 joerg if (v->r_edge == unused)
335 1.1 joerg abort();
336 1.1 joerg e = &state->edges[v->r_edge];
337 1.1 joerg if (e->r_next != unused)
338 1.1 joerg return;
339 1.1 joerg }
340 1.1 joerg
341 1.3 joerg state->output_order[--state->output_index] = e - state->edges;
342 1.1 joerg
343 1.1 joerg vl = &state->verts[e->left];
344 1.1 joerg vm = &state->verts[e->middle];
345 1.1 joerg vr = &state->verts[e->right];
346 1.1 joerg
347 1.1 joerg if (e->l_prev == unused)
348 1.1 joerg vl->l_edge = e->l_next;
349 1.1 joerg else
350 1.1 joerg state->edges[e->l_prev].l_next = e->l_next;
351 1.1 joerg if (e->l_next != unused)
352 1.1 joerg state->edges[e->l_next].l_prev = e->l_prev;
353 1.1 joerg
354 1.1 joerg if (e->m_prev == unused)
355 1.1 joerg vm->m_edge = e->m_next;
356 1.1 joerg else
357 1.1 joerg state->edges[e->m_prev].m_next = e->m_next;
358 1.1 joerg if (e->m_next != unused)
359 1.1 joerg state->edges[e->m_next].m_prev = e->m_prev;
360 1.1 joerg
361 1.1 joerg if (e->r_prev == unused)
362 1.1 joerg vr->r_edge = e->r_next;
363 1.1 joerg else
364 1.1 joerg state->edges[e->r_prev].r_next = e->r_next;
365 1.1 joerg if (e->r_next != unused)
366 1.1 joerg state->edges[e->r_next].r_prev = e->r_prev;
367 1.1 joerg }
368 1.1 joerg
369 1.1 joerg static int
370 1.1 joerg build_graph(struct cdbw *cdbw, struct state *state)
371 1.1 joerg {
372 1.1 joerg struct key_hash_head *head;
373 1.1 joerg struct key_hash *key_hash;
374 1.1 joerg struct vertex *v;
375 1.1 joerg struct edge *e;
376 1.3 joerg uint32_t hashes[3];
377 1.3 joerg size_t i;
378 1.1 joerg
379 1.1 joerg e = state->edges;
380 1.1 joerg for (i = 0; i < cdbw->hash_size; ++i) {
381 1.1 joerg head = &cdbw->hash[i];
382 1.1 joerg SLIST_FOREACH(key_hash, head, link) {
383 1.1 joerg e->idx = key_hash->idx;
384 1.1 joerg mi_vector_hash(key_hash->key, key_hash->keylen,
385 1.1 joerg state->seed, hashes);
386 1.1 joerg e->left = hashes[0] % state->entries;
387 1.1 joerg e->middle = hashes[1] % state->entries;
388 1.1 joerg e->right = hashes[2] % state->entries;
389 1.1 joerg
390 1.1 joerg ++e;
391 1.1 joerg }
392 1.1 joerg }
393 1.1 joerg
394 1.1 joerg for (i = 0; i < state->entries; ++i) {
395 1.1 joerg v = state->verts + i;
396 1.1 joerg v->l_edge = unused;
397 1.1 joerg v->m_edge = unused;
398 1.1 joerg v->r_edge = unused;
399 1.1 joerg }
400 1.1 joerg
401 1.1 joerg for (i = 0; i < state->keys; ++i) {
402 1.1 joerg e = state->edges + i;
403 1.1 joerg v = state->verts + e->left;
404 1.1 joerg if (v->l_edge != unused)
405 1.1 joerg state->edges[v->l_edge].l_prev = i;
406 1.1 joerg e->l_next = v->l_edge;
407 1.1 joerg e->l_prev = unused;
408 1.1 joerg v->l_edge = i;
409 1.1 joerg
410 1.1 joerg v = &state->verts[e->middle];
411 1.1 joerg if (v->m_edge != unused)
412 1.1 joerg state->edges[v->m_edge].m_prev = i;
413 1.1 joerg e->m_next = v->m_edge;
414 1.1 joerg e->m_prev = unused;
415 1.1 joerg v->m_edge = i;
416 1.1 joerg
417 1.1 joerg v = &state->verts[e->right];
418 1.1 joerg if (v->r_edge != unused)
419 1.1 joerg state->edges[v->r_edge].r_prev = i;
420 1.1 joerg e->r_next = v->r_edge;
421 1.1 joerg e->r_prev = unused;
422 1.1 joerg v->r_edge = i;
423 1.1 joerg }
424 1.1 joerg
425 1.1 joerg state->output_index = state->keys;
426 1.1 joerg for (i = 0; i < state->entries; ++i)
427 1.1 joerg remove_vertex(state, state->verts + i);
428 1.1 joerg
429 1.1 joerg i = state->keys;
430 1.1 joerg while (i > 0 && i > state->output_index) {
431 1.1 joerg --i;
432 1.1 joerg e = state->edges + state->output_order[i];
433 1.1 joerg remove_vertex(state, state->verts + e->left);
434 1.1 joerg remove_vertex(state, state->verts + e->middle);
435 1.1 joerg remove_vertex(state, state->verts + e->right);
436 1.1 joerg }
437 1.1 joerg
438 1.1 joerg return state->output_index == 0 ? 0 : -1;
439 1.1 joerg }
440 1.1 joerg
441 1.1 joerg static void
442 1.1 joerg assign_nodes(struct state *state)
443 1.1 joerg {
444 1.1 joerg struct edge *e;
445 1.1 joerg size_t i;
446 1.1 joerg
447 1.1 joerg for (i = 0; i < state->keys; ++i) {
448 1.1 joerg e = state->edges + state->output_order[i];
449 1.1 joerg
450 1.1 joerg if (!state->visited[e->left]) {
451 1.1 joerg state->g[e->left] =
452 1.1 joerg (2 * state->data_entries + e->idx
453 1.1 joerg - state->g[e->middle] - state->g[e->right])
454 1.1 joerg % state->data_entries;
455 1.1 joerg } else if (!state->visited[e->middle]) {
456 1.1 joerg state->g[e->middle] =
457 1.1 joerg (2 * state->data_entries + e->idx
458 1.1 joerg - state->g[e->left] - state->g[e->right])
459 1.1 joerg % state->data_entries;
460 1.1 joerg } else {
461 1.1 joerg state->g[e->right] =
462 1.1 joerg (2 * state->data_entries + e->idx
463 1.1 joerg - state->g[e->left] - state->g[e->middle])
464 1.1 joerg % state->data_entries;
465 1.1 joerg }
466 1.1 joerg state->visited[e->left] = 1;
467 1.1 joerg state->visited[e->middle] = 1;
468 1.1 joerg state->visited[e->right] = 1;
469 1.1 joerg }
470 1.1 joerg }
471 1.1 joerg
472 1.1 joerg static size_t
473 1.1 joerg compute_size(uint32_t size)
474 1.1 joerg {
475 1.1 joerg if (size < 0x100)
476 1.1 joerg return 1;
477 1.1 joerg else if (size < 0x10000)
478 1.1 joerg return 2;
479 1.1 joerg else
480 1.1 joerg return 4;
481 1.1 joerg }
482 1.1 joerg
483 1.1 joerg #define COND_FLUSH_BUFFER(n) do { \
484 1.1 joerg if (__predict_false(cur_pos + (n) >= sizeof(buf))) { \
485 1.1 joerg ret = write(fd, buf, cur_pos); \
486 1.1 joerg if (ret == -1 || (size_t)ret != cur_pos) \
487 1.1 joerg return -1; \
488 1.1 joerg cur_pos = 0; \
489 1.1 joerg } \
490 1.1 joerg } while (/* CONSTCOND */ 0)
491 1.1 joerg
492 1.1 joerg static int
493 1.1 joerg print_hash(struct cdbw *cdbw, struct state *state, int fd, const char *descr)
494 1.1 joerg {
495 1.1 joerg uint32_t data_size;
496 1.1 joerg uint8_t buf[90000];
497 1.1 joerg size_t i, size, size2, cur_pos;
498 1.1 joerg ssize_t ret;
499 1.1 joerg
500 1.1 joerg memcpy(buf, "NBCDB\n\0", 7);
501 1.1 joerg buf[7] = 1;
502 1.1 joerg strncpy((char *)buf + 8, descr, 16);
503 1.3 joerg le32enc(buf + 24, cdbw->data_size);
504 1.3 joerg le32enc(buf + 28, cdbw->data_counter);
505 1.1 joerg le32enc(buf + 32, state->entries);
506 1.1 joerg le32enc(buf + 36, state->seed);
507 1.1 joerg cur_pos = 40;
508 1.1 joerg
509 1.1 joerg size = compute_size(state->entries);
510 1.1 joerg for (i = 0; i < state->entries; ++i) {
511 1.1 joerg COND_FLUSH_BUFFER(4);
512 1.1 joerg le32enc(buf + cur_pos, state->g[i]);
513 1.1 joerg cur_pos += size;
514 1.1 joerg }
515 1.3 joerg size2 = compute_size(cdbw->data_size);
516 1.1 joerg size = size * state->entries % size2;
517 1.1 joerg if (size != 0) {
518 1.1 joerg size = size2 - size;
519 1.1 joerg COND_FLUSH_BUFFER(4);
520 1.1 joerg le32enc(buf + cur_pos, 0);
521 1.1 joerg cur_pos += size;
522 1.1 joerg }
523 1.1 joerg for (data_size = 0, i = 0; i < cdbw->data_counter; ++i) {
524 1.1 joerg COND_FLUSH_BUFFER(4);
525 1.1 joerg le32enc(buf + cur_pos, data_size);
526 1.1 joerg cur_pos += size2;
527 1.3 joerg data_size += cdbw->data_len[i];
528 1.1 joerg }
529 1.1 joerg COND_FLUSH_BUFFER(4);
530 1.1 joerg le32enc(buf + cur_pos, data_size);
531 1.1 joerg cur_pos += size2;
532 1.1 joerg
533 1.1 joerg for (i = 0; i < cdbw->data_counter; ++i) {
534 1.1 joerg COND_FLUSH_BUFFER(cdbw->data_len[i]);
535 1.1 joerg if (cdbw->data_len[i] < sizeof(buf)) {
536 1.1 joerg memcpy(buf + cur_pos, cdbw->data_ptr[i],
537 1.1 joerg cdbw->data_len[i]);
538 1.1 joerg cur_pos += cdbw->data_len[i];
539 1.1 joerg } else {
540 1.1 joerg ret = write(fd, cdbw->data_ptr[i], cdbw->data_len[i]);
541 1.1 joerg if (ret == -1 || (size_t)ret != cdbw->data_len[i])
542 1.1 joerg return -1;
543 1.1 joerg }
544 1.1 joerg }
545 1.1 joerg if (cur_pos != 0) {
546 1.1 joerg ret = write(fd, buf, cur_pos);
547 1.1 joerg if (ret == -1 || (size_t)ret != cur_pos)
548 1.1 joerg return -1;
549 1.1 joerg }
550 1.1 joerg return 0;
551 1.1 joerg }
552 1.1 joerg
553 1.1 joerg int
554 1.1 joerg cdbw_output(struct cdbw *cdbw, int fd, const char descr[16],
555 1.1 joerg uint32_t (*seedgen)(void))
556 1.1 joerg {
557 1.1 joerg struct state state;
558 1.1 joerg int rv;
559 1.1 joerg
560 1.1 joerg if (cdbw->data_counter == 0 || cdbw->key_counter == 0) {
561 1.1 joerg state.entries = 0;
562 1.1 joerg state.seed = 0;
563 1.1 joerg print_hash(cdbw, &state, fd, descr);
564 1.1 joerg return 0;
565 1.1 joerg }
566 1.1 joerg
567 1.4 joerg #if HAVE_NBTOOL_CONFIG_H
568 1.4 joerg if (seedgen == NULL)
569 1.4 joerg seedgen = cdbw_stable_seeder;
570 1.4 joerg #else
571 1.1 joerg if (seedgen == NULL)
572 1.1 joerg seedgen = arc4random;
573 1.4 joerg #endif
574 1.1 joerg
575 1.1 joerg rv = 0;
576 1.1 joerg
577 1.3 joerg state.keys = cdbw->key_counter;
578 1.3 joerg state.data_entries = cdbw->data_counter;
579 1.1 joerg state.entries = state.keys + (state.keys + 3) / 4;
580 1.1 joerg if (state.entries < 10)
581 1.1 joerg state.entries = 10;
582 1.1 joerg
583 1.1 joerg #define NALLOC(var, n) var = calloc(sizeof(*var), n)
584 1.1 joerg NALLOC(state.g, state.entries);
585 1.1 joerg NALLOC(state.visited, state.entries);
586 1.1 joerg NALLOC(state.verts, state.entries);
587 1.1 joerg NALLOC(state.edges, state.entries);
588 1.1 joerg NALLOC(state.output_order, state.keys);
589 1.1 joerg #undef NALLOC
590 1.1 joerg
591 1.1 joerg if (state.g == NULL || state.visited == NULL || state.verts == NULL ||
592 1.1 joerg state.edges == NULL || state.output_order == NULL) {
593 1.1 joerg rv = -1;
594 1.1 joerg goto release;
595 1.1 joerg }
596 1.1 joerg
597 1.4 joerg state.seed = 0;
598 1.1 joerg do {
599 1.4 joerg if (seedgen == cdbw_stable_seeder)
600 1.4 joerg ++state.seed;
601 1.4 joerg else
602 1.4 joerg state.seed = (*seedgen)();
603 1.1 joerg } while (build_graph(cdbw, &state));
604 1.1 joerg
605 1.1 joerg assign_nodes(&state);
606 1.1 joerg rv = print_hash(cdbw, &state, fd, descr);
607 1.1 joerg
608 1.1 joerg release:
609 1.1 joerg free(state.g);
610 1.1 joerg free(state.visited);
611 1.1 joerg free(state.verts);
612 1.1 joerg free(state.edges);
613 1.1 joerg free(state.output_order);
614 1.1 joerg
615 1.1 joerg return rv;
616 1.1 joerg }
617