Coverage Report

Created: 2026-10-15 06:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/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
22.6k
{
32
22.6k
  pa->data = NULL;
33
22.6k
  pa->n = pa->capacity = 0;
34
22.6k
}
35
36
void Curl_ptrarray_clear(struct ptrarray *pa)
37
78
{
38
78
  curlx_safefree(pa->data);
39
78
  pa->n = pa->capacity = 0;
40
78
}
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
125
{
49
125
  if(!ptr)
50
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
51
125
  if(pa->n >= pa->capacity) {
52
79
    uint32_t growth;
53
79
    void **ndata;
54
55
79
    if(pa->capacity == UINT32_MAX)
56
0
      return CURLE_OUT_OF_MEMORY;
57
79
    if(!pa->capacity)
58
79
      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
79
    ndata = curlx_calloc(1, (pa->capacity + growth) * sizeof(void *));
66
79
    if(!ndata)
67
1
      return CURLE_OUT_OF_MEMORY;
68
78
    if(pa->n)
69
0
      memcpy(ndata, pa->data, pa->n * sizeof(void *));
70
78
    curlx_free(pa->data);
71
78
    pa->data = ndata;
72
78
    pa->capacity += growth;
73
78
  }
74
124
  DEBUGASSERT(pa->n < pa->capacity);
75
124
  pa->data[pa->n++] = ptr;
76
124
  return CURLE_OK;
77
124
}
78
79
void *Curl_ptrarray_remove(struct ptrarray *pa, uint32_t idx)
80
124
{
81
124
  if(idx < pa->n) {
82
124
    uint32_t rem = pa->n - idx - 1;
83
124
    void *ptr = pa->data[idx];
84
124
    pa->data[idx] = NULL;
85
124
    if(rem)
86
46
      memmove(pa->data + idx, pa->data + idx + 1, rem * sizeof(void *));
87
124
    pa->n--;
88
124
    if(!pa->n)
89
78
      Curl_ptrarray_clear(pa);
90
124
    return ptr;
91
124
  }
92
0
  return NULL;
93
124
}
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
}