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.
22 static int random_fd
= -1;
24 static int ubus_cmp_id(const void *k1
, const void *k2
, void *ptr
)
26 const uint32_t *id1
= k1
, *id2
= k2
;
34 static int ubus_cmp_str(const void *k1
, const void *k2
, void *ptr
)
36 return strcmp(k1
, k2
);
39 void ubus_init_string_tree(struct avl_tree
*tree
, bool dup
)
41 avl_init(tree
, ubus_cmp_str
, dup
, NULL
);
44 void ubus_init_id_tree(struct avl_tree
*tree
)
47 random_fd
= open("/dev/urandom", O_RDONLY
);
54 avl_init(tree
, ubus_cmp_id
, false, NULL
);
57 bool ubus_alloc_id(struct avl_tree
*tree
, struct ubus_id
*id
, uint32_t val
)
59 id
->avl
.key
= &id
->id
;
62 return avl_insert(tree
, &id
->avl
) == 0;
66 if (read(random_fd
, &id
->id
, sizeof(id
->id
)) != sizeof(id
->id
))
69 if (id
->id
< UBUS_SYSTEM_OBJECT_MAX
)
71 } while (avl_insert(tree
, &id
->avl
) != 0);