X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=src%2Fvppinfra%2Fmem.h;h=5492e106d916da86c8de6f2d82ee711059e9b090;hb=a690fdbfe179e0ea65818c03b52535bf9210efd0;hp=a2c5474a46e27a1c39651bfb2c628f22fd4d5676;hpb=567e61d09cd00174203eff85f63a598420476951;p=vpp.git diff --git a/src/vppinfra/mem.h b/src/vppinfra/mem.h index a2c5474a46e..5492e106d91 100644 --- a/src/vppinfra/mem.h +++ b/src/vppinfra/mem.h @@ -53,12 +53,17 @@ #include #include /* memcpy, clib_memset */ -#include +#include #define CLIB_MAX_MHEAPS 256 +#define CLIB_MAX_NUMAS 8 + +/* Unspecified NUMA socket */ +#define VEC_NUMA_UNSPECIFIED (0xFF) /* Per CPU heaps. */ extern void *clib_per_cpu_mheaps[CLIB_MAX_MHEAPS]; +extern void *clib_per_numa_mheaps[CLIB_MAX_NUMAS]; always_inline void * clib_mem_get_per_cpu_heap (void) @@ -76,6 +81,54 @@ clib_mem_set_per_cpu_heap (u8 * new_heap) return old; } +always_inline void * +clib_mem_get_per_numa_heap (u32 numa_id) +{ + ASSERT (numa_id >= 0 && numa_id < ARRAY_LEN (clib_per_numa_mheaps)); + return clib_per_numa_mheaps[numa_id]; +} + +always_inline void * +clib_mem_set_per_numa_heap (u8 * new_heap) +{ + int numa = os_get_numa_index (); + void *old = clib_per_numa_mheaps[numa]; + clib_per_numa_mheaps[numa] = new_heap; + return old; +} + +always_inline void +clib_mem_set_thread_index (void) +{ + /* + * Find an unused slot in the per-cpu-mheaps array, + * and grab it for this thread. We need to be able to + * push/pop the thread heap without affecting other thread(s). + */ + int i; + if (__os_thread_index != 0) + return; + for (i = 0; i < ARRAY_LEN (clib_per_cpu_mheaps); i++) + if (clib_atomic_bool_cmp_and_swap (&clib_per_cpu_mheaps[i], + 0, clib_per_cpu_mheaps[0])) + { + os_set_thread_index (i); + break; + } + ASSERT (__os_thread_index > 0); +} + +always_inline uword +clib_mem_size_nocheck (void *p) +{ +#if USE_DLMALLOC == 0 + mheap_elt_t *e = mheap_user_pointer_to_elt (p); + return mheap_elt_data_bytes (e); +#else + return mspace_usable_size_with_delta (p); +#endif +} + /* Memory allocator which may call os_out_of_memory() if it fails */ always_inline void * clib_mem_alloc_aligned_at_offset (uword size, uword align, uword align_offset, @@ -99,32 +152,21 @@ clib_mem_alloc_aligned_at_offset (uword size, uword align, uword align_offset, uword offset; heap = mheap_get_aligned (heap, size, align, align_offset, &offset); clib_per_cpu_mheaps[cpu] = heap; - - if (offset != ~0) - { - p = heap + offset; -#if CLIB_DEBUG > 0 - VALGRIND_MALLOCLIKE_BLOCK (p, mheap_data_bytes (heap, offset), 0, 0); -#endif - return p; - } - else - { - if (os_out_of_memory_on_failure) - os_out_of_memory (); - return 0; - } + if (PREDICT_TRUE (offset != ~0)) + p = heap + offset; #else p = mspace_get_aligned (heap, size, align, align_offset); - if (PREDICT_FALSE (p == 0)) +#endif /* USE_DLMALLOC */ + + if (PREDICT_FALSE (0 == p)) { if (os_out_of_memory_on_failure) os_out_of_memory (); return 0; } + CLIB_MEM_UNPOISON (p, size); return p; -#endif /* USE_DLMALLOC */ } /* Memory allocator which calls os_out_of_memory() when it fails */ @@ -209,15 +251,13 @@ clib_mem_free (void *p) /* Make sure object is in the correct heap. */ ASSERT (clib_mem_is_heap_object (p)); + CLIB_MEM_POISON (p, clib_mem_size_nocheck (p)); + #if USE_DLMALLOC == 0 mheap_put (heap, (u8 *) p - heap); #else mspace_put (heap, p); #endif - -#if CLIB_DEBUG > 0 - VALGRIND_FREELIKE_BLOCK (p, 0); -#endif } always_inline void * @@ -232,7 +272,7 @@ clib_mem_realloc (void *p, uword new_size, uword old_size) copy_size = old_size; else copy_size = new_size; - clib_memcpy (q, p, copy_size); + clib_memcpy_fast (q, p, copy_size); clib_mem_free (p); } return q; @@ -241,14 +281,17 @@ clib_mem_realloc (void *p, uword new_size, uword old_size) always_inline uword clib_mem_size (void *p) { -#if USE_DLMALLOC == 0 - mheap_elt_t *e = mheap_user_pointer_to_elt (p); ASSERT (clib_mem_is_heap_object (p)); - return mheap_elt_data_bytes (e); -#else - ASSERT (clib_mem_is_heap_object (p)); - return mspace_usable_size_with_delta (p); -#endif + return clib_mem_size_nocheck (p); +} + +always_inline void +clib_mem_free_s (void *p) +{ + uword size = clib_mem_size (p); + CLIB_MEM_UNPOISON (p, size); + memset_s_inline (p, size, 0, size); + clib_mem_free (p); } always_inline void * @@ -265,6 +308,7 @@ clib_mem_set_heap (void *heap) void *clib_mem_init (void *heap, uword size); void *clib_mem_init_thread_safe (void *memory, uword memory_size); +void *clib_mem_init_thread_safe_numa (void *memory, uword memory_size); void clib_mem_exit (void); @@ -274,6 +318,8 @@ void clib_mem_validate (void); void clib_mem_trace (int enable); +int clib_mem_is_traced (void); + typedef struct { /* Total number of objects allocated. */ @@ -406,6 +452,8 @@ typedef struct clib_error_t *clib_mem_vm_ext_map (clib_mem_vm_map_t * a); void clib_mem_vm_randomize_va (uword * requested_va, u32 log2_page_size); void mheap_trace (void *v, int enable); +uword clib_mem_trace_enable_disable (uword enable); +void clib_mem_trace (int enable); #include /* clib_panic */