Home | History | Annotate | Line # | Download | only in cdb
cdbw.c revision 1.2
      1  1.2  christos /*	$NetBSD: cdbw.c,v 1.2 2012/03/13 21:13:31 christos 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.2  christos __RCSID("$NetBSD: cdbw.c,v 1.2 2012/03/13 21:13:31 christos Exp $");
     40  1.1     joerg 
     41  1.1     joerg #include "namespace.h"
     42  1.1     joerg 
     43  1.1     joerg #include <sys/endian.h>
     44  1.1     joerg #include <sys/queue.h>
     45  1.2  christos #include <assert.h>
     46  1.1     joerg #include <cdbw.h>
     47  1.1     joerg #include <stdlib.h>
     48  1.1     joerg #include <string.h>
     49  1.1     joerg #include <unistd.h>
     50  1.1     joerg 
     51  1.1     joerg #ifdef __weak_alias
     52  1.1     joerg __weak_alias(cdbw_close,_cdbw_close)
     53  1.1     joerg __weak_alias(cdbw_open,_cdbw_open)
     54  1.1     joerg __weak_alias(cdbw_output,_cdbw_output)
     55  1.1     joerg __weak_alias(cdbw_put,_cdbw_put)
     56  1.1     joerg __weak_alias(cdbw_put_data,_cdbw_put_data)
     57  1.1     joerg __weak_alias(cdbw_put_key,_cdbw_put_key)
     58  1.1     joerg #endif
     59  1.1     joerg 
     60  1.1     joerg struct key_hash {
     61  1.1     joerg 	SLIST_ENTRY(key_hash) link;
     62  1.1     joerg 	uint32_t hashes[3];
     63  1.1     joerg 	uint32_t idx;
     64  1.1     joerg 	void *key;
     65  1.1     joerg 	size_t keylen;
     66  1.1     joerg };
     67  1.1     joerg 
     68  1.1     joerg SLIST_HEAD(key_hash_head, key_hash);
     69  1.1     joerg 
     70  1.1     joerg struct cdbw {
     71  1.1     joerg 	size_t data_counter;
     72  1.1     joerg 	size_t data_allocated;
     73  1.1     joerg 	size_t data_size;
     74  1.1     joerg 	size_t *data_len;
     75  1.1     joerg 	void **data_ptr;
     76  1.1     joerg 
     77  1.1     joerg 	size_t hash_size;
     78  1.1     joerg 	struct key_hash_head *hash;
     79  1.1     joerg 	size_t key_counter;
     80  1.1     joerg };
     81  1.1     joerg 
     82  1.1     joerg  /* Max. data counter that allows the index size to be 32bit. */
     83  1.1     joerg static const uint32_t max_data_counter = 0xccccccccU;
     84  1.1     joerg 
     85  1.1     joerg struct cdbw *
     86  1.1     joerg cdbw_open(void)
     87  1.1     joerg {
     88  1.1     joerg 	struct cdbw *cdbw;
     89  1.1     joerg 	size_t i;
     90  1.1     joerg 
     91  1.1     joerg 	cdbw = calloc(sizeof(*cdbw), 1);
     92  1.1     joerg 	if (cdbw == NULL)
     93  1.1     joerg 		return NULL;
     94  1.1     joerg 
     95  1.1     joerg 	cdbw->hash_size = 1024;
     96  1.1     joerg 	cdbw->hash = calloc(cdbw->hash_size, sizeof(*cdbw->hash));
     97  1.1     joerg 	if (cdbw->hash == NULL) {
     98  1.1     joerg 		free(cdbw);
     99  1.1     joerg 		return NULL;
    100  1.1     joerg 	}
    101  1.1     joerg 
    102  1.1     joerg 	for (i = 0; i < cdbw->hash_size; ++i)
    103  1.1     joerg 		SLIST_INIT(cdbw->hash + i);
    104  1.1     joerg 
    105  1.1     joerg 	return cdbw;
    106  1.1     joerg }
    107  1.1     joerg 
    108  1.1     joerg int
    109  1.1     joerg cdbw_put(struct cdbw *cdbw, const void *key, size_t keylen,
    110  1.1     joerg     const void *data, size_t datalen)
    111  1.1     joerg {
    112  1.1     joerg 	uint32_t idx;
    113  1.1     joerg 	int rv;
    114  1.1     joerg 
    115  1.1     joerg 	rv = cdbw_put_data(cdbw, data, datalen, &idx);
    116  1.1     joerg 	if (rv)
    117  1.1     joerg 		return rv;
    118  1.1     joerg 	rv = cdbw_put_key(cdbw, key, keylen, idx);
    119  1.1     joerg 	if (rv) {
    120  1.1     joerg 		--cdbw->data_counter;
    121  1.1     joerg 		free(cdbw->data_ptr[cdbw->data_counter]);
    122  1.1     joerg 		cdbw->data_size -= datalen;
    123  1.1     joerg 		return rv;
    124  1.1     joerg 	}
    125  1.1     joerg 	return 0;
    126  1.1     joerg }
    127  1.1     joerg 
    128  1.1     joerg int
    129  1.1     joerg cdbw_put_data(struct cdbw *cdbw, const void *data, size_t datalen,
    130  1.1     joerg     uint32_t *idx)
    131  1.1     joerg {
    132  1.1     joerg 
    133  1.1     joerg 	if (cdbw->data_counter == max_data_counter)
    134  1.1     joerg 		return -1;
    135  1.1     joerg 
    136  1.1     joerg 	if (cdbw->data_size + datalen < cdbw->data_size ||
    137  1.1     joerg 	    cdbw->data_size + datalen > 0xffffffffU)
    138  1.1     joerg 		return -1; /* Overflow */
    139  1.1     joerg 
    140  1.1     joerg 	if (cdbw->data_allocated == cdbw->data_counter) {
    141  1.1     joerg 		void **new_data_ptr;
    142  1.1     joerg 		size_t *new_data_len;
    143  1.1     joerg 		size_t new_allocated;
    144  1.1     joerg 
    145  1.1     joerg 		if (cdbw->data_allocated == 0)
    146  1.1     joerg 			new_allocated = 256;
    147  1.1     joerg 		else
    148  1.1     joerg 			new_allocated = cdbw->data_allocated * 2;
    149  1.1     joerg 
    150  1.1     joerg 		new_data_ptr = realloc(cdbw->data_ptr,
    151  1.1     joerg 		    sizeof(*cdbw->data_ptr) * new_allocated);
    152  1.1     joerg 		if (new_data_ptr == NULL)
    153  1.1     joerg 			return -1;
    154  1.1     joerg 		cdbw->data_ptr = new_data_ptr;
    155  1.1     joerg 
    156  1.1     joerg 		new_data_len = realloc(cdbw->data_len,
    157  1.1     joerg 		    sizeof(*cdbw->data_len) * new_allocated);
    158  1.1     joerg 		if (new_data_len == NULL)
    159  1.1     joerg 			return -1;
    160  1.1     joerg 		cdbw->data_len = new_data_len;
    161  1.1     joerg 
    162  1.1     joerg 		cdbw->data_allocated = new_allocated;
    163  1.1     joerg 	}
    164  1.1     joerg 
    165  1.1     joerg 	cdbw->data_ptr[cdbw->data_counter] = malloc(datalen);
    166  1.1     joerg 	if (cdbw->data_ptr[cdbw->data_counter] == NULL)
    167  1.1     joerg 		return -1;
    168  1.1     joerg 	memcpy(cdbw->data_ptr[cdbw->data_counter], data, datalen);
    169  1.1     joerg 	cdbw->data_len[cdbw->data_counter] = datalen;
    170  1.1     joerg 	cdbw->data_size += datalen;
    171  1.2  christos 	_DIAGASSERT(__type_fit(uint32_t, cdbw->data_counter));
    172  1.2  christos 	*idx = (uint32_t)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.1     joerg #define unused 0xffffffffU
    276  1.1     joerg 
    277  1.1     joerg struct vertex {
    278  1.1     joerg 	uint32_t l_edge, m_edge, r_edge;
    279  1.1     joerg };
    280  1.1     joerg 
    281  1.1     joerg struct edge {
    282  1.1     joerg 	uint32_t idx;
    283  1.1     joerg 
    284  1.1     joerg 	uint32_t left, middle, right;
    285  1.1     joerg 	uint32_t l_prev, m_prev, l_next;
    286  1.1     joerg 	uint32_t r_prev, m_next, r_next;
    287  1.1     joerg };
    288  1.1     joerg 
    289  1.1     joerg struct state {
    290  1.1     joerg 	uint32_t data_entries;
    291  1.1     joerg 	uint32_t entries;
    292  1.1     joerg 	uint32_t keys;
    293  1.1     joerg 	uint32_t seed;
    294  1.1     joerg 
    295  1.1     joerg 	uint32_t *g;
    296  1.1     joerg 	char *visited;
    297  1.1     joerg 
    298  1.1     joerg 	struct vertex *verts;
    299  1.1     joerg 	struct edge *edges;
    300  1.1     joerg 	uint32_t output_index;
    301  1.1     joerg 	uint32_t *output_order;
    302  1.1     joerg };
    303  1.1     joerg 
    304  1.1     joerg static void
    305  1.1     joerg remove_vertex(struct state *state, struct vertex *v)
    306  1.1     joerg {
    307  1.1     joerg 	struct edge *e;
    308  1.1     joerg 	struct vertex *vl, *vm, *vr;
    309  1.1     joerg 
    310  1.1     joerg 	if (v->l_edge != unused && v->m_edge != unused)
    311  1.1     joerg 		return;
    312  1.1     joerg 	if (v->l_edge != unused && v->r_edge != unused)
    313  1.1     joerg 		return;
    314  1.1     joerg 	if (v->m_edge != unused && v->r_edge != unused)
    315  1.1     joerg 		return;
    316  1.1     joerg 	if (v->l_edge == unused && v->m_edge == unused && v->r_edge == unused)
    317  1.1     joerg 		return;
    318  1.1     joerg 
    319  1.1     joerg 	if (v->l_edge != unused) {
    320  1.1     joerg 		e = &state->edges[v->l_edge];
    321  1.1     joerg 		if (e->l_next != unused)
    322  1.1     joerg 			return;
    323  1.1     joerg 	} else if (v->m_edge != unused) {
    324  1.1     joerg 		e = &state->edges[v->m_edge];
    325  1.1     joerg 		if (e->m_next != unused)
    326  1.1     joerg 			return;
    327  1.1     joerg 	} else {
    328  1.1     joerg 		if (v->r_edge == unused)
    329  1.1     joerg 			abort();
    330  1.1     joerg 		e = &state->edges[v->r_edge];
    331  1.1     joerg 		if (e->r_next != unused)
    332  1.1     joerg 			return;
    333  1.1     joerg 	}
    334  1.1     joerg 
    335  1.2  christos 	ptrdiff_t td = e - state->edges;
    336  1.2  christos 	_DIAGASSERT(__type_fit(uint32_t, td));
    337  1.2  christos 	state->output_order[--state->output_index] = (uint32_t)td;
    338  1.1     joerg 
    339  1.1     joerg 	vl = &state->verts[e->left];
    340  1.1     joerg 	vm = &state->verts[e->middle];
    341  1.1     joerg 	vr = &state->verts[e->right];
    342  1.1     joerg 
    343  1.1     joerg 	if (e->l_prev == unused)
    344  1.1     joerg 		vl->l_edge = e->l_next;
    345  1.1     joerg 	else
    346  1.1     joerg 		state->edges[e->l_prev].l_next = e->l_next;
    347  1.1     joerg 	if (e->l_next != unused)
    348  1.1     joerg 		state->edges[e->l_next].l_prev = e->l_prev;
    349  1.1     joerg 
    350  1.1     joerg 	if (e->m_prev == unused)
    351  1.1     joerg 		vm->m_edge = e->m_next;
    352  1.1     joerg 	else
    353  1.1     joerg 		state->edges[e->m_prev].m_next = e->m_next;
    354  1.1     joerg 	if (e->m_next != unused)
    355  1.1     joerg 		state->edges[e->m_next].m_prev = e->m_prev;
    356  1.1     joerg 
    357  1.1     joerg 	if (e->r_prev == unused)
    358  1.1     joerg 		vr->r_edge = e->r_next;
    359  1.1     joerg 	else
    360  1.1     joerg 		state->edges[e->r_prev].r_next = e->r_next;
    361  1.1     joerg 	if (e->r_next != unused)
    362  1.1     joerg 		state->edges[e->r_next].r_prev = e->r_prev;
    363  1.1     joerg }
    364  1.1     joerg 
    365  1.1     joerg static int
    366  1.1     joerg build_graph(struct cdbw *cdbw, struct state *state)
    367  1.1     joerg {
    368  1.1     joerg 	struct key_hash_head *head;
    369  1.1     joerg 	struct key_hash *key_hash;
    370  1.1     joerg 	struct vertex *v;
    371  1.1     joerg 	struct edge *e;
    372  1.2  christos 	uint32_t hashes[3], i;
    373  1.1     joerg 
    374  1.1     joerg 	e = state->edges;
    375  1.1     joerg 	for (i = 0; i < cdbw->hash_size; ++i) {
    376  1.1     joerg 		head = &cdbw->hash[i];
    377  1.1     joerg 		SLIST_FOREACH(key_hash, head, link) {
    378  1.1     joerg 			e->idx = key_hash->idx;
    379  1.1     joerg 			mi_vector_hash(key_hash->key, key_hash->keylen,
    380  1.1     joerg 			    state->seed, hashes);
    381  1.1     joerg 			e->left = hashes[0] % state->entries;
    382  1.1     joerg 			e->middle = hashes[1] % state->entries;
    383  1.1     joerg 			e->right = hashes[2] % state->entries;
    384  1.1     joerg 
    385  1.1     joerg 			++e;
    386  1.1     joerg 		}
    387  1.1     joerg 	}
    388  1.1     joerg 
    389  1.1     joerg 	for (i = 0; i < state->entries; ++i) {
    390  1.1     joerg 		v = state->verts + i;
    391  1.1     joerg 		v->l_edge = unused;
    392  1.1     joerg 		v->m_edge = unused;
    393  1.1     joerg 		v->r_edge = unused;
    394  1.1     joerg 	}
    395  1.1     joerg 
    396  1.1     joerg 	for (i = 0; i < state->keys; ++i) {
    397  1.1     joerg 		e = state->edges + i;
    398  1.1     joerg 		v = state->verts + e->left;
    399  1.1     joerg 		if (v->l_edge != unused)
    400  1.1     joerg 			state->edges[v->l_edge].l_prev = i;
    401  1.1     joerg 		e->l_next = v->l_edge;
    402  1.1     joerg 		e->l_prev = unused;
    403  1.1     joerg 		v->l_edge = i;
    404  1.1     joerg 
    405  1.1     joerg 		v = &state->verts[e->middle];
    406  1.1     joerg 		if (v->m_edge != unused)
    407  1.1     joerg 			state->edges[v->m_edge].m_prev = i;
    408  1.1     joerg 		e->m_next = v->m_edge;
    409  1.1     joerg 		e->m_prev = unused;
    410  1.1     joerg 		v->m_edge = i;
    411  1.1     joerg 
    412  1.1     joerg 		v = &state->verts[e->right];
    413  1.1     joerg 		if (v->r_edge != unused)
    414  1.1     joerg 			state->edges[v->r_edge].r_prev = i;
    415  1.1     joerg 		e->r_next = v->r_edge;
    416  1.1     joerg 		e->r_prev = unused;
    417  1.1     joerg 		v->r_edge = i;
    418  1.1     joerg 	}
    419  1.1     joerg 
    420  1.1     joerg 	state->output_index = state->keys;
    421  1.1     joerg 	for (i = 0; i < state->entries; ++i)
    422  1.1     joerg 		remove_vertex(state, state->verts + i);
    423  1.1     joerg 
    424  1.1     joerg 	i = state->keys;
    425  1.1     joerg 	while (i > 0 && i > state->output_index) {
    426  1.1     joerg 		--i;
    427  1.1     joerg 		e = state->edges + state->output_order[i];
    428  1.1     joerg 		remove_vertex(state, state->verts + e->left);
    429  1.1     joerg 		remove_vertex(state, state->verts + e->middle);
    430  1.1     joerg 		remove_vertex(state, state->verts + e->right);
    431  1.1     joerg 	}
    432  1.1     joerg 
    433  1.1     joerg 	return state->output_index == 0 ? 0 : -1;
    434  1.1     joerg }
    435  1.1     joerg 
    436  1.1     joerg static void
    437  1.1     joerg assign_nodes(struct state *state)
    438  1.1     joerg {
    439  1.1     joerg 	struct edge *e;
    440  1.1     joerg 	size_t i;
    441  1.1     joerg 
    442  1.1     joerg 	for (i = 0; i < state->keys; ++i) {
    443  1.1     joerg 		e = state->edges + state->output_order[i];
    444  1.1     joerg 
    445  1.1     joerg 		if (!state->visited[e->left]) {
    446  1.1     joerg 			state->g[e->left] =
    447  1.1     joerg 			    (2 * state->data_entries + e->idx
    448  1.1     joerg 			    - state->g[e->middle] - state->g[e->right])
    449  1.1     joerg 			    % state->data_entries;
    450  1.1     joerg 		} else if (!state->visited[e->middle]) {
    451  1.1     joerg 			state->g[e->middle] =
    452  1.1     joerg 			    (2 * state->data_entries + e->idx
    453  1.1     joerg 			    - state->g[e->left] - state->g[e->right])
    454  1.1     joerg 			    % state->data_entries;
    455  1.1     joerg 		} else {
    456  1.1     joerg 			state->g[e->right] =
    457  1.1     joerg 			    (2 * state->data_entries + e->idx
    458  1.1     joerg 			    - state->g[e->left] - state->g[e->middle])
    459  1.1     joerg 			    % state->data_entries;
    460  1.1     joerg 		}
    461  1.1     joerg 		state->visited[e->left] = 1;
    462  1.1     joerg 		state->visited[e->middle] = 1;
    463  1.1     joerg 		state->visited[e->right] = 1;
    464  1.1     joerg 	}
    465  1.1     joerg }
    466  1.1     joerg 
    467  1.1     joerg static size_t
    468  1.1     joerg compute_size(uint32_t size)
    469  1.1     joerg {
    470  1.1     joerg 	if (size < 0x100)
    471  1.1     joerg 		return 1;
    472  1.1     joerg 	else if (size < 0x10000)
    473  1.1     joerg 		return 2;
    474  1.1     joerg 	else
    475  1.1     joerg 		return 4;
    476  1.1     joerg }
    477  1.1     joerg 
    478  1.1     joerg #define COND_FLUSH_BUFFER(n) do { 				\
    479  1.1     joerg 	if (__predict_false(cur_pos + (n) >= sizeof(buf))) {	\
    480  1.1     joerg 		ret = write(fd, buf, cur_pos);			\
    481  1.1     joerg 		if (ret == -1 || (size_t)ret != cur_pos)	\
    482  1.1     joerg 			return -1;				\
    483  1.1     joerg 		cur_pos = 0;					\
    484  1.1     joerg 	}							\
    485  1.1     joerg } while (/* CONSTCOND */ 0)
    486  1.1     joerg 
    487  1.1     joerg static int
    488  1.1     joerg print_hash(struct cdbw *cdbw, struct state *state, int fd, const char *descr)
    489  1.1     joerg {
    490  1.1     joerg 	uint32_t data_size;
    491  1.1     joerg 	uint8_t buf[90000];
    492  1.1     joerg 	size_t i, size, size2, cur_pos;
    493  1.1     joerg 	ssize_t ret;
    494  1.1     joerg 
    495  1.1     joerg 	memcpy(buf, "NBCDB\n\0", 7);
    496  1.1     joerg 	buf[7] = 1;
    497  1.1     joerg 	strncpy((char *)buf + 8, descr, 16);
    498  1.2  christos 	_DIAGASSERT(__type_fit(uint32_t, cdbw->data_size));
    499  1.2  christos 	le32enc(buf + 24, (uint32_t)cdbw->data_size);
    500  1.2  christos 	_DIAGASSERT(__type_fit(uint32_t, cdbw->data_counter));
    501  1.2  christos 	le32enc(buf + 28, (uint32_t)cdbw->data_counter);
    502  1.1     joerg 	le32enc(buf + 32, state->entries);
    503  1.1     joerg 	le32enc(buf + 36, state->seed);
    504  1.1     joerg 	cur_pos = 40;
    505  1.1     joerg 
    506  1.1     joerg 	size = compute_size(state->entries);
    507  1.1     joerg 	for (i = 0; i < state->entries; ++i) {
    508  1.1     joerg 		COND_FLUSH_BUFFER(4);
    509  1.1     joerg 		le32enc(buf + cur_pos, state->g[i]);
    510  1.1     joerg 		cur_pos += size;
    511  1.1     joerg 	}
    512  1.2  christos 	_DIAGASSERT(__type_fit(uint32_t, cdbw->data_counter));
    513  1.2  christos 	size2 = compute_size((uint32_t)cdbw->data_size);
    514  1.1     joerg 	size = size * state->entries % size2;
    515  1.1     joerg 	if (size != 0) {
    516  1.1     joerg 		size = size2 - size;
    517  1.1     joerg 		COND_FLUSH_BUFFER(4);
    518  1.1     joerg 		le32enc(buf + cur_pos, 0);
    519  1.1     joerg 		cur_pos += size;
    520  1.1     joerg 	}
    521  1.1     joerg 	for (data_size = 0, i = 0; i < cdbw->data_counter; ++i) {
    522  1.1     joerg 		COND_FLUSH_BUFFER(4);
    523  1.1     joerg 		le32enc(buf + cur_pos, data_size);
    524  1.1     joerg 		cur_pos += size2;
    525  1.2  christos 		_DIAGASSERT(__type_fit(uint32_t,
    526  1.2  christos 		    data_size + cdbw->data_len[i]));
    527  1.2  christos 		data_size += (uint32_t)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.1     joerg 	if (seedgen == NULL)
    568  1.1     joerg 		seedgen = arc4random;
    569  1.1     joerg 
    570  1.1     joerg 	rv = 0;
    571  1.1     joerg 
    572  1.2  christos 	_DIAGASSERT(__type_fit(uint32_t, cdbw->key_counter));
    573  1.2  christos 	state.keys = (uint32_t)cdbw->key_counter;
    574  1.2  christos 	_DIAGASSERT(__type_fit(uint32_t, cdbw->key_counter));
    575  1.2  christos 	state.data_entries = (uint32_t)cdbw->data_counter;
    576  1.1     joerg 	state.entries = state.keys + (state.keys + 3) / 4;
    577  1.1     joerg 	if (state.entries < 10)
    578  1.1     joerg 		state.entries = 10;
    579  1.1     joerg 
    580  1.1     joerg #define	NALLOC(var, n)	var = calloc(sizeof(*var), n)
    581  1.1     joerg 	NALLOC(state.g, state.entries);
    582  1.1     joerg 	NALLOC(state.visited, state.entries);
    583  1.1     joerg 	NALLOC(state.verts, state.entries);
    584  1.1     joerg 	NALLOC(state.edges, state.entries);
    585  1.1     joerg 	NALLOC(state.output_order, state.keys);
    586  1.1     joerg #undef NALLOC
    587  1.1     joerg 
    588  1.1     joerg 	if (state.g == NULL || state.visited == NULL || state.verts == NULL ||
    589  1.1     joerg 	    state.edges == NULL || state.output_order == NULL) {
    590  1.1     joerg 		rv = -1;
    591  1.1     joerg 		goto release;
    592  1.1     joerg 	}
    593  1.1     joerg 
    594  1.1     joerg 	do {
    595  1.1     joerg 		state.seed = (*seedgen)();
    596  1.1     joerg 	} while (build_graph(cdbw, &state));
    597  1.1     joerg 
    598  1.1     joerg 	assign_nodes(&state);
    599  1.1     joerg 	rv = print_hash(cdbw, &state, fd, descr);
    600  1.1     joerg 
    601  1.1     joerg release:
    602  1.1     joerg 	free(state.g);
    603  1.1     joerg 	free(state.visited);
    604  1.1     joerg 	free(state.verts);
    605  1.1     joerg 	free(state.edges);
    606  1.1     joerg 	free(state.output_order);
    607  1.1     joerg 
    608  1.1     joerg 	return rv;
    609  1.1     joerg }
    610