/src/CMake/Utilities/cmcurl/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 | | #define CURL_UINT32_TBL_MAGIC 0x62757473 |
30 | | #endif |
31 | | |
32 | | /* Clear the table, making it empty. */ |
33 | | UNITTEST void Curl_uint32_tbl_clear(struct uint32_tbl *tbl); |
34 | | |
35 | | void Curl_uint32_tbl_init(struct uint32_tbl *tbl, |
36 | | Curl_uint32_tbl_entry_dtor *entry_dtor) |
37 | 0 | { |
38 | 0 | memset(tbl, 0, sizeof(*tbl)); |
39 | 0 | tbl->entry_dtor = entry_dtor; |
40 | 0 | tbl->last_key_added = UINT32_MAX; |
41 | | #ifdef DEBUGBUILD |
42 | | tbl->init = CURL_UINT32_TBL_MAGIC; |
43 | | #endif |
44 | 0 | } |
45 | | |
46 | | static void uint32_tbl_clear_rows(struct uint32_tbl *tbl, |
47 | | uint32_t from, |
48 | | uint32_t upto_excluding) |
49 | 0 | { |
50 | 0 | uint32_t i, end; |
51 | |
|
52 | 0 | end = CURLMIN(upto_excluding, tbl->nrows); |
53 | 0 | for(i = from; i < end; ++i) { |
54 | 0 | if(tbl->rows[i]) { |
55 | 0 | if(tbl->entry_dtor) |
56 | 0 | tbl->entry_dtor(i, tbl->rows[i]); |
57 | 0 | tbl->rows[i] = NULL; |
58 | 0 | tbl->nentries--; |
59 | 0 | } |
60 | 0 | } |
61 | 0 | } |
62 | | |
63 | | CURLcode Curl_uint32_tbl_resize(struct uint32_tbl *tbl, uint32_t nrows) |
64 | 0 | { |
65 | | /* we use `tbl->nrows + 1` during iteration, want that to work */ |
66 | 0 | DEBUGASSERT(tbl->init == CURL_UINT32_TBL_MAGIC); |
67 | 0 | if(!nrows) |
68 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
69 | 0 | if(nrows != tbl->nrows) { |
70 | 0 | void **rows = curlx_calloc(nrows, sizeof(void *)); |
71 | 0 | if(!rows) |
72 | 0 | return CURLE_OUT_OF_MEMORY; |
73 | 0 | if(tbl->rows) { |
74 | 0 | memcpy(rows, tbl->rows, (CURLMIN(nrows, tbl->nrows) * sizeof(void *))); |
75 | 0 | if(nrows < tbl->nrows) |
76 | 0 | uint32_tbl_clear_rows(tbl, nrows, tbl->nrows); |
77 | 0 | curlx_free(tbl->rows); |
78 | 0 | } |
79 | 0 | tbl->rows = rows; |
80 | 0 | tbl->nrows = nrows; |
81 | 0 | } |
82 | 0 | return CURLE_OK; |
83 | 0 | } |
84 | | |
85 | | void Curl_uint32_tbl_destroy(struct uint32_tbl *tbl) |
86 | 0 | { |
87 | 0 | DEBUGASSERT(tbl->init == CURL_UINT32_TBL_MAGIC); |
88 | 0 | Curl_uint32_tbl_clear(tbl); |
89 | 0 | curlx_free(tbl->rows); |
90 | 0 | memset(tbl, 0, sizeof(*tbl)); |
91 | 0 | } |
92 | | |
93 | | UNITTEST void Curl_uint32_tbl_clear(struct uint32_tbl *tbl) |
94 | 0 | { |
95 | 0 | DEBUGASSERT(tbl->init == CURL_UINT32_TBL_MAGIC); |
96 | 0 | uint32_tbl_clear_rows(tbl, 0, tbl->nrows); |
97 | 0 | DEBUGASSERT(!tbl->nentries); |
98 | 0 | tbl->last_key_added = UINT32_MAX; |
99 | 0 | } |
100 | | |
101 | | uint32_t Curl_uint32_tbl_capacity(struct uint32_tbl *tbl) |
102 | 0 | { |
103 | 0 | return tbl->nrows; |
104 | 0 | } |
105 | | |
106 | | uint32_t Curl_uint32_tbl_count(struct uint32_tbl *tbl) |
107 | 0 | { |
108 | 0 | return tbl->nentries; |
109 | 0 | } |
110 | | |
111 | | void *Curl_uint32_tbl_get(struct uint32_tbl *tbl, uint32_t key) |
112 | 0 | { |
113 | 0 | return (key < tbl->nrows) ? tbl->rows[key] : NULL; |
114 | 0 | } |
115 | | |
116 | | bool Curl_uint32_tbl_add(struct uint32_tbl *tbl, void *entry, uint32_t *pkey) |
117 | 0 | { |
118 | 0 | uint32_t key, start_pos; |
119 | |
|
120 | 0 | DEBUGASSERT(tbl->init == CURL_UINT32_TBL_MAGIC); |
121 | 0 | if(!entry || !pkey) |
122 | 0 | return FALSE; |
123 | 0 | *pkey = UINT32_MAX; |
124 | 0 | if(tbl->nentries == tbl->nrows) /* full */ |
125 | 0 | return FALSE; |
126 | | |
127 | 0 | start_pos = CURLMIN(tbl->last_key_added, tbl->nrows) + 1; |
128 | 0 | for(key = start_pos; key < tbl->nrows; ++key) { |
129 | 0 | if(!tbl->rows[key]) { |
130 | 0 | tbl->rows[key] = entry; |
131 | 0 | tbl->nentries++; |
132 | 0 | tbl->last_key_added = key; |
133 | 0 | *pkey = key; |
134 | 0 | return TRUE; |
135 | 0 | } |
136 | 0 | } |
137 | | /* no free entry at or above tbl->maybe_next_key, wrap around */ |
138 | 0 | for(key = 0; key < start_pos; ++key) { |
139 | 0 | if(!tbl->rows[key]) { |
140 | 0 | tbl->rows[key] = entry; |
141 | 0 | tbl->nentries++; |
142 | 0 | tbl->last_key_added = key; |
143 | 0 | *pkey = key; |
144 | 0 | return TRUE; |
145 | 0 | } |
146 | 0 | } |
147 | | /* Did not find any free row? Should not happen */ |
148 | 0 | DEBUGASSERT(0); |
149 | 0 | return FALSE; |
150 | 0 | } |
151 | | |
152 | | void Curl_uint32_tbl_remove(struct uint32_tbl *tbl, uint32_t key) |
153 | 0 | { |
154 | 0 | uint32_tbl_clear_rows(tbl, key, key + 1); |
155 | 0 | } |
156 | | |
157 | | bool Curl_uint32_tbl_contains(struct uint32_tbl *tbl, uint32_t key) |
158 | 0 | { |
159 | 0 | return (key < tbl->nrows) ? !!tbl->rows[key] : FALSE; |
160 | 0 | } |
161 | | |
162 | | static bool uint32_tbl_next_at(struct uint32_tbl *tbl, uint32_t key, |
163 | | uint32_t *pkey, void **pentry) |
164 | 0 | { |
165 | 0 | for(; key < tbl->nrows; ++key) { |
166 | 0 | if(tbl->rows[key]) { |
167 | 0 | *pkey = key; |
168 | 0 | *pentry = tbl->rows[key]; |
169 | 0 | return TRUE; |
170 | 0 | } |
171 | 0 | } |
172 | 0 | *pkey = UINT32_MAX; /* always invalid */ |
173 | 0 | *pentry = NULL; |
174 | 0 | return FALSE; |
175 | 0 | } |
176 | | |
177 | | bool Curl_uint32_tbl_first(struct uint32_tbl *tbl, |
178 | | uint32_t *pkey, void **pentry) |
179 | 0 | { |
180 | 0 | if(!pkey || !pentry) |
181 | 0 | return FALSE; |
182 | 0 | if(tbl->nentries && uint32_tbl_next_at(tbl, 0, pkey, pentry)) |
183 | 0 | return TRUE; |
184 | 0 | DEBUGASSERT(!tbl->nentries); |
185 | 0 | *pkey = UINT32_MAX; /* always invalid */ |
186 | 0 | *pentry = NULL; |
187 | 0 | return FALSE; |
188 | 0 | } |
189 | | |
190 | | bool Curl_uint32_tbl_next(struct uint32_tbl *tbl, uint32_t last_key, |
191 | | uint32_t *pkey, void **pentry) |
192 | 0 | { |
193 | 0 | if(!pkey || !pentry) |
194 | 0 | return FALSE; |
195 | 0 | if(uint32_tbl_next_at(tbl, last_key + 1, pkey, pentry)) |
196 | 0 | return TRUE; |
197 | 0 | *pkey = UINT32_MAX; /* always invalid */ |
198 | 0 | *pentry = NULL; |
199 | | return FALSE; |
200 | 0 | } |