X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fubus.git;a=blobdiff_plain;f=ubusd_id.c;h=8d9fede2dd10296db2d8abc189bc3075fb216150;hp=9443db9297941b50e80247b905bbf791948c626b;hb=a69f062cbd4041229f8d29ef9647bf783df414c1;hpb=dbd4c2f121be08e514828f2533687b145a6e16dd diff --git a/ubusd_id.c b/ubusd_id.c index 9443db9..8d9fede 100644 --- a/ubusd_id.c +++ b/ubusd_id.c @@ -1,8 +1,23 @@ +/* + * Copyright (C) 2011 Felix Fietkau + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 2.1 + * as published by the Free Software Foundation + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + #include #include #include #include +#include +#include "ubusmsg.h" #include "ubusd_id.h" static int random_fd = -1; @@ -17,6 +32,11 @@ static int ubus_cmp_id(const void *k1, const void *k2, void *ptr) return *id1 > *id2; } +void ubus_init_string_tree(struct avl_tree *tree, bool dup) +{ + avl_init(tree, avl_strcmp, dup, NULL); +} + void ubus_init_id_tree(struct avl_tree *tree) { if (random_fd < 0) { @@ -30,14 +50,19 @@ void ubus_init_id_tree(struct avl_tree *tree) avl_init(tree, ubus_cmp_id, false, NULL); } -bool ubus_alloc_id(struct avl_tree *tree, struct ubus_id *id) +bool ubus_alloc_id(struct avl_tree *tree, struct ubus_id *id, uint32_t val) { id->avl.key = &id->id; + if (val) { + id->id = val; + return avl_insert(tree, &id->avl) == 0; + } + do { if (read(random_fd, &id->id, sizeof(id->id)) != sizeof(id->id)) return false; - if (!id->id) + if (id->id < UBUS_SYSTEM_OBJECT_MAX) continue; } while (avl_insert(tree, &id->avl) != 0);