Damjan Marion | 68b4da6 | 2018-09-30 18:26:20 +0200 | [diff] [blame] | 1 | /* |
| 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: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 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. |
| 14 | */ |
| 15 | |
| 16 | #ifndef included_palloc_h |
| 17 | #define included_palloc_h |
| 18 | #include <vppinfra/format.h> |
| 19 | #include <vppinfra/pool.h> |
| 20 | |
| 21 | #define PMALLOC_LOG2_BLOCK_SZ CLIB_LOG2_CACHE_LINE_BYTES |
| 22 | #define PMALLOC_BLOCK_SZ (1 << 6) |
| 23 | |
| 24 | #define CLIB_PMALLOC_NUMA_LOCAL 0xffffffff |
| 25 | |
| 26 | typedef struct |
| 27 | { |
| 28 | u32 start, prev, next; |
| 29 | u32 size:31; |
| 30 | u32 used:1; |
| 31 | } clib_pmalloc_chunk_t; |
| 32 | |
| 33 | STATIC_ASSERT_SIZEOF (clib_pmalloc_chunk_t, 16); |
| 34 | |
| 35 | typedef struct |
| 36 | { |
| 37 | u32 index; |
| 38 | u32 arena_index; |
| 39 | uword pa; |
| 40 | clib_pmalloc_chunk_t *chunks; |
| 41 | u32 first_chunk_index; |
| 42 | u32 n_free_chunks; |
| 43 | u32 n_free_blocks; |
| 44 | } clib_pmalloc_page_t; |
| 45 | |
| 46 | typedef struct |
| 47 | { |
| 48 | u32 index; |
| 49 | u32 flags; |
| 50 | #define CLIB_PMALLOC_ARENA_F_SHARED_MEM (1 << 0) |
| 51 | int fd; |
| 52 | u32 numa_node; |
| 53 | u32 first_page_index; |
| 54 | u32 log2_page_sz; |
| 55 | u32 n_pages; |
| 56 | u8 *name; |
| 57 | u32 *page_indices; |
| 58 | } clib_pmalloc_arena_t; |
| 59 | |
| 60 | typedef struct |
| 61 | { |
| 62 | u8 *base; |
| 63 | uword log2_page_sz; |
| 64 | uword *va_pa_diffs; |
| 65 | u32 max_pages; |
| 66 | clib_pmalloc_page_t *pages; |
| 67 | uword *chunk_index_by_va; |
| 68 | clib_pmalloc_arena_t *arenas; |
| 69 | u32 *default_arena_for_numa_node; |
| 70 | |
| 71 | clib_error_t *error; |
| 72 | } clib_pmalloc_main_t; |
| 73 | |
| 74 | |
| 75 | int clib_pmalloc_init (clib_pmalloc_main_t * pm, uword size); |
| 76 | void *clib_pmalloc_alloc_aligned_on_numa (clib_pmalloc_main_t * pm, |
| 77 | uword size, uword align, |
| 78 | u32 numa_node); |
| 79 | void *clib_pmalloc_alloc_aligned (clib_pmalloc_main_t * pm, uword size, |
| 80 | uword align); |
| 81 | void clib_pmalloc_free (clib_pmalloc_main_t * pm, void *va); |
| 82 | |
| 83 | void *clib_pmalloc_create_shared_arena (clib_pmalloc_main_t * pm, char *name, |
| 84 | uword size, u32 numa_node); |
| 85 | |
| 86 | void *clib_pmalloc_alloc_from_arena (clib_pmalloc_main_t * pm, void *arena_va, |
| 87 | uword size, uword align); |
| 88 | |
| 89 | format_function_t format_pmalloc; |
| 90 | |
| 91 | always_inline clib_error_t * |
| 92 | clib_pmalloc_last_error (clib_pmalloc_main_t * pm) |
| 93 | { |
| 94 | return pm->error; |
| 95 | } |
| 96 | |
| 97 | always_inline u32 |
| 98 | clib_pmalloc_get_page_index (clib_pmalloc_main_t * pm, void *va) |
| 99 | { |
| 100 | uword index = (pointer_to_uword (va) - pointer_to_uword (pm->base)) >> |
| 101 | pm->log2_page_sz; |
| 102 | |
| 103 | ASSERT (index < vec_len (pm->pages)); |
| 104 | |
| 105 | return index; |
| 106 | } |
| 107 | |
| 108 | always_inline clib_pmalloc_arena_t * |
| 109 | clib_pmalloc_get_arena (clib_pmalloc_main_t * pm, void *va) |
| 110 | { |
| 111 | u32 index = clib_pmalloc_get_page_index (pm, va); |
| 112 | return pm->arenas + pm->pages[index].arena_index; |
| 113 | } |
| 114 | |
| 115 | always_inline uword |
| 116 | clib_pmalloc_get_pa (clib_pmalloc_main_t * pm, void *va) |
| 117 | { |
| 118 | u32 index = clib_pmalloc_get_page_index (pm, va); |
| 119 | return pointer_to_uword (va) - pm->va_pa_diffs[index]; |
| 120 | } |
| 121 | |
| 122 | |
| 123 | #endif /* included_palloc_h */ |
| 124 | |
| 125 | /* |
| 126 | * fd.io coding-style-patch-verification: ON |
| 127 | * |
| 128 | * Local Variables: |
| 129 | * eval: (c-set-style "gnu") |
| 130 | * End: |
| 131 | */ |