Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/queue.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <sha1.h>
22 #include <stdio.h>
23 #include <zlib.h>
24 #include <limits.h>
25 #include <time.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 #ifndef nitems
36 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
37 #endif
39 struct got_object_idset_element {
40 TAILQ_ENTRY(got_object_idset_element) entry;
41 struct got_object_id id;
42 void *data; /* API user data */
43 };
45 struct got_object_idset {
46 /*
47 * A set is implemented as a collection of 256 lists.
48 * The value of the first byte of an object ID determines
49 * which of these lists an object ID is stored in.
50 */
51 TAILQ_HEAD(, got_object_idset_element) entries[0xff + 1];
52 int nelem[0xff + 1];
53 int totelem;
54 #define GOT_OBJECT_IDSET_MAX_ELEM INT_MAX
55 };
57 struct got_object_idset *
58 got_object_idset_alloc(void)
59 {
60 struct got_object_idset *set;
61 int i;
63 set = calloc(1, sizeof(*set));
64 if (set == NULL)
65 return NULL;
67 for (i = 0; i < nitems(set->entries); i++)
68 TAILQ_INIT(&set->entries[i]);
70 return set;
71 }
73 void
74 got_object_idset_free(struct got_object_idset *set)
75 {
76 struct got_object_idset_element *entry;
77 int i;
79 for (i = 0; i < nitems(set->entries); i++) {
80 while (!TAILQ_EMPTY(&set->entries[i])) {
81 entry = TAILQ_FIRST(&set->entries[i]);
82 TAILQ_REMOVE(&set->entries[i], entry, entry);
83 /* User data should be freed by caller. */
84 free(entry);
85 }
86 }
87 free(set);
88 }
90 const struct got_error *
91 got_object_idset_add(struct got_object_idset *set, struct got_object_id *id,
92 void *data)
93 {
94 struct got_object_idset_element *new;
95 uint8_t i = id->sha1[0];
97 if (set->totelem >= GOT_OBJECT_IDSET_MAX_ELEM)
98 return got_error(GOT_ERR_NO_SPACE);
100 new = malloc(sizeof(*new));
101 if (new == NULL)
102 return got_error_from_errno();
104 memcpy(&new->id, id, sizeof(new->id));
105 new->data = data;
107 TAILQ_INSERT_HEAD(&set->entries[i], new, entry);
108 set->nelem[i]++;
109 set->totelem++;
110 return NULL;
113 void *
114 got_object_idset_get(struct got_object_idset *set, struct got_object_id *id)
116 struct got_object_idset_element *entry;
117 uint8_t i = id->sha1[0];
119 TAILQ_FOREACH(entry, &set->entries[i], entry) {
120 if (got_object_id_cmp(&entry->id, id) == 0)
121 return entry->data;
124 return NULL;
127 const struct got_error *
128 got_object_idset_remove(void **data, struct got_object_idset *set,
129 struct got_object_id *id)
131 struct got_object_idset_element *entry, *tmp;
132 uint8_t i = id->sha1[0];
134 if (data)
135 *data = NULL;
137 if (set->nelem[i] == 0)
138 return got_error(GOT_ERR_NO_OBJ);
140 TAILQ_FOREACH_SAFE(entry, &set->entries[i], entry, tmp) {
141 if (got_object_id_cmp(&entry->id, id) == 0) {
142 TAILQ_REMOVE(&set->entries[i], entry, entry);
143 if (data)
144 *data = entry->data;
145 free(entry);
146 set->nelem[i]--;
147 set->totelem--;
148 return NULL;
152 return got_error(GOT_ERR_NO_OBJ);
155 int
156 got_object_idset_contains(struct got_object_idset *set,
157 struct got_object_id *id)
159 struct got_object_idset_element *entry;
160 uint8_t i = id->sha1[0];
162 TAILQ_FOREACH(entry, &set->entries[i], entry) {
163 if (got_object_id_cmp(&entry->id, id) == 0)
164 return 1;
167 return 0;
170 void got_object_idset_for_each(struct got_object_idset *set,
171 void (*cb)(struct got_object_id *, void *, void *), void *arg)
173 struct got_object_idset_element *entry;
174 int i;
176 for (i = 0; i < nitems(set->entries); i++) {
177 TAILQ_FOREACH(entry, &set->entries[i], entry)
178 cb(&entry->id, entry->data, arg);
182 int
183 got_object_idset_num_elements(struct got_object_idset *set)
185 return set->totelem;