/src/curl/lib/uint-table.c
Line | Count | Source |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. |
9 | | * |
10 | | * This software is licensed as described in the file COPYING, which |
11 | | * you should have received as part of this distribution. The terms |
12 | | * are also available at https://curl.se/docs/copyright.html. |
13 | | * |
14 | | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
15 | | * copies of the Software, and permit persons to whom the Software is |
16 | | * furnished to do so, under the terms of the COPYING file. |
17 | | * |
18 | | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
19 | | * KIND, either express or implied. |
20 | | * |
21 | | * SPDX-License-Identifier: curl |
22 | | * |
23 | | ***************************************************************************/ |
24 | | #include "curl_setup.h" |
25 | | |
26 | | #include "uint-table.h" |
27 | | |
28 | | #ifdef DEBUGBUILD |
29 | 15.4k | #define CURL_UINT32_TBL_MAGIC 0x62757473 |
30 | | #endif |
31 | | |
32 | | void Curl_uint32_tbl_init(struct uint32_tbl *tbl, |
33 | | Curl_uint32_tbl_entry_dtor *entry_dtor) |
34 | 15.4k | { |
35 | 15.4k | memset(tbl, 0, sizeof(*tbl)); |
36 | 15.4k | tbl->entry_dtor = entry_dtor; |
37 | 15.4k | tbl->last_key_added = UINT32_MAX; |
38 | 15.4k | #ifdef DEBUGBUILD |
39 | 15.4k | tbl->init = CURL_UINT32_TBL_MAGIC; |
40 | 15.4k | #endif |
41 | 15.4k | } |
42 | | |
43 | | static void uint32_tbl_clear_rows(struct uint32_tbl *tbl, |
44 | | uint32_t from, |
45 | | uint32_t upto_excluding) |
46 | 46.7k | { |
47 | 46.7k | uint32_t i, end; |
48 | | |
49 | 46.7k | end = CURLMIN(upto_excluding, tbl->nrows); |
50 | 2.05M | for(i = from; i < end; ++i) { |
51 | 2.01M | if(tbl->rows[i]) { |
52 | 31.2k | if(tbl->entry_dtor) |
53 | 0 | tbl->entry_dtor(i, tbl->rows[i]); |
54 | 31.2k | tbl->rows[i] = NULL; |
55 | 31.2k | tbl->nentries--; |
56 | 31.2k | } |
57 | 2.01M | } |
58 | 46.7k | } |
59 | | |
60 | | CURLcode Curl_uint32_tbl_resize(struct uint32_tbl *tbl, uint32_t nrows) |
61 | 15.4k | { |
62 | | /* we use `tbl->nrows + 1` during iteration, want that to work */ |
63 | 15.4k | DEBUGASSERT(tbl->init == CURL_UINT32_TBL_MAGIC); |
64 | 15.4k | if(!nrows) |
65 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
66 | 15.4k | if(nrows != tbl->nrows) { |
67 | 15.4k | void **rows = curlx_calloc(nrows, sizeof(void *)); |
68 | 15.4k | if(!rows) |
69 | 0 | return CURLE_OUT_OF_MEMORY; |
70 | 15.4k | if(tbl->rows) { |
71 | 0 | memcpy(rows, tbl->rows, (CURLMIN(nrows, tbl->nrows) * sizeof(void *))); |
72 | 0 | if(nrows < tbl->nrows) |
73 | 0 | uint32_tbl_clear_rows(tbl, nrows, tbl->nrows); |
74 | 0 | curlx_free(tbl->rows); |
75 | 0 | } |
76 | 15.4k | tbl->rows = rows; |
77 | 15.4k | tbl->nrows = nrows; |
78 | 15.4k | } |
79 | 15.4k | return CURLE_OK; |
80 | 15.4k | } |
81 | | |
82 | | /* Clear the table, making it empty. |
83 | | |
84 | | @unittest 3212 |
85 | | */ |
86 | | UNITTEST void uint32_tbl_clear(struct uint32_tbl *tbl); |
87 | | UNITTEST void uint32_tbl_clear(struct uint32_tbl *tbl) |
88 | 15.4k | { |
89 | 15.4k | DEBUGASSERT(tbl->init == CURL_UINT32_TBL_MAGIC); |
90 | 15.4k | uint32_tbl_clear_rows(tbl, 0, tbl->nrows); |
91 | 15.4k | DEBUGASSERT(!tbl->nentries); |
92 | 15.4k | tbl->last_key_added = UINT32_MAX; |
93 | 15.4k | } |
94 | | |
95 | | void Curl_uint32_tbl_destroy(struct uint32_tbl *tbl) |
96 | 15.4k | { |
97 | 15.4k | DEBUGASSERT(tbl->init == CURL_UINT32_TBL_MAGIC); |
98 | 15.4k | uint32_tbl_clear(tbl); |
99 | 15.4k | curlx_free(tbl->rows); |
100 | 15.4k | memset(tbl, 0, sizeof(*tbl)); |
101 | 15.4k | } |
102 | | |
103 | | uint32_t Curl_uint32_tbl_capacity(struct uint32_tbl *tbl) |
104 | 15.7k | { |
105 | 15.7k | return tbl->nrows; |
106 | 15.7k | } |
107 | | |
108 | | uint32_t Curl_uint32_tbl_count(struct uint32_tbl *tbl) |
109 | 32.6k | { |
110 | 32.6k | return tbl->nentries; |
111 | 32.6k | } |
112 | | |
113 | | void *Curl_uint32_tbl_get(struct uint32_tbl *tbl, uint32_t key) |
114 | 64.5k | { |
115 | 64.5k | return (key < tbl->nrows) ? tbl->rows[key] : NULL; |
116 | 64.5k | } |
117 | | |
118 | | bool Curl_uint32_tbl_add(struct uint32_tbl *tbl, void *entry, uint32_t *pkey) |
119 | 31.2k | { |
120 | 31.2k | uint32_t key, start_pos; |
121 | | |
122 | 31.2k | DEBUGASSERT(tbl->init == CURL_UINT32_TBL_MAGIC); |
123 | 31.2k | if(!entry || !pkey) |
124 | 0 | return FALSE; |
125 | 31.2k | *pkey = UINT32_MAX; |
126 | 31.2k | if(tbl->nentries == tbl->nrows) /* full */ |
127 | 0 | return FALSE; |
128 | | |
129 | 31.2k | start_pos = CURLMIN(tbl->last_key_added, tbl->nrows) + 1; |
130 | 31.2k | for(key = start_pos; key < tbl->nrows; ++key) { |
131 | 15.7k | if(!tbl->rows[key]) { |
132 | 15.7k | tbl->rows[key] = entry; |
133 | 15.7k | tbl->nentries++; |
134 | 15.7k | tbl->last_key_added = key; |
135 | 15.7k | *pkey = key; |
136 | 15.7k | return TRUE; |
137 | 15.7k | } |
138 | 15.7k | } |
139 | | /* no free entry at or above tbl->maybe_next_key, wrap around */ |
140 | 15.4k | for(key = 0; key < start_pos; ++key) { |
141 | 15.4k | if(!tbl->rows[key]) { |
142 | 15.4k | tbl->rows[key] = entry; |
143 | 15.4k | tbl->nentries++; |
144 | 15.4k | tbl->last_key_added = key; |
145 | 15.4k | *pkey = key; |
146 | 15.4k | return TRUE; |
147 | 15.4k | } |
148 | 15.4k | } |
149 | | /* Did not find any free row? Should not happen */ |
150 | 0 | DEBUGASSERT(0); |
151 | 0 | return FALSE; |
152 | 0 | } |
153 | | |
154 | | void Curl_uint32_tbl_remove(struct uint32_tbl *tbl, uint32_t key) |
155 | 31.2k | { |
156 | 31.2k | uint32_tbl_clear_rows(tbl, key, key + 1); |
157 | 31.2k | } |
158 | | |
159 | | bool Curl_uint32_tbl_contains(struct uint32_tbl *tbl, uint32_t key) |
160 | 15.7k | { |
161 | 15.7k | return (key < tbl->nrows) ? !!tbl->rows[key] : FALSE; |
162 | 15.7k | } |
163 | | |
164 | | static bool uint32_tbl_next_at(struct uint32_tbl *tbl, uint32_t key, |
165 | | uint32_t *pkey, void **pentry) |
166 | 30.9k | { |
167 | 1.99M | for(; key < tbl->nrows; ++key) { |
168 | 1.98M | if(tbl->rows[key]) { |
169 | 15.4k | *pkey = key; |
170 | 15.4k | *pentry = tbl->rows[key]; |
171 | 15.4k | return TRUE; |
172 | 15.4k | } |
173 | 1.98M | } |
174 | 15.4k | *pkey = UINT32_MAX; /* always invalid */ |
175 | 15.4k | *pentry = NULL; |
176 | 15.4k | return FALSE; |
177 | 30.9k | } |
178 | | |
179 | | bool Curl_uint32_tbl_first(struct uint32_tbl *tbl, |
180 | | uint32_t *pkey, void **pentry) |
181 | 15.4k | { |
182 | 15.4k | if(!pkey || !pentry) |
183 | 0 | return FALSE; |
184 | 15.4k | if(tbl->nentries && uint32_tbl_next_at(tbl, 0, pkey, pentry)) |
185 | 15.4k | return TRUE; |
186 | 0 | DEBUGASSERT(!tbl->nentries); |
187 | 0 | *pkey = UINT32_MAX; /* always invalid */ |
188 | 0 | *pentry = NULL; |
189 | 0 | return FALSE; |
190 | 0 | } |
191 | | |
192 | | bool Curl_uint32_tbl_next(struct uint32_tbl *tbl, uint32_t last_key, |
193 | | uint32_t *pkey, void **pentry) |
194 | 15.4k | { |
195 | 15.4k | if(!pkey || !pentry) |
196 | 0 | return FALSE; |
197 | 15.4k | if(uint32_tbl_next_at(tbl, last_key + 1, pkey, pentry)) |
198 | 0 | return TRUE; |
199 | 15.4k | *pkey = UINT32_MAX; /* always invalid */ |
200 | 15.4k | *pentry = NULL; |
201 | | return FALSE; |
202 | 15.4k | } |