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 | 13.5k | { |
32 | 13.5k | pa->data = NULL; |
33 | 13.5k | pa->n = pa->capacity = 0; |
34 | 13.5k | } |
35 | | |
36 | | void Curl_ptrarray_clear(struct ptrarray *pa) |
37 | 0 | { |
38 | 0 | curlx_safefree(pa->data); |
39 | 0 | pa->n = pa->capacity = 0; |
40 | 0 | } |
41 | | |
42 | | uint32_t Curl_ptrarray_count(struct ptrarray *pa) |
43 | 0 | { |
44 | 0 | return pa->n; |
45 | 0 | } |
46 | | |
47 | | CURLcode Curl_ptrarray_add(struct ptrarray *pa, void *ptr) |
48 | 0 | { |
49 | 0 | if(!ptr) |
50 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
51 | 0 | if(pa->n >= pa->capacity) { |
52 | 0 | uint32_t growth; |
53 | 0 | void **ndata; |
54 | |
|
55 | 0 | if(pa->capacity == UINT32_MAX) |
56 | 0 | return CURLE_OUT_OF_MEMORY; |
57 | 0 | if(!pa->capacity) |
58 | 0 | growth = 8; |
59 | 0 | else if((UINT32_MAX / 2) > pa->capacity) |
60 | 0 | growth = CURLMIN(2 * pa->capacity, CURL_PTRARRAY_MAX_GROW); |
61 | 0 | else |
62 | 0 | growth = ((UINT32_MAX - pa->capacity) > CURL_PTRARRAY_MAX_GROW) ? |
63 | 0 | CURL_PTRARRAY_MAX_GROW : (UINT32_MAX - pa->capacity); |
64 | |
|
65 | 0 | ndata = curlx_calloc(1, (pa->capacity + growth) * sizeof(void *)); |
66 | 0 | if(!ndata) |
67 | 0 | return CURLE_OUT_OF_MEMORY; |
68 | 0 | if(pa->n) |
69 | 0 | memcpy(ndata, pa->data, pa->n * sizeof(void *)); |
70 | 0 | curlx_free(pa->data); |
71 | 0 | pa->data = ndata; |
72 | 0 | pa->capacity += growth; |
73 | 0 | } |
74 | 0 | DEBUGASSERT(pa->n < pa->capacity); |
75 | 0 | pa->data[pa->n++] = ptr; |
76 | 0 | return CURLE_OK; |
77 | 0 | } |
78 | | |
79 | | void *Curl_ptrarray_remove(struct ptrarray *pa, uint32_t idx) |
80 | 0 | { |
81 | 0 | if(idx < pa->n) { |
82 | 0 | uint32_t rem = pa->n - idx - 1; |
83 | 0 | void *ptr = pa->data[idx]; |
84 | 0 | pa->data[idx] = NULL; |
85 | 0 | if(rem) |
86 | 0 | memmove(pa->data + idx, pa->data + idx + 1, rem * sizeof(void *)); |
87 | 0 | pa->n--; |
88 | 0 | if(!pa->n) |
89 | 0 | Curl_ptrarray_clear(pa); |
90 | 0 | return ptr; |
91 | 0 | } |
92 | 0 | return NULL; |
93 | 0 | } |
94 | | |
95 | | void *Curl_ptrarray_get(struct ptrarray *pa, uint32_t idx) |
96 | 0 | { |
97 | 0 | return (idx < pa->n) ? pa->data[idx] : NULL; |
98 | 0 | } |