2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 #include "got_compat.h"
27 #include "got_object.h"
28 #include "got_error.h"
30 #include "got_lib_delta.h"
31 #include "got_lib_inflate.h"
32 #include "got_lib_object.h"
33 #include "got_lib_object_idset.h"
35 struct got_object_idset_element {
36 RB_ENTRY(got_object_idset_element) entry;
37 struct got_object_id id;
38 void *data; /* API user data */
41 RB_HEAD(got_object_idset_tree, got_object_idset_element);
44 cmp_elements(const struct got_object_idset_element *e1,
45 const struct got_object_idset_element *e2)
47 return got_object_id_cmp(&e1->id, &e2->id);
50 RB_PROTOTYPE(got_object_idset_tree, got_object_idset_element, entry,
53 struct got_object_idset {
54 struct got_object_idset_tree entries;
56 #define GOT_OBJECT_IDSET_MAX_ELEM INT_MAX
59 struct got_object_idset *
60 got_object_idset_alloc(void)
62 struct got_object_idset *set;
64 set = malloc(sizeof(*set));
68 RB_INIT(&set->entries);
75 got_object_idset_free(struct got_object_idset *set)
77 struct got_object_idset_element *entry;
79 while ((entry = RB_MIN(got_object_idset_tree, &set->entries))) {
80 RB_REMOVE(got_object_idset_tree, &set->entries, entry);
81 /* User data should be freed by caller. */
88 const struct got_error *
89 got_object_idset_add(struct got_object_idset *set, struct got_object_id *id,
92 struct got_object_idset_element *new;
94 if (set->totelem >= GOT_OBJECT_IDSET_MAX_ELEM)
95 return got_error(GOT_ERR_NO_SPACE);
97 new = malloc(sizeof(*new));
99 return got_error_from_errno("malloc");
101 memcpy(&new->id, id, sizeof(new->id));
104 if (RB_INSERT(got_object_idset_tree, &set->entries, new) != NULL) {
106 return got_error(GOT_ERR_OBJ_EXISTS);
113 static struct got_object_idset_element *
114 find_element(struct got_object_idset *set, struct got_object_id *id)
116 struct got_object_idset_element *entry;
118 entry = RB_ROOT(&set->entries);
120 int cmp = got_object_id_cmp(id, &entry->id);
122 entry = RB_LEFT(entry, entry);
124 entry = RB_RIGHT(entry, entry);
133 got_object_idset_get(struct got_object_idset *set, struct got_object_id *id)
135 struct got_object_idset_element *entry = find_element(set, id);
136 return entry ? entry->data : NULL;
139 const struct got_error *
140 got_object_idset_remove(void **data, struct got_object_idset *set,
141 struct got_object_id *id)
143 struct got_object_idset_element *entry;
148 if (set->totelem == 0)
149 return got_error(GOT_ERR_NO_OBJ);
152 entry = RB_ROOT(&set->entries);
154 return got_error(GOT_ERR_NO_OBJ);
156 entry = find_element(set, id);
158 return got_error_no_obj(id);
161 RB_REMOVE(got_object_idset_tree, &set->entries, entry);
170 got_object_idset_contains(struct got_object_idset *set,
171 struct got_object_id *id)
173 struct got_object_idset_element *entry = find_element(set, id);
174 return entry ? 1 : 0;
177 const struct got_error *
178 got_object_idset_for_each(struct got_object_idset *set,
179 const struct got_error *(*cb)(struct got_object_id *, void *, void *),
182 const struct got_error *err;
183 struct got_object_idset_element *entry, *tmp;
185 RB_FOREACH_SAFE(entry, got_object_idset_tree, &set->entries, tmp) {
186 err = (*cb)(&entry->id, entry->data, arg);
194 got_object_idset_num_elements(struct got_object_idset *set)
199 struct got_object_idset_element *
200 got_object_idset_get_element(struct got_object_idset *set, struct got_object_id *id)
202 return find_element(set, id);
206 got_object_idset_get_element_data(struct got_object_idset_element *entry)
211 const struct got_error *
212 got_object_idset_for_each_element(struct got_object_idset *set,
213 const struct got_error *(*cb)(struct got_object_idset_element *, void *),
216 const struct got_error *err;
217 struct got_object_idset_element *entry, *tmp;
219 RB_FOREACH_SAFE(entry, got_object_idset_tree, &set->entries, tmp) {
220 err = (*cb)(entry, arg);
228 got_object_idset_remove_element(struct got_object_idset *set,
229 struct got_object_idset_element *entry)
231 RB_REMOVE(got_object_idset_tree, &set->entries, entry);
236 RB_GENERATE(got_object_idset_tree, got_object_idset_element, entry,