2 * Copyright (c) 2015 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 Copyright (c) 2001, 2002, 2003 Eliot Dresselhaus
18 Permission is hereby granted, free of charge, to any person obtaining
19 a copy of this software and associated documentation files (the
20 "Software"), to deal in the Software without restriction, including
21 without limitation the rights to use, copy, modify, merge, publish,
22 distribute, sublicense, and/or sell copies of the Software, and to
23 permit persons to whom the Software is furnished to do so, subject to
24 the following conditions:
26 The above copyright notice and this permission notice shall be
27 included in all copies or substantial portions of the Software.
29 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 #ifndef _included_clib_mem_h
39 #define _included_clib_mem_h
45 #include <vppinfra/clib.h> /* uword, etc */
46 #include <vppinfra/clib_error.h>
49 #include <vppinfra/mheap_bootstrap.h>
51 #include <vppinfra/dlmalloc.h>
54 #include <vppinfra/os.h>
55 #include <vppinfra/string.h> /* memcpy, memset */
56 #include <vppinfra/valgrind.h>
58 #define CLIB_MAX_MHEAPS 256
61 extern void *clib_per_cpu_mheaps[CLIB_MAX_MHEAPS];
64 clib_mem_get_per_cpu_heap (void)
66 int cpu = os_get_thread_index ();
67 return clib_per_cpu_mheaps[cpu];
71 clib_mem_set_per_cpu_heap (u8 * new_heap)
73 int cpu = os_get_thread_index ();
74 void *old = clib_per_cpu_mheaps[cpu];
75 clib_per_cpu_mheaps[cpu] = new_heap;
79 /* Memory allocator which may call os_out_of_memory() if it fails */
81 clib_mem_alloc_aligned_at_offset (uword size, uword align, uword align_offset,
82 int os_out_of_memory_on_failure)
87 if (align_offset > align)
90 align_offset %= align;
95 cpu = os_get_thread_index ();
96 heap = clib_per_cpu_mheaps[cpu];
100 heap = mheap_get_aligned (heap, size, align, align_offset, &offset);
101 clib_per_cpu_mheaps[cpu] = heap;
107 VALGRIND_MALLOCLIKE_BLOCK (p, mheap_data_bytes (heap, offset), 0, 0);
113 if (os_out_of_memory_on_failure)
118 p = mspace_get_aligned (heap, size, align, align_offset);
119 if (PREDICT_FALSE (p == 0))
121 if (os_out_of_memory_on_failure)
127 #endif /* USE_DLMALLOC */
130 /* Memory allocator which calls os_out_of_memory() when it fails */
132 clib_mem_alloc (uword size)
134 return clib_mem_alloc_aligned_at_offset (size, /* align */ 1,
135 /* align_offset */ 0,
136 /* os_out_of_memory */ 1);
140 clib_mem_alloc_aligned (uword size, uword align)
142 return clib_mem_alloc_aligned_at_offset (size, align, /* align_offset */ 0,
143 /* os_out_of_memory */ 1);
146 /* Memory allocator which calls os_out_of_memory() when it fails */
148 clib_mem_alloc_or_null (uword size)
150 return clib_mem_alloc_aligned_at_offset (size, /* align */ 1,
151 /* align_offset */ 0,
152 /* os_out_of_memory */ 0);
156 clib_mem_alloc_aligned_or_null (uword size, uword align)
158 return clib_mem_alloc_aligned_at_offset (size, align, /* align_offset */ 0,
159 /* os_out_of_memory */ 0);
164 /* Memory allocator which panics when it fails.
165 Use macro so that clib_panic macro can expand __FUNCTION__ and __LINE__. */
166 #define clib_mem_alloc_aligned_no_fail(size,align) \
168 uword _clib_mem_alloc_size = (size); \
169 void * _clib_mem_alloc_p; \
170 _clib_mem_alloc_p = clib_mem_alloc_aligned (_clib_mem_alloc_size, (align)); \
171 if (! _clib_mem_alloc_p) \
172 clib_panic ("failed to allocate %d bytes", _clib_mem_alloc_size); \
176 #define clib_mem_alloc_no_fail(size) clib_mem_alloc_aligned_no_fail(size,1)
178 /* Alias to stack allocator for naming consistency. */
179 #define clib_mem_alloc_stack(bytes) __builtin_alloca(bytes)
182 clib_mem_is_heap_object (void *p)
184 #if USE_DLMALLOC == 0
185 void *heap = clib_mem_get_per_cpu_heap ();
186 uword offset = (uword) p - (uword) heap;
189 if (offset >= vec_len (heap))
192 e = mheap_elt_at_uoffset (heap, offset);
193 n = mheap_next_elt (e);
195 /* Check that heap forward and reverse pointers agree. */
196 return e->n_user_data == n->prev_n_user_data;
198 void *heap = clib_mem_get_per_cpu_heap ();
200 return mspace_is_heap_object (heap, p);
201 #endif /* USE_DLMALLOC */
205 clib_mem_free (void *p)
207 u8 *heap = clib_mem_get_per_cpu_heap ();
209 /* Make sure object is in the correct heap. */
210 ASSERT (clib_mem_is_heap_object (p));
212 #if USE_DLMALLOC == 0
213 mheap_put (heap, (u8 *) p - heap);
215 mspace_put (heap, p);
219 VALGRIND_FREELIKE_BLOCK (p, 0);
224 clib_mem_realloc (void *p, uword new_size, uword old_size)
226 /* By default use alloc, copy and free to emulate realloc. */
227 void *q = clib_mem_alloc (new_size);
231 if (old_size < new_size)
232 copy_size = old_size;
234 copy_size = new_size;
235 clib_memcpy (q, p, copy_size);
242 clib_mem_size (void *p)
244 #if USE_DLMALLOC == 0
245 mheap_elt_t *e = mheap_user_pointer_to_elt (p);
246 ASSERT (clib_mem_is_heap_object (p));
247 return mheap_elt_data_bytes (e);
249 ASSERT (clib_mem_is_heap_object (p));
250 return mspace_usable_size_with_delta (p);
255 clib_mem_get_heap (void)
257 return clib_mem_get_per_cpu_heap ();
261 clib_mem_set_heap (void *heap)
263 return clib_mem_set_per_cpu_heap (heap);
266 void *clib_mem_init (void *heap, uword size);
267 void *clib_mem_init_thread_safe (void *memory, uword memory_size);
269 void clib_mem_exit (void);
271 uword clib_mem_get_page_size (void);
273 void clib_mem_validate (void);
275 void clib_mem_trace (int enable);
279 /* Total number of objects allocated. */
282 /* Total allocated bytes. Bytes used and free.
283 used + free = total */
284 uword bytes_total, bytes_used, bytes_free;
286 /* Number of bytes used by mheap data structure overhead
287 (e.g. free lists, mheap header). */
288 uword bytes_overhead;
290 /* Amount of free space returned to operating system. */
291 uword bytes_free_reclaimed;
293 /* For malloc which puts small objects in sbrk region and
294 large objects in mmap'ed regions. */
295 uword bytes_used_sbrk;
296 uword bytes_used_mmap;
298 /* Max. number of bytes in this heap. */
302 void clib_mem_usage (clib_mem_usage_t * usage);
304 u8 *format_clib_mem_usage (u8 * s, va_list * args);
306 /* Allocate virtual address space. */
308 clib_mem_vm_alloc (uword size)
311 uword flags = MAP_PRIVATE;
314 flags |= MAP_ANONYMOUS;
317 mmap_addr = mmap (0, size, PROT_READ | PROT_WRITE, flags, -1, 0);
318 if (mmap_addr == (void *) -1)
325 clib_mem_vm_free (void *addr, uword size)
331 clib_mem_vm_unmap (void *addr, uword size)
334 uword flags = MAP_PRIVATE | MAP_FIXED;
336 /* To unmap we "map" with no protection. If we actually called
337 munmap then other callers could steal the address space. By
338 changing to PROT_NONE the kernel can free up the pages which is
339 really what we want "unmap" to mean. */
340 mmap_addr = mmap (addr, size, PROT_NONE, flags, -1, 0);
341 if (mmap_addr == (void *) -1)
348 clib_mem_vm_map (void *addr, uword size)
351 uword flags = MAP_PRIVATE | MAP_FIXED;
353 mmap_addr = mmap (addr, size, (PROT_READ | PROT_WRITE), flags, -1, 0);
354 if (mmap_addr == (void *) -1)
362 #define CLIB_MEM_VM_F_SHARED (1 << 0)
363 #define CLIB_MEM_VM_F_HUGETLB (1 << 1)
364 #define CLIB_MEM_VM_F_NUMA_PREFER (1 << 2)
365 #define CLIB_MEM_VM_F_NUMA_FORCE (1 << 3)
366 #define CLIB_MEM_VM_F_HUGETLB_PREALLOC (1 << 4)
367 #define CLIB_MEM_VM_F_LOCKED (1 << 5)
368 u32 flags; /**< vm allocation flags:
369 <br> CLIB_MEM_VM_F_SHARED: request shared memory, file
370 descriptor will be provided on successful allocation.
371 <br> CLIB_MEM_VM_F_HUGETLB: request hugepages.
372 <br> CLIB_MEM_VM_F_NUMA_PREFER: numa_node field contains valid
373 numa node preference.
374 <br> CLIB_MEM_VM_F_NUMA_FORCE: fail if setting numa policy fails.
375 <br> CLIB_MEM_VM_F_HUGETLB_PREALLOC: pre-allocate hugepages if
376 number of available pages is not sufficient.
377 <br> CLIB_MEM_VM_F_LOCKED: request locked memory.
379 char *name; /**< Name for memory allocation, set by caller. */
380 uword size; /**< Allocation size, set by caller. */
381 int numa_node; /**< numa node preference. Valid if CLIB_MEM_VM_F_NUMA_PREFER set. */
382 void *addr; /**< Pointer to allocated memory, set on successful allocation. */
383 int fd; /**< File descriptor, set on successful allocation if CLIB_MEM_VM_F_SHARED is set. */
384 int log2_page_size; /* Page size in log2 format, set on successful allocation. */
385 int n_pages; /* Number of pages. */
386 uword requested_va; /**< Request fixed position mapping */
387 } clib_mem_vm_alloc_t;
389 clib_error_t *clib_mem_vm_ext_alloc (clib_mem_vm_alloc_t * a);
390 u64 clib_mem_vm_get_page_size (int fd);
391 int clib_mem_vm_get_log2_page_size (int fd);
392 u64 *clib_mem_vm_get_paddr (void *mem, int log2_page_size, int n_pages);
396 uword size; /**< Map size */
397 int fd; /**< File descriptor to be mapped */
398 uword requested_va; /**< Request fixed position mapping */
399 void *addr; /**< Pointer to mapped memory, if successful */
402 clib_error_t *clib_mem_vm_ext_map (clib_mem_vm_map_t * a);
403 void clib_mem_vm_randomize_va (uword * requested_va, u32 log2_page_size);
404 void mheap_trace (void *v, int enable);
406 #include <vppinfra/error.h> /* clib_panic */
408 #endif /* _included_clib_mem_h */
411 * fd.io coding-style-patch-verification: ON
414 * eval: (c-set-style "gnu")