2 * Copyright (c) 2018 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #ifndef included_palloc_h
17 #define included_palloc_h
18 #include <vppinfra/format.h>
19 #include <vppinfra/pool.h>
21 #define PMALLOC_LOG2_BLOCK_SZ CLIB_LOG2_CACHE_LINE_BYTES
22 #define PMALLOC_BLOCK_SZ (1 << 6)
24 #define CLIB_PMALLOC_NUMA_LOCAL 0xffffffff
28 u32 start, prev, next;
31 } clib_pmalloc_chunk_t;
33 STATIC_ASSERT_SIZEOF (clib_pmalloc_chunk_t, 16);
39 clib_pmalloc_chunk_t *chunks;
40 u32 first_chunk_index;
43 } clib_pmalloc_page_t;
49 #define CLIB_PMALLOC_ARENA_F_SHARED_MEM (1 << 0)
54 u32 subpages_per_page;
58 } clib_pmalloc_arena_t;
64 #define CLIB_PMALLOC_F_NO_PAGEMAP (1 << 0)
69 /* default page size - typically 2M */
72 /* system page size - typically 4K */
75 /* maximum number of pages, limited by VA preallocation size */
78 /* vector of pages - each page have own alloc pool and it can be split
79 into subpages (i.e. 2M page build out of 512 4K pages) */
80 clib_pmalloc_page_t *pages;
82 /* hash used to find chunk index out of VA, chunk index is defined
84 uword *chunk_index_by_va;
86 /* alloc arenas are group of pages which share same attributes
87 shared arenas are represented by FD and they are not grovable
88 private arenas are growable */
89 clib_pmalloc_arena_t *arenas;
91 /* vector of per numa node alloc arena indices
92 each numa node have own default privat alloc arena */
93 u32 *default_arena_for_numa_node;
95 /* VA to PA lookup table */
98 /* lookup page size - equals to smalles subpage used */
99 u32 lookup_log2_page_sz;
103 } clib_pmalloc_main_t;
106 int clib_pmalloc_init (clib_pmalloc_main_t * pm, uword size);
107 void *clib_pmalloc_alloc_aligned_on_numa (clib_pmalloc_main_t * pm,
108 uword size, uword align,
110 void *clib_pmalloc_alloc_aligned (clib_pmalloc_main_t * pm, uword size,
112 void clib_pmalloc_free (clib_pmalloc_main_t * pm, void *va);
114 void *clib_pmalloc_create_shared_arena (clib_pmalloc_main_t * pm, char *name,
115 uword size, u32 log2_page_sz,
118 void *clib_pmalloc_alloc_from_arena (clib_pmalloc_main_t * pm, void *arena_va,
119 uword size, uword align);
121 format_function_t format_pmalloc;
122 format_function_t format_pmalloc_map;
124 always_inline clib_error_t *
125 clib_pmalloc_last_error (clib_pmalloc_main_t * pm)
131 clib_pmalloc_get_page_index (clib_pmalloc_main_t * pm, void *va)
133 uword index = (pointer_to_uword (va) - pointer_to_uword (pm->base)) >>
134 pm->def_log2_page_sz;
136 ASSERT (index < vec_len (pm->pages));
141 always_inline clib_pmalloc_arena_t *
142 clib_pmalloc_get_arena (clib_pmalloc_main_t * pm, void *va)
144 u32 index = clib_pmalloc_get_page_index (pm, va);
145 return pm->arenas + pm->pages[index].arena_index;
149 clib_pmalloc_get_pa (clib_pmalloc_main_t * pm, void *va)
151 uword index = (pointer_to_uword (va) - pointer_to_uword (pm->base)) >>
152 pm->lookup_log2_page_sz;
153 return pointer_to_uword (va) - pm->lookup_table[index];
157 #endif /* included_palloc_h */
160 * fd.io coding-style-patch-verification: ON
163 * eval: (c-set-style "gnu")