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