2 * Copyright (C) 2011 Felix Fietkau <nbd@openwrt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2.1
6 * as published by the Free Software Foundation
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
18 #include <libubox/avl-cmp.h>
23 static int random_fd
= -1;
25 static int ubus_cmp_id(const void *k1
, const void *k2
, void *ptr
)
27 const uint32_t *id1
= k1
, *id2
= k2
;
35 void ubus_init_string_tree(struct avl_tree
*tree
, bool dup
)
37 avl_init(tree
, avl_strcmp
, dup
, NULL
);
40 void ubus_init_id_tree(struct avl_tree
*tree
)
43 random_fd
= open("/dev/urandom", O_RDONLY
);
50 avl_init(tree
, ubus_cmp_id
, false, NULL
);
53 bool ubus_alloc_id(struct avl_tree
*tree
, struct ubus_id
*id
, uint32_t val
)
55 id
->avl
.key
= &id
->id
;
58 return avl_insert(tree
, &id
->avl
) == 0;
62 if (read(random_fd
, &id
->id
, sizeof(id
->id
)) != sizeof(id
->id
))
65 if (id
->id
< UBUS_SYSTEM_OBJECT_MAX
)
67 } while (avl_insert(tree
, &id
->avl
) != 0);