aboutsummaryrefslogtreecommitdiffstats
path: root/src/vppinfra/vec.c
blob: 2fbab2f7e45fcf6524d7cf4888749dad7caadfd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* SPDX-License-Identifier: Apache-2.0
 * Copyright(c) 2022 Cisco Systems, Inc.
 */

#include <vppinfra/vec.h>
#include <vppinfra/mem.h>

#ifndef CLIB_VECTOR_GROW_BY_ONE
#define CLIB_VECTOR_GROW_BY_ONE 0
#endif

__clib_export uword
vec_mem_size (void *v)
{
  return v ? clib_mem_size (v - vec_get_header_size (v)) : 0;
}

__clib_export void *
_vec_realloc (void *v, uword n_elts, uword elt_sz, uword hdr_sz, uword align,
	      void *heap)
{
  uword n_data_bytes, data_offset, new_data_size, alloc_size;
  void *p;

  /* alignment must be power of 2 */
  align = clib_max (align, VEC_MIN_ALIGN);
  ASSERT (count_set_bits (align) == 1);

  /* number of bytes needed to store both vector header and optional user
   * header */
  data_offset = round_pow2 (hdr_sz + sizeof (vec_header_t), align);

  /* mumber of bytes needed to store vector data */
  n_data_bytes = n_elts * elt_sz;

  /* minimal allocation needed to store data and headers */
  new_data_size = data_offset + n_data_bytes;

  if (v)
    {
      uword old_data_size = data_offset + _vec_len (v) * elt_sz;
      p = vec_header (v);
      alloc_size = clib_mem_size (p);

      /* check that we are still dealing with the same vector type */
      ASSERT (_vec_find (v)->hdr_size * VEC_MIN_ALIGN == data_offset);
      ASSERT (_vec_find (v)->log2_align == min_log2 (align));

      /* realloc if new size cannot fit into existing allocation */
      if (alloc_size < new_data_size)
	{
	  if (CLIB_VECTOR_GROW_BY_ONE)
	    alloc_size = n_data_bytes + data_offset;
	  else
	    alloc_size = (n_data_bytes * 3) / 2 + data_offset;

	  p = clib_mem_realloc_aligned (p, alloc_size, align);
	  alloc_size = clib_mem_size (p);
	  v = p + data_offset;
	}

      CLIB_MEM_UNPOISON (p, alloc_size);
      clib_memset_u8 (p + old_data_size, 0, alloc_size - old_data_size);
    }
  else
    {
      /* new allocation */
      p = clib_mem_alloc_aligned (new_data_size, align);
      alloc_size = clib_mem_size (p);
      CLIB_MEM_UNPOISON (p, alloc_size);
      clib_memset_u8 (p, 0, alloc_size);
      v = p + data_offset;
      _vec_find (v)->hdr_size = data_offset / VEC_MIN_ALIGN;
      _vec_find (v)->log2_align = min_log2 (align);
    }

  CLIB_MEM_POISON (p + new_data_size, alloc_size - new_data_size);
  _vec_len (v) = n_elts;
  return v;
}

__clib_export u32
vec_len_not_inline (void *v)
{
  return vec_len (v);
}

__clib_export void
vec_free_not_inline (void *v)
{
  vec_free (v);
}