Blame


1 eb77ee11 2018-07-08 stsp /*
2 eb77ee11 2018-07-08 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 eb77ee11 2018-07-08 stsp *
4 eb77ee11 2018-07-08 stsp * Permission to use, copy, modify, and distribute this software for any
5 eb77ee11 2018-07-08 stsp * purpose with or without fee is hereby granted, provided that the above
6 eb77ee11 2018-07-08 stsp * copyright notice and this permission notice appear in all copies.
7 eb77ee11 2018-07-08 stsp *
8 eb77ee11 2018-07-08 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 eb77ee11 2018-07-08 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 eb77ee11 2018-07-08 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 eb77ee11 2018-07-08 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 eb77ee11 2018-07-08 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 eb77ee11 2018-07-08 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 eb77ee11 2018-07-08 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 eb77ee11 2018-07-08 stsp */
16 eb77ee11 2018-07-08 stsp
17 eb77ee11 2018-07-08 stsp #include <sys/queue.h>
18 eb77ee11 2018-07-08 stsp
19 eb77ee11 2018-07-08 stsp #include <stdlib.h>
20 eb77ee11 2018-07-08 stsp #include <string.h>
21 eb77ee11 2018-07-08 stsp #include <sha1.h>
22 eb77ee11 2018-07-08 stsp #include <stdio.h>
23 eb77ee11 2018-07-08 stsp #include <zlib.h>
24 eb77ee11 2018-07-08 stsp #include <limits.h>
25 eb77ee11 2018-07-08 stsp #include <time.h>
26 eb77ee11 2018-07-08 stsp
27 eb77ee11 2018-07-08 stsp #include "got_object.h"
28 eb77ee11 2018-07-08 stsp #include "got_error.h"
29 eb77ee11 2018-07-08 stsp
30 eb77ee11 2018-07-08 stsp #include "got_lib_delta.h"
31 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
32 eb77ee11 2018-07-08 stsp #include "got_lib_object.h"
33 eb77ee11 2018-07-08 stsp #include "got_lib_object_idcache.h"
34 eb77ee11 2018-07-08 stsp
35 eb77ee11 2018-07-08 stsp #ifndef nitems
36 eb77ee11 2018-07-08 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
37 eb77ee11 2018-07-08 stsp #endif
38 eb77ee11 2018-07-08 stsp
39 eb77ee11 2018-07-08 stsp struct got_object_idcache_element {
40 eb77ee11 2018-07-08 stsp TAILQ_ENTRY(got_object_idcache_element) entry;
41 eb77ee11 2018-07-08 stsp struct got_object_id id;
42 eb77ee11 2018-07-08 stsp void *data; /* API user data */
43 eb77ee11 2018-07-08 stsp };
44 eb77ee11 2018-07-08 stsp
45 eb77ee11 2018-07-08 stsp TAILQ_HEAD(got_object_idcache_head, got_object_idcache_element);
46 eb77ee11 2018-07-08 stsp
47 eb77ee11 2018-07-08 stsp struct got_object_idcache {
48 eb77ee11 2018-07-08 stsp struct got_object_idcache_head entries;
49 eb77ee11 2018-07-08 stsp int nelem;
50 eb77ee11 2018-07-08 stsp int maxelem;
51 eb77ee11 2018-07-08 stsp };
52 eb77ee11 2018-07-08 stsp
53 eb77ee11 2018-07-08 stsp struct got_object_idcache *
54 eb77ee11 2018-07-08 stsp got_object_idcache_alloc(int maxelem)
55 eb77ee11 2018-07-08 stsp {
56 eb77ee11 2018-07-08 stsp struct got_object_idcache *cache;
57 eb77ee11 2018-07-08 stsp
58 eb77ee11 2018-07-08 stsp cache = calloc(1, sizeof(*cache));
59 eb77ee11 2018-07-08 stsp if (cache == NULL)
60 eb77ee11 2018-07-08 stsp return NULL;
61 eb77ee11 2018-07-08 stsp
62 eb77ee11 2018-07-08 stsp TAILQ_INIT(&cache->entries);
63 eb77ee11 2018-07-08 stsp cache->maxelem = maxelem;
64 eb77ee11 2018-07-08 stsp return cache;
65 eb77ee11 2018-07-08 stsp }
66 eb77ee11 2018-07-08 stsp
67 eb77ee11 2018-07-08 stsp void
68 eb77ee11 2018-07-08 stsp got_object_idcache_free(struct got_object_idcache *cache)
69 eb77ee11 2018-07-08 stsp {
70 eb77ee11 2018-07-08 stsp struct got_object_idcache_element *entry;
71 eb77ee11 2018-07-08 stsp
72 eb77ee11 2018-07-08 stsp while (!TAILQ_EMPTY(&cache->entries)) {
73 eb77ee11 2018-07-08 stsp entry = TAILQ_FIRST(&cache->entries);
74 eb77ee11 2018-07-08 stsp TAILQ_REMOVE(&cache->entries, entry, entry);
75 eb77ee11 2018-07-08 stsp /* User data should be freed by caller. */
76 eb77ee11 2018-07-08 stsp free(entry);
77 eb77ee11 2018-07-08 stsp }
78 eb77ee11 2018-07-08 stsp free(cache);
79 eb77ee11 2018-07-08 stsp }
80 eb77ee11 2018-07-08 stsp
81 eb77ee11 2018-07-08 stsp const struct got_error *
82 eb77ee11 2018-07-08 stsp got_object_idcache_add(struct got_object_idcache *cache,
83 eb77ee11 2018-07-08 stsp struct got_object_id *id, void *data)
84 eb77ee11 2018-07-08 stsp {
85 eb77ee11 2018-07-08 stsp struct got_object_idcache_element *entry;
86 eb77ee11 2018-07-08 stsp
87 720ad641 2018-07-23 stsp if (cache->nelem >= cache->maxelem)
88 720ad641 2018-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
89 eb77ee11 2018-07-08 stsp
90 eb77ee11 2018-07-08 stsp entry = calloc(1, sizeof(*entry));
91 eb77ee11 2018-07-08 stsp if (entry == NULL)
92 eb77ee11 2018-07-08 stsp return got_error_from_errno();
93 eb77ee11 2018-07-08 stsp
94 eb77ee11 2018-07-08 stsp memcpy(&entry->id, id, sizeof(entry->id));
95 eb77ee11 2018-07-08 stsp entry->data = data;
96 eb77ee11 2018-07-08 stsp
97 eb77ee11 2018-07-08 stsp TAILQ_INSERT_HEAD(&cache->entries, entry, entry);
98 eb77ee11 2018-07-08 stsp cache->nelem++;
99 eb77ee11 2018-07-08 stsp return NULL;
100 eb77ee11 2018-07-08 stsp }
101 eb77ee11 2018-07-08 stsp
102 eb77ee11 2018-07-08 stsp void *
103 eb77ee11 2018-07-08 stsp got_object_idcache_get(struct got_object_idcache *cache, struct got_object_id *id)
104 eb77ee11 2018-07-08 stsp {
105 eb77ee11 2018-07-08 stsp struct got_object_idcache_element *entry;
106 eb77ee11 2018-07-08 stsp
107 eb77ee11 2018-07-08 stsp TAILQ_FOREACH(entry, &cache->entries, entry) {
108 eb77ee11 2018-07-08 stsp if (got_object_id_cmp(&entry->id, id) != 0)
109 eb77ee11 2018-07-08 stsp continue;
110 eb77ee11 2018-07-08 stsp if (entry != TAILQ_FIRST(&cache->entries)) {
111 eb77ee11 2018-07-08 stsp TAILQ_REMOVE(&cache->entries, entry, entry);
112 eb77ee11 2018-07-08 stsp TAILQ_INSERT_HEAD(&cache->entries, entry, entry);
113 eb77ee11 2018-07-08 stsp }
114 eb77ee11 2018-07-08 stsp return entry->data;
115 eb77ee11 2018-07-08 stsp }
116 eb77ee11 2018-07-08 stsp
117 eb77ee11 2018-07-08 stsp return NULL;
118 eb77ee11 2018-07-08 stsp }
119 eb77ee11 2018-07-08 stsp
120 eb77ee11 2018-07-08 stsp const struct got_error *
121 eb77ee11 2018-07-08 stsp got_object_idcache_remove_least_used(void **data, struct got_object_idcache *cache)
122 eb77ee11 2018-07-08 stsp {
123 eb77ee11 2018-07-08 stsp struct got_object_idcache_element *entry;
124 eb77ee11 2018-07-08 stsp
125 eb77ee11 2018-07-08 stsp if (data)
126 eb77ee11 2018-07-08 stsp *data = NULL;
127 eb77ee11 2018-07-08 stsp
128 eb77ee11 2018-07-08 stsp if (cache->nelem == 0)
129 eb77ee11 2018-07-08 stsp return got_error(GOT_ERR_NO_OBJ);
130 eb77ee11 2018-07-08 stsp
131 eb77ee11 2018-07-08 stsp entry = TAILQ_LAST(&cache->entries, got_object_idcache_head);
132 eb77ee11 2018-07-08 stsp TAILQ_REMOVE(&cache->entries, entry, entry);
133 eb77ee11 2018-07-08 stsp if (data)
134 eb77ee11 2018-07-08 stsp *data = entry->data;
135 eb77ee11 2018-07-08 stsp free(entry);
136 eb77ee11 2018-07-08 stsp cache->nelem--;
137 eb77ee11 2018-07-08 stsp return NULL;
138 eb77ee11 2018-07-08 stsp }
139 eb77ee11 2018-07-08 stsp
140 eb77ee11 2018-07-08 stsp int
141 eb77ee11 2018-07-08 stsp got_object_idcache_contains(struct got_object_idcache *cache,
142 eb77ee11 2018-07-08 stsp struct got_object_id *id)
143 eb77ee11 2018-07-08 stsp {
144 eb77ee11 2018-07-08 stsp struct got_object_idcache_element *entry;
145 eb77ee11 2018-07-08 stsp
146 eb77ee11 2018-07-08 stsp TAILQ_FOREACH(entry, &cache->entries, entry) {
147 eb77ee11 2018-07-08 stsp if (got_object_id_cmp(&entry->id, id) == 0)
148 eb77ee11 2018-07-08 stsp return 1;
149 eb77ee11 2018-07-08 stsp }
150 eb77ee11 2018-07-08 stsp
151 eb77ee11 2018-07-08 stsp return 0;
152 eb77ee11 2018-07-08 stsp }
153 eb77ee11 2018-07-08 stsp
154 eb77ee11 2018-07-08 stsp void got_object_idcache_for_each(struct got_object_idcache *cache,
155 eb77ee11 2018-07-08 stsp void (*cb)(struct got_object_id *, void *, void *), void *arg)
156 eb77ee11 2018-07-08 stsp {
157 eb77ee11 2018-07-08 stsp struct got_object_idcache_element *entry;
158 eb77ee11 2018-07-08 stsp
159 eb77ee11 2018-07-08 stsp TAILQ_FOREACH(entry, &cache->entries, entry)
160 eb77ee11 2018-07-08 stsp cb(&entry->id, entry->data, arg);
161 eb77ee11 2018-07-08 stsp }
162 eb77ee11 2018-07-08 stsp
163 eb77ee11 2018-07-08 stsp int
164 eb77ee11 2018-07-08 stsp got_object_idcache_num_elements(struct got_object_idcache *set)
165 eb77ee11 2018-07-08 stsp {
166 eb77ee11 2018-07-08 stsp return set->nelem;
167 eb77ee11 2018-07-08 stsp }