1*77cffba6Ssyl /* $OpenBSD: tree.c,v 1.2 2014/02/05 20:13:58 syl Exp $ */
275af46c2Stedu
375af46c2Stedu /*
475af46c2Stedu * Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
575af46c2Stedu *
675af46c2Stedu * Permission to use, copy, modify, and distribute this software for any
775af46c2Stedu * purpose with or without fee is hereby granted, provided that the above
875af46c2Stedu * copyright notice and this permission notice appear in all copies.
975af46c2Stedu *
1075af46c2Stedu * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1175af46c2Stedu * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1275af46c2Stedu * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1375af46c2Stedu * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1475af46c2Stedu * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1575af46c2Stedu * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1675af46c2Stedu * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1775af46c2Stedu */
1875af46c2Stedu
1975af46c2Stedu #include <stdlib.h>
20*77cffba6Ssyl #include <errno.h>
2175af46c2Stedu
2275af46c2Stedu #include "fuse_private.h"
2375af46c2Stedu
2475af46c2Stedu struct treeentry {
2575af46c2Stedu SPLAY_ENTRY(treeentry) entry;
2675af46c2Stedu uint64_t id;
2775af46c2Stedu void *data;
2875af46c2Stedu };
2975af46c2Stedu
3075af46c2Stedu static int treeentry_cmp(struct treeentry *, struct treeentry *);
3175af46c2Stedu
3275af46c2Stedu SPLAY_PROTOTYPE(tree, treeentry, entry, treeentry_cmp);
3375af46c2Stedu
3475af46c2Stedu int
tree_check(struct tree * t,uint64_t id)3575af46c2Stedu tree_check(struct tree *t, uint64_t id)
3675af46c2Stedu {
3775af46c2Stedu struct treeentry key;
3875af46c2Stedu
3975af46c2Stedu key.id = id;
4075af46c2Stedu return (SPLAY_FIND(tree, t, &key) != NULL);
4175af46c2Stedu }
4275af46c2Stedu
4375af46c2Stedu void *
tree_set(struct tree * t,uint64_t id,void * data)4475af46c2Stedu tree_set(struct tree *t, uint64_t id, void *data)
4575af46c2Stedu {
4675af46c2Stedu struct treeentry *entry, key;
4775af46c2Stedu
4875af46c2Stedu key.id = id;
4975af46c2Stedu if ((entry = SPLAY_FIND(tree, t, &key)) == NULL) {
5075af46c2Stedu entry = malloc(sizeof *entry);
5175af46c2Stedu if (entry == NULL)
5275af46c2Stedu return (NULL);
5375af46c2Stedu entry->id = id;
5475af46c2Stedu SPLAY_INSERT(tree, t, entry);
5575af46c2Stedu }
5675af46c2Stedu
5775af46c2Stedu entry->data = data;
5875af46c2Stedu
5975af46c2Stedu return (entry);
6075af46c2Stedu }
6175af46c2Stedu
6275af46c2Stedu void *
tree_get(struct tree * t,uint64_t id)6375af46c2Stedu tree_get(struct tree *t, uint64_t id)
6475af46c2Stedu {
6575af46c2Stedu struct treeentry key, *entry;
6675af46c2Stedu
6775af46c2Stedu key.id = id;
68*77cffba6Ssyl if ((entry = SPLAY_FIND(tree, t, &key)) == NULL) {
69*77cffba6Ssyl errno = ENOENT;
7075af46c2Stedu return (NULL);
71*77cffba6Ssyl }
7275af46c2Stedu
7375af46c2Stedu return (entry->data);
7475af46c2Stedu }
7575af46c2Stedu
7675af46c2Stedu void *
tree_pop(struct tree * t,uint64_t id)7775af46c2Stedu tree_pop(struct tree *t, uint64_t id)
7875af46c2Stedu {
7975af46c2Stedu struct treeentry key, *entry;
8075af46c2Stedu void *data;
8175af46c2Stedu
8275af46c2Stedu key.id = id;
8375af46c2Stedu if ((entry = SPLAY_FIND(tree, t, &key)) == NULL)
8475af46c2Stedu return (NULL);
8575af46c2Stedu
8675af46c2Stedu data = entry->data;
8775af46c2Stedu SPLAY_REMOVE(tree, t, entry);
8875af46c2Stedu free(entry);
8975af46c2Stedu
9075af46c2Stedu return (data);
9175af46c2Stedu }
9275af46c2Stedu
9375af46c2Stedu static int
treeentry_cmp(struct treeentry * a,struct treeentry * b)9475af46c2Stedu treeentry_cmp(struct treeentry *a, struct treeentry *b)
9575af46c2Stedu {
9675af46c2Stedu if (a->id < b->id)
9775af46c2Stedu return (-1);
9875af46c2Stedu if (a->id > b->id)
9975af46c2Stedu return (1);
10075af46c2Stedu return (0);
10175af46c2Stedu }
10275af46c2Stedu
10375af46c2Stedu SPLAY_GENERATE(tree, treeentry, entry, treeentry_cmp);
104