/src/gdal/curl/lib/ptrarray.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 "ptrarray.h" |
27 | | |
28 | 0 | #define CURL_PTRARRAY_MAX_GROW (16 * 1024) |
29 | | |
30 | | void Curl_ptrarray_init(struct ptrarray *pa) |
31 | 189k | { |
32 | 189k | pa->data = NULL; |
33 | 189k | pa->n = pa->capacity = 0; |
34 | 189k | } |
35 | | |
36 | | /* @unittest 3211 */ |
37 | | |
38 | | UNITTEST void ptrarray_clear(struct ptrarray *pa); |
39 | | UNITTEST void ptrarray_clear(struct ptrarray *pa) |
40 | 5.60k | { |
41 | 5.60k | curlx_safefree(pa->data); |
42 | 5.60k | pa->n = pa->capacity = 0; |
43 | 5.60k | } |
44 | | |
45 | | #ifdef UNITTESTS |
46 | | /* @unittest 3211 */ |
47 | | UNITTEST uint32_t ptrarray_count(struct ptrarray *pa); |
48 | | UNITTEST uint32_t ptrarray_count(struct ptrarray *pa) |
49 | | { |
50 | | return pa->n; |
51 | | } |
52 | | |
53 | | /* @unittest 3211 */ |
54 | | UNITTEST void *ptrarray_get(struct ptrarray *pa, uint32_t idx); |
55 | | UNITTEST void *ptrarray_get(struct ptrarray *pa, uint32_t idx) |
56 | | { |
57 | | return (idx < pa->n) ? pa->data[idx] : NULL; |
58 | | } |
59 | | #endif |
60 | | |
61 | | CURLcode Curl_ptrarray_add(struct ptrarray *pa, void *ptr) |
62 | 5.62k | { |
63 | 5.62k | if(!ptr) |
64 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
65 | 5.62k | if(pa->n >= pa->capacity) { |
66 | 5.60k | uint32_t growth; |
67 | 5.60k | void **ndata; |
68 | | |
69 | 5.60k | if(pa->capacity == UINT32_MAX) |
70 | 0 | return CURLE_OUT_OF_MEMORY; |
71 | 5.60k | if(!pa->capacity) |
72 | 5.60k | growth = 8; |
73 | 0 | else if((UINT32_MAX / 2) > pa->capacity) |
74 | 0 | growth = CURLMIN(2 * pa->capacity, CURL_PTRARRAY_MAX_GROW); |
75 | 0 | else |
76 | 0 | growth = ((UINT32_MAX - pa->capacity) > CURL_PTRARRAY_MAX_GROW) ? |
77 | 0 | CURL_PTRARRAY_MAX_GROW : (UINT32_MAX - pa->capacity); |
78 | | |
79 | 5.60k | ndata = curlx_calloc(1, (pa->capacity + growth) * sizeof(void *)); |
80 | 5.60k | if(!ndata) |
81 | 0 | return CURLE_OUT_OF_MEMORY; |
82 | 5.60k | if(pa->n) |
83 | 0 | memcpy(ndata, pa->data, pa->n * sizeof(void *)); |
84 | 5.60k | curlx_free(pa->data); |
85 | 5.60k | pa->data = ndata; |
86 | 5.60k | pa->capacity += growth; |
87 | 5.60k | } |
88 | 5.62k | DEBUGASSERT(pa->n < pa->capacity); |
89 | 5.62k | pa->data[pa->n++] = ptr; |
90 | 5.62k | return CURLE_OK; |
91 | 5.62k | } |
92 | | |
93 | | void *Curl_ptrarray_remove(struct ptrarray *pa, uint32_t idx) |
94 | 5.62k | { |
95 | 5.62k | if(idx < pa->n) { |
96 | 5.62k | uint32_t rem = pa->n - idx - 1; |
97 | 5.62k | void *ptr = pa->data[idx]; |
98 | 5.62k | pa->data[idx] = NULL; |
99 | 5.62k | if(rem) |
100 | 20 | memmove(pa->data + idx, pa->data + idx + 1, rem * sizeof(void *)); |
101 | 5.62k | pa->n--; |
102 | 5.62k | if(!pa->n) |
103 | 5.60k | ptrarray_clear(pa); |
104 | 5.62k | return ptr; |
105 | 5.62k | } |
106 | 0 | return NULL; |
107 | 5.62k | } |