FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
mem.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <vppinfra/mem.h>
3 
4 extern void * __libc_malloc (size_t);
5 extern void __libc_free (void *);
6 extern void * __libc_calloc (size_t, size_t);
7 extern void * __libc_realloc (void *, size_t);
8 extern void * __libc_valloc (size_t);
9 extern void * __libc_memalign (size_t, size_t);
10 extern void * __libc_pvalloc (size_t);
11 
13 
14 static void no_heap (void)
15 {
17 
18  if (1 == vpp_mem_no_vpp_heap)
19  fprintf (stderr, "vpp mem: libc allocation requested but no vpp heap ready, defaulting to libc.\n");
20 }
21 
24 {
26  return 1;
27 
28  no_heap ();
29  return 0;
30 }
31 
32 void *
33 malloc(size_t size)
34 {
35  if (!check_vpp_heap ())
36  return __libc_malloc (size);
37 
38  return clib_mem_alloc (size);
39 }
40 
41 void
42 free(void *p)
43 {
44  if (!p)
45  return;
46 
47  if (!check_vpp_heap ())
48  return __libc_free (p);
49 
50  clib_mem_free (p);
51 }
52 
53 void *
54 calloc(size_t nmemb, size_t size)
55 {
56  void * p;
57 
58  if (!check_vpp_heap ())
59  return __libc_calloc (nmemb, size);
60 
61  p = clib_mem_alloc (nmemb * size);
62  clib_memset (p, 0, nmemb * size);
63  return p;
64 }
65 
66 void *
67 realloc(void *p, size_t size)
68 {
69  if (!check_vpp_heap ())
70  return __libc_realloc (p, size);
71 
72  return clib_mem_realloc (p, size, clib_mem_size (p));
73 }
74 
75 int
76 posix_memalign(void **memptr, size_t alignment, size_t size)
77 {
78  if (!check_vpp_heap ())
79  *memptr = __libc_memalign (alignment, size);
80  else
81  *memptr = clib_mem_alloc_aligned (size, alignment);
82  return 0;
83 }
84 
85 void *
86 aligned_alloc(size_t alignment, size_t size)
87 {
88  if (!check_vpp_heap ())
89  return __libc_memalign (alignment, size);
90 
91  return clib_mem_alloc_aligned (size, alignment);
92 }
93 
94 void *
95 valloc(size_t size)
96 {
97  if (!check_vpp_heap ())
98  return __libc_valloc (size);
99 
101 }
102 
103 void *memalign(size_t alignment, size_t size)
104 {
105  if (!check_vpp_heap ())
106  return __libc_memalign (alignment, size);
107 
108  return clib_mem_alloc_aligned (size, alignment);
109 }
110 
111 void *
112 pvalloc(size_t size)
113 {
114  uword pagesz;
115 
116  if (!check_vpp_heap ())
117  return __libc_pvalloc (size);
118 
119  pagesz = clib_mem_get_page_size ();
120  return clib_mem_alloc_aligned (round_pow2 (size, pagesz), pagesz);
121 }
realloc
void * realloc(void *p, size_t size)
Definition: mem.c:67
clib_mem_realloc
static void * clib_mem_realloc(void *p, uword new_size, uword old_size)
Definition: mem.h:325
clib_mem_free
static void clib_mem_free(void *p)
Definition: mem.h:311
posix_memalign
int posix_memalign(void **memptr, size_t alignment, size_t size)
Definition: mem.c:76
round_pow2
static uword round_pow2(uword x, uword pow2)
Definition: clib.h:279
memalign
void * memalign(size_t alignment, size_t size)
Definition: mem.c:103
pvalloc
void * pvalloc(size_t size)
Definition: mem.c:112
clib_mem_get_per_cpu_heap
static clib_mem_heap_t * clib_mem_get_per_cpu_heap(void)
Definition: mem.h:161
clib_mem_size
static uword clib_mem_size(void *p)
Definition: mem.h:343
static_always_inline
#define static_always_inline
Definition: clib.h:112
uword
u64 uword
Definition: types.h:112
size
u32 size
Definition: vhost_user.h:125
vpp_mem_no_vpp_heap
__thread u64 vpp_mem_no_vpp_heap
Definition: mem.c:12
u64
unsigned long u64
Definition: types.h:89
check_vpp_heap
static_always_inline int check_vpp_heap(void)
Definition: mem.c:23
clib_mem_get_page_size
static_always_inline uword clib_mem_get_page_size(void)
Definition: mem.h:468
clib_memset
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
calloc
void * calloc(size_t nmemb, size_t size)
Definition: mem.c:54
free
void free(void *p)
Definition: mem.c:42
malloc
void * malloc(size_t size)
Definition: mem.c:33
clib_mem_alloc_aligned
static void * clib_mem_alloc_aligned(uword size, uword align)
Definition: mem.h:261
aligned_alloc
void * aligned_alloc(size_t alignment, size_t size)
Definition: mem.c:86
mem.h
valloc
void * valloc(size_t size)
Definition: mem.c:95
PREDICT_TRUE
#define PREDICT_TRUE(x)
Definition: clib.h:125
clib_mem_alloc
static void * clib_mem_alloc(uword size)
Definition: mem.h:253
no_heap
static void no_heap(void)
Definition: mem.c:14