Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 1 | /* |
| 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: |
| 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 | #include <vppinfra/format.h> |
| 17 | #include <vppinfra/dlmalloc.h> |
| 18 | #include <vppinfra/os.h> |
| 19 | #include <vppinfra/lock.h> |
| 20 | #include <vppinfra/hash.h> |
| 21 | #include <vppinfra/elf_clib.h> |
| 22 | |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 23 | typedef struct |
| 24 | { |
| 25 | /* Address of callers: outer first, inner last. */ |
| 26 | uword callers[12]; |
| 27 | |
| 28 | /* Count of allocations with this traceback. */ |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 29 | u32 n_allocations; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 30 | |
| 31 | /* Count of bytes allocated with this traceback. */ |
| 32 | u32 n_bytes; |
| 33 | |
| 34 | /* Offset of this item */ |
| 35 | uword offset; |
| 36 | } mheap_trace_t; |
| 37 | |
| 38 | typedef struct |
| 39 | { |
| 40 | clib_spinlock_t lock; |
| 41 | uword enabled; |
| 42 | |
| 43 | mheap_trace_t *traces; |
| 44 | |
| 45 | /* Indices of free traces. */ |
| 46 | u32 *trace_free_list; |
| 47 | |
| 48 | /* Hash table mapping callers to trace index. */ |
| 49 | uword *trace_by_callers; |
| 50 | |
| 51 | /* Hash table mapping mheap offset to trace index. */ |
| 52 | uword *trace_index_by_offset; |
Dave Barach | d67a428 | 2019-06-15 12:46:13 -0400 | [diff] [blame] | 53 | |
| 54 | /* So we can easily shut off current segment trace, if any */ |
| 55 | void *current_traced_mheap; |
| 56 | |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 57 | } mheap_trace_main_t; |
| 58 | |
| 59 | mheap_trace_main_t mheap_trace_main; |
| 60 | |
| 61 | void |
| 62 | mheap_get_trace (uword offset, uword size) |
| 63 | { |
| 64 | mheap_trace_main_t *tm = &mheap_trace_main; |
| 65 | mheap_trace_t *t; |
| 66 | uword i, n_callers, trace_index, *p; |
| 67 | mheap_trace_t trace; |
| 68 | uword save_enabled; |
| 69 | |
Dave Barach | d67a428 | 2019-06-15 12:46:13 -0400 | [diff] [blame] | 70 | if (tm->enabled == 0 || (clib_mem_get_heap () != tm->current_traced_mheap)) |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 71 | return; |
| 72 | |
| 73 | /* Spurious Coverity warnings be gone. */ |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 74 | clib_memset (&trace, 0, sizeof (trace)); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 75 | |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 76 | clib_spinlock_lock (&tm->lock); |
| 77 | |
| 78 | /* Turn off tracing to avoid embarrassment... */ |
| 79 | save_enabled = tm->enabled; |
| 80 | tm->enabled = 0; |
| 81 | |
Benoît Ganne | ec4749a | 2020-09-18 10:05:37 +0200 | [diff] [blame] | 82 | /* Skip our frame and mspace_get_aligned's frame */ |
| 83 | n_callers = clib_backtrace (trace.callers, ARRAY_LEN (trace.callers), 2); |
| 84 | if (n_callers == 0) |
| 85 | goto out; |
| 86 | |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 87 | if (!tm->trace_by_callers) |
| 88 | tm->trace_by_callers = |
| 89 | hash_create_shmem (0, sizeof (trace.callers), sizeof (uword)); |
| 90 | |
| 91 | p = hash_get_mem (tm->trace_by_callers, &trace.callers); |
| 92 | if (p) |
| 93 | { |
| 94 | trace_index = p[0]; |
| 95 | t = tm->traces + trace_index; |
| 96 | } |
| 97 | else |
| 98 | { |
| 99 | i = vec_len (tm->trace_free_list); |
| 100 | if (i > 0) |
| 101 | { |
| 102 | trace_index = tm->trace_free_list[i - 1]; |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 103 | vec_set_len (tm->trace_free_list, i - 1); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 104 | } |
| 105 | else |
| 106 | { |
| 107 | mheap_trace_t *old_start = tm->traces; |
| 108 | mheap_trace_t *old_end = vec_end (tm->traces); |
| 109 | |
| 110 | vec_add2 (tm->traces, t, 1); |
| 111 | |
| 112 | if (tm->traces != old_start) |
| 113 | { |
| 114 | hash_pair_t *p; |
| 115 | mheap_trace_t *q; |
| 116 | /* *INDENT-OFF* */ |
| 117 | hash_foreach_pair (p, tm->trace_by_callers, |
| 118 | ({ |
| 119 | q = uword_to_pointer (p->key, mheap_trace_t *); |
| 120 | ASSERT (q >= old_start && q < old_end); |
| 121 | p->key = pointer_to_uword (tm->traces + (q - old_start)); |
| 122 | })); |
| 123 | /* *INDENT-ON* */ |
| 124 | } |
| 125 | trace_index = t - tm->traces; |
| 126 | } |
| 127 | |
| 128 | t = tm->traces + trace_index; |
| 129 | t[0] = trace; |
| 130 | t->n_allocations = 0; |
| 131 | t->n_bytes = 0; |
| 132 | hash_set_mem (tm->trace_by_callers, t->callers, trace_index); |
| 133 | } |
| 134 | |
| 135 | t->n_allocations += 1; |
| 136 | t->n_bytes += size; |
| 137 | t->offset = offset; /* keep a sample to autopsy */ |
| 138 | hash_set (tm->trace_index_by_offset, offset, t - tm->traces); |
Benoît Ganne | ec4749a | 2020-09-18 10:05:37 +0200 | [diff] [blame] | 139 | |
| 140 | out: |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 141 | tm->enabled = save_enabled; |
| 142 | clib_spinlock_unlock (&tm->lock); |
| 143 | } |
| 144 | |
| 145 | void |
| 146 | mheap_put_trace (uword offset, uword size) |
| 147 | { |
| 148 | mheap_trace_t *t; |
| 149 | uword trace_index, *p; |
| 150 | mheap_trace_main_t *tm = &mheap_trace_main; |
| 151 | uword save_enabled; |
| 152 | |
| 153 | if (tm->enabled == 0) |
| 154 | return; |
| 155 | |
| 156 | clib_spinlock_lock (&tm->lock); |
| 157 | |
| 158 | /* Turn off tracing for a moment */ |
| 159 | save_enabled = tm->enabled; |
| 160 | tm->enabled = 0; |
| 161 | |
| 162 | p = hash_get (tm->trace_index_by_offset, offset); |
| 163 | if (!p) |
| 164 | { |
| 165 | tm->enabled = save_enabled; |
| 166 | clib_spinlock_unlock (&tm->lock); |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | trace_index = p[0]; |
| 171 | hash_unset (tm->trace_index_by_offset, offset); |
| 172 | ASSERT (trace_index < vec_len (tm->traces)); |
| 173 | |
| 174 | t = tm->traces + trace_index; |
| 175 | ASSERT (t->n_allocations > 0); |
| 176 | ASSERT (t->n_bytes >= size); |
| 177 | t->n_allocations -= 1; |
| 178 | t->n_bytes -= size; |
| 179 | if (t->n_allocations == 0) |
| 180 | { |
| 181 | hash_unset_mem (tm->trace_by_callers, t->callers); |
| 182 | vec_add1 (tm->trace_free_list, trace_index); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 183 | clib_memset (t, 0, sizeof (t[0])); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 184 | } |
| 185 | tm->enabled = save_enabled; |
| 186 | clib_spinlock_unlock (&tm->lock); |
| 187 | } |
| 188 | |
| 189 | always_inline void |
| 190 | mheap_trace_main_free (mheap_trace_main_t * tm) |
| 191 | { |
| 192 | vec_free (tm->traces); |
| 193 | vec_free (tm->trace_free_list); |
| 194 | hash_free (tm->trace_by_callers); |
| 195 | hash_free (tm->trace_index_by_offset); |
| 196 | } |
| 197 | |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 198 | static clib_mem_heap_t * |
| 199 | clib_mem_create_heap_internal (void *base, uword size, |
| 200 | clib_mem_page_sz_t log2_page_sz, int is_locked, |
| 201 | char *name) |
| 202 | { |
| 203 | clib_mem_heap_t *h; |
| 204 | u8 flags = 0; |
| 205 | int sz = sizeof (clib_mem_heap_t); |
| 206 | |
| 207 | if (base == 0) |
| 208 | { |
| 209 | log2_page_sz = clib_mem_log2_page_size_validate (log2_page_sz); |
| 210 | size = round_pow2 (size, clib_mem_page_bytes (log2_page_sz)); |
| 211 | base = clib_mem_vm_map_internal (0, log2_page_sz, size, -1, 0, |
| 212 | "main heap"); |
| 213 | |
| 214 | if (base == CLIB_MEM_VM_MAP_FAILED) |
| 215 | return 0; |
| 216 | |
| 217 | flags = CLIB_MEM_HEAP_F_UNMAP_ON_DESTROY; |
| 218 | } |
| 219 | else |
| 220 | log2_page_sz = CLIB_MEM_PAGE_SZ_UNKNOWN; |
| 221 | |
| 222 | if (is_locked) |
| 223 | flags |= CLIB_MEM_HEAP_F_LOCKED; |
| 224 | |
| 225 | h = base; |
| 226 | h->base = base; |
| 227 | h->size = size; |
| 228 | h->log2_page_sz = log2_page_sz; |
| 229 | h->flags = flags; |
| 230 | sz = strlen (name); |
| 231 | strcpy (h->name, name); |
| 232 | sz = round_pow2 (sz + sizeof (clib_mem_heap_t), 16); |
| 233 | h->mspace = create_mspace_with_base (base + sz, size - sz, is_locked); |
| 234 | |
| 235 | mspace_disable_expand (h->mspace); |
| 236 | |
Damjan Marion | 79934e8 | 2022-04-05 12:40:31 +0200 | [diff] [blame] | 237 | clib_mem_poison (mspace_least_addr (h->mspace), |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 238 | mspace_footprint (h->mspace)); |
| 239 | |
| 240 | return h; |
| 241 | } |
| 242 | |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 243 | /* Initialize CLIB heap based on memory/size given by user. |
| 244 | Set memory to 0 and CLIB will try to allocate its own heap. */ |
Dave Barach | a690fdb | 2020-01-21 12:34:55 -0500 | [diff] [blame] | 245 | static void * |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 246 | clib_mem_init_internal (void *base, uword size, |
| 247 | clib_mem_page_sz_t log2_page_sz) |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 248 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 249 | clib_mem_heap_t *h; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 250 | |
Damjan Marion | c63e2a4 | 2020-09-16 21:36:00 +0200 | [diff] [blame] | 251 | clib_mem_main_init (); |
| 252 | |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 253 | h = clib_mem_create_heap_internal (base, size, log2_page_sz, |
| 254 | 1 /*is_locked */ , "main heap"); |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 255 | |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 256 | clib_mem_set_heap (h); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 257 | |
| 258 | if (mheap_trace_main.lock == 0) |
| 259 | clib_spinlock_init (&mheap_trace_main.lock); |
| 260 | |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 261 | return h; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 262 | } |
| 263 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 264 | __clib_export void * |
Dave Barach | a690fdb | 2020-01-21 12:34:55 -0500 | [diff] [blame] | 265 | clib_mem_init (void *memory, uword memory_size) |
| 266 | { |
| 267 | return clib_mem_init_internal (memory, memory_size, |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 268 | CLIB_MEM_PAGE_SZ_DEFAULT); |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 269 | } |
| 270 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 271 | __clib_export void * |
Damjan Marion | 6bfd076 | 2020-09-11 22:16:53 +0200 | [diff] [blame] | 272 | clib_mem_init_with_page_size (uword memory_size, |
| 273 | clib_mem_page_sz_t log2_page_sz) |
| 274 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 275 | return clib_mem_init_internal (0, memory_size, log2_page_sz); |
Dave Barach | a690fdb | 2020-01-21 12:34:55 -0500 | [diff] [blame] | 276 | } |
| 277 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 278 | __clib_export void * |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 279 | clib_mem_init_thread_safe (void *memory, uword memory_size) |
| 280 | { |
Dave Barach | a690fdb | 2020-01-21 12:34:55 -0500 | [diff] [blame] | 281 | return clib_mem_init_internal (memory, memory_size, |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 282 | CLIB_MEM_PAGE_SZ_DEFAULT); |
Dave Barach | 2b79341 | 2020-08-28 10:39:00 -0400 | [diff] [blame] | 283 | } |
| 284 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 285 | __clib_export void |
Dave Barach | 2b79341 | 2020-08-28 10:39:00 -0400 | [diff] [blame] | 286 | clib_mem_destroy (void) |
| 287 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 288 | mheap_trace_main_t *tm = &mheap_trace_main; |
| 289 | clib_mem_heap_t *heap = clib_mem_get_heap (); |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 290 | |
| 291 | if (tm->enabled && heap->mspace == tm->current_traced_mheap) |
| 292 | tm->enabled = 0; |
| 293 | |
| 294 | destroy_mspace (heap->mspace); |
Damjan Marion | 16ca1a9 | 2022-04-28 17:46:13 +0200 | [diff] [blame] | 295 | clib_mem_vm_unmap (heap); |
Dave Barach | 2b79341 | 2020-08-28 10:39:00 -0400 | [diff] [blame] | 296 | } |
| 297 | |
Damjan Marion | 4a251d0 | 2021-05-06 17:28:12 +0200 | [diff] [blame] | 298 | __clib_export u8 * |
| 299 | format_clib_mem_usage (u8 *s, va_list *va) |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 300 | { |
| 301 | int verbose = va_arg (*va, int); |
| 302 | return format (s, "$$$$ heap at %llx verbose %d", clib_mem_get_heap (), |
| 303 | verbose); |
| 304 | } |
| 305 | |
| 306 | /* |
| 307 | * Magic decoder ring for mallinfo stats (ala dlmalloc): |
| 308 | * |
| 309 | * size_t arena; / * Non-mmapped space allocated (bytes) * / |
| 310 | * size_t ordblks; / * Number of free chunks * / |
| 311 | * size_t smblks; / * Number of free fastbin blocks * / |
| 312 | * size_t hblks; / * Number of mmapped regions * / |
| 313 | * size_t hblkhd; / * Space allocated in mmapped regions (bytes) * / |
| 314 | * size_t usmblks; / * Maximum total allocated space (bytes) * / |
| 315 | * size_t fsmblks; / * Space in freed fastbin blocks (bytes) * / |
| 316 | * size_t uordblks; / * Total allocated space (bytes) * / |
| 317 | * size_t fordblks; / * Total free space (bytes) * / |
| 318 | * size_t keepcost; / * Top-most, releasable space (bytes) * / |
| 319 | * |
| 320 | */ |
| 321 | |
| 322 | u8 * |
| 323 | format_msize (u8 * s, va_list * va) |
| 324 | { |
| 325 | uword a = va_arg (*va, uword); |
| 326 | |
| 327 | if (a >= 1ULL << 30) |
| 328 | s = format (s, "%.2fG", (((f64) a) / ((f64) (1ULL << 30)))); |
| 329 | else if (a >= 1ULL << 20) |
| 330 | s = format (s, "%.2fM", (((f64) a) / ((f64) (1ULL << 20)))); |
| 331 | else if (a >= 1ULL << 10) |
| 332 | s = format (s, "%.2fK", (((f64) a) / ((f64) (1ULL << 10)))); |
| 333 | else |
| 334 | s = format (s, "%lld", a); |
| 335 | return s; |
| 336 | } |
| 337 | |
| 338 | static int |
| 339 | mheap_trace_sort (const void *_t1, const void *_t2) |
| 340 | { |
| 341 | const mheap_trace_t *t1 = _t1; |
| 342 | const mheap_trace_t *t2 = _t2; |
| 343 | word cmp; |
| 344 | |
| 345 | cmp = (word) t2->n_bytes - (word) t1->n_bytes; |
| 346 | if (!cmp) |
| 347 | cmp = (word) t2->n_allocations - (word) t1->n_allocations; |
| 348 | return cmp; |
| 349 | } |
| 350 | |
| 351 | u8 * |
| 352 | format_mheap_trace (u8 * s, va_list * va) |
| 353 | { |
| 354 | mheap_trace_main_t *tm = va_arg (*va, mheap_trace_main_t *); |
| 355 | int verbose = va_arg (*va, int); |
| 356 | int have_traces = 0; |
| 357 | int i; |
| 358 | |
| 359 | clib_spinlock_lock (&tm->lock); |
Dave Barach | d67a428 | 2019-06-15 12:46:13 -0400 | [diff] [blame] | 360 | if (vec_len (tm->traces) > 0 && |
| 361 | clib_mem_get_heap () == tm->current_traced_mheap) |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 362 | { |
| 363 | have_traces = 1; |
| 364 | |
| 365 | /* Make a copy of traces since we'll be sorting them. */ |
| 366 | mheap_trace_t *t, *traces_copy; |
| 367 | u32 indent, total_objects_traced; |
| 368 | |
| 369 | traces_copy = vec_dup (tm->traces); |
| 370 | |
| 371 | qsort (traces_copy, vec_len (traces_copy), sizeof (traces_copy[0]), |
| 372 | mheap_trace_sort); |
| 373 | |
| 374 | total_objects_traced = 0; |
| 375 | s = format (s, "\n"); |
| 376 | vec_foreach (t, traces_copy) |
| 377 | { |
| 378 | /* Skip over free elements. */ |
| 379 | if (t->n_allocations == 0) |
| 380 | continue; |
| 381 | |
| 382 | total_objects_traced += t->n_allocations; |
| 383 | |
| 384 | /* When not verbose only report allocations of more than 1k. */ |
| 385 | if (!verbose && t->n_bytes < 1024) |
| 386 | continue; |
| 387 | |
| 388 | if (t == traces_copy) |
| 389 | s = format (s, "%=9s%=9s %=10s Traceback\n", "Bytes", "Count", |
| 390 | "Sample"); |
| 391 | s = format (s, "%9d%9d %p", t->n_bytes, t->n_allocations, t->offset); |
| 392 | indent = format_get_indent (s); |
| 393 | for (i = 0; i < ARRAY_LEN (t->callers) && t->callers[i]; i++) |
| 394 | { |
| 395 | if (i > 0) |
| 396 | s = format (s, "%U", format_white_space, indent); |
Damjan Marion | 4dffd1c | 2018-09-03 12:30:36 +0200 | [diff] [blame] | 397 | #if defined(CLIB_UNIX) && !defined(__APPLE__) |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 398 | /* $$$$ does this actually work? */ |
| 399 | s = |
| 400 | format (s, " %U\n", format_clib_elf_symbol_with_address, |
| 401 | t->callers[i]); |
| 402 | #else |
| 403 | s = format (s, " %p\n", t->callers[i]); |
| 404 | #endif |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | s = format (s, "%d total traced objects\n", total_objects_traced); |
| 409 | |
| 410 | vec_free (traces_copy); |
| 411 | } |
| 412 | clib_spinlock_unlock (&tm->lock); |
| 413 | if (have_traces == 0) |
| 414 | s = format (s, "no traced allocations\n"); |
| 415 | |
| 416 | return s; |
| 417 | } |
| 418 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 419 | __clib_export u8 * |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 420 | format_clib_mem_heap (u8 * s, va_list * va) |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 421 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 422 | clib_mem_heap_t *heap = va_arg (*va, clib_mem_heap_t *); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 423 | int verbose = va_arg (*va, int); |
Dave Barach | af7dd5b | 2018-08-23 17:08:44 -0400 | [diff] [blame] | 424 | struct dlmallinfo mi; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 425 | mheap_trace_main_t *tm = &mheap_trace_main; |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 426 | u32 indent = format_get_indent (s) + 2; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 427 | |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 428 | if (heap == 0) |
| 429 | heap = clib_mem_get_heap (); |
| 430 | |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 431 | mi = mspace_mallinfo (heap->mspace); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 432 | |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 433 | s = format (s, "base %p, size %U", |
| 434 | heap->base, format_memory_size, heap->size); |
| 435 | |
| 436 | #define _(i,v,str) \ |
| 437 | if (heap->flags & CLIB_MEM_HEAP_F_##v) s = format (s, ", %s", str); |
| 438 | foreach_clib_mem_heap_flag; |
| 439 | #undef _ |
| 440 | |
| 441 | s = format (s, ", name '%s'", heap->name); |
| 442 | |
| 443 | if (heap->log2_page_sz != CLIB_MEM_PAGE_SZ_UNKNOWN) |
| 444 | { |
| 445 | clib_mem_page_stats_t stats; |
| 446 | clib_mem_get_page_stats (heap->base, heap->log2_page_sz, |
| 447 | heap->size >> heap->log2_page_sz, &stats); |
| 448 | s = format (s, "\n%U%U", format_white_space, indent, |
| 449 | format_clib_mem_page_stats, &stats); |
| 450 | } |
| 451 | |
| 452 | s = format (s, "\n%Utotal: %U, used: %U, free: %U, trimmable: %U", |
| 453 | format_white_space, indent, |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 454 | format_msize, mi.arena, |
| 455 | format_msize, mi.uordblks, |
| 456 | format_msize, mi.fordblks, format_msize, mi.keepcost); |
| 457 | if (verbose > 0) |
| 458 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 459 | s = format (s, "\n%Ufree chunks %llu free fastbin blks %llu", |
| 460 | format_white_space, indent + 2, mi.ordblks, mi.smblks); |
| 461 | s = format (s, "\n%Umax total allocated %U", |
| 462 | format_white_space, indent + 2, format_msize, mi.usmblks); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 463 | } |
| 464 | |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 465 | if (heap->flags & CLIB_MEM_HEAP_F_TRACED) |
Dave Barach | d67a428 | 2019-06-15 12:46:13 -0400 | [diff] [blame] | 466 | s = format (s, "\n%U", format_mheap_trace, tm, verbose); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 467 | return s; |
| 468 | } |
| 469 | |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 470 | __clib_export __clib_flatten void |
| 471 | clib_mem_get_heap_usage (clib_mem_heap_t *heap, clib_mem_usage_t *usage) |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 472 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 473 | struct dlmallinfo mi = mspace_mallinfo (heap->mspace); |
Ole Troan | 92e3082 | 2019-06-16 12:33:51 +0200 | [diff] [blame] | 474 | |
Ole Troan | a606d92 | 2021-05-05 09:23:17 +0200 | [diff] [blame] | 475 | usage->bytes_total = mi.arena; /* non-mmapped space allocated from system */ |
| 476 | usage->bytes_used = mi.uordblks; /* total allocated space */ |
| 477 | usage->bytes_free = mi.fordblks; /* total free space */ |
| 478 | usage->bytes_used_mmap = mi.hblkhd; /* space in mmapped regions */ |
| 479 | usage->bytes_max = mi.usmblks; /* maximum total allocated space */ |
| 480 | usage->bytes_free_reclaimed = mi.ordblks; /* number of free chunks */ |
| 481 | usage->bytes_overhead = mi.keepcost; /* releasable (via malloc_trim) space */ |
| 482 | |
| 483 | /* Not supported */ |
| 484 | usage->bytes_used_sbrk = 0; |
Ole Troan | 92e3082 | 2019-06-16 12:33:51 +0200 | [diff] [blame] | 485 | usage->object_count = 0; |
Ole Troan | 92e3082 | 2019-06-16 12:33:51 +0200 | [diff] [blame] | 486 | } |
| 487 | |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 488 | /* Call serial number for debugger breakpoints. */ |
| 489 | uword clib_mem_validate_serial = 0; |
| 490 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 491 | __clib_export void |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 492 | mheap_trace (clib_mem_heap_t * h, int enable) |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 493 | { |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 494 | if (enable) |
| 495 | h->flags |= CLIB_MEM_HEAP_F_TRACED; |
| 496 | else |
| 497 | h->flags &= ~CLIB_MEM_HEAP_F_TRACED; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 498 | |
| 499 | if (enable == 0) |
| 500 | mheap_trace_main_free (&mheap_trace_main); |
| 501 | } |
| 502 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 503 | __clib_export void |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 504 | clib_mem_trace (int enable) |
| 505 | { |
| 506 | mheap_trace_main_t *tm = &mheap_trace_main; |
Dave Barach | d67a428 | 2019-06-15 12:46:13 -0400 | [diff] [blame] | 507 | void *current_heap = clib_mem_get_heap (); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 508 | |
| 509 | tm->enabled = enable; |
Dave Barach | d67a428 | 2019-06-15 12:46:13 -0400 | [diff] [blame] | 510 | mheap_trace (current_heap, enable); |
| 511 | |
| 512 | if (enable) |
| 513 | tm->current_traced_mheap = current_heap; |
| 514 | else |
| 515 | tm->current_traced_mheap = 0; |
| 516 | } |
| 517 | |
| 518 | int |
| 519 | clib_mem_is_traced (void) |
| 520 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 521 | clib_mem_heap_t *h = clib_mem_get_heap (); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 522 | return (h->flags &= CLIB_MEM_HEAP_F_TRACED) != 0; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 523 | } |
| 524 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 525 | __clib_export uword |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 526 | clib_mem_trace_enable_disable (uword enable) |
| 527 | { |
| 528 | uword rv; |
| 529 | mheap_trace_main_t *tm = &mheap_trace_main; |
| 530 | |
| 531 | rv = tm->enabled; |
| 532 | tm->enabled = enable; |
| 533 | return rv; |
| 534 | } |
| 535 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 536 | __clib_export clib_mem_heap_t * |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 537 | clib_mem_create_heap (void *base, uword size, int is_locked, char *fmt, ...) |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 538 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 539 | clib_mem_page_sz_t log2_page_sz = clib_mem_get_log2_page_size (); |
| 540 | clib_mem_heap_t *h; |
| 541 | char *name; |
| 542 | u8 *s = 0; |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 543 | |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 544 | if (fmt == 0) |
| 545 | { |
| 546 | name = ""; |
| 547 | } |
| 548 | else if (strchr (fmt, '%')) |
| 549 | { |
| 550 | va_list va; |
| 551 | va_start (va, fmt); |
| 552 | s = va_format (0, fmt, &va); |
| 553 | vec_add1 (s, 0); |
| 554 | va_end (va); |
| 555 | name = (char *) s; |
| 556 | } |
| 557 | else |
| 558 | name = fmt; |
| 559 | |
| 560 | h = clib_mem_create_heap_internal (base, size, log2_page_sz, is_locked, |
| 561 | name); |
| 562 | vec_free (s); |
| 563 | return h; |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 564 | } |
| 565 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 566 | __clib_export void |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 567 | clib_mem_destroy_heap (clib_mem_heap_t * h) |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 568 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 569 | mheap_trace_main_t *tm = &mheap_trace_main; |
| 570 | |
| 571 | if (tm->enabled && h->mspace == tm->current_traced_mheap) |
| 572 | tm->enabled = 0; |
| 573 | |
| 574 | destroy_mspace (h->mspace); |
| 575 | if (h->flags & CLIB_MEM_HEAP_F_UNMAP_ON_DESTROY) |
| 576 | clib_mem_vm_unmap (h->base); |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 577 | } |
| 578 | |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 579 | __clib_export __clib_flatten uword |
| 580 | clib_mem_get_heap_free_space (clib_mem_heap_t *h) |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 581 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 582 | struct dlmallinfo dlminfo = mspace_mallinfo (h->mspace); |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 583 | return dlminfo.fordblks; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 584 | } |
| 585 | |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 586 | __clib_export __clib_flatten void * |
| 587 | clib_mem_get_heap_base (clib_mem_heap_t *h) |
Damjan Marion | bdbb0c5 | 2020-09-17 10:40:44 +0200 | [diff] [blame] | 588 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 589 | return h->base; |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 590 | } |
Damjan Marion | bdbb0c5 | 2020-09-17 10:40:44 +0200 | [diff] [blame] | 591 | |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 592 | __clib_export __clib_flatten uword |
| 593 | clib_mem_get_heap_size (clib_mem_heap_t *heap) |
Damjan Marion | 4537c30 | 2020-09-28 19:03:37 +0200 | [diff] [blame] | 594 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 595 | return heap->size; |
Damjan Marion | bdbb0c5 | 2020-09-17 10:40:44 +0200 | [diff] [blame] | 596 | } |
| 597 | |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 598 | /* Memory allocator which may call os_out_of_memory() if it fails */ |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 599 | static inline void * |
| 600 | clib_mem_heap_alloc_inline (void *heap, uword size, uword align, |
| 601 | int os_out_of_memory_on_failure) |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 602 | { |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 603 | clib_mem_heap_t *h = heap ? heap : clib_mem_get_per_cpu_heap (); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 604 | void *p; |
| 605 | |
| 606 | align = clib_max (CLIB_MEM_MIN_ALIGN, align); |
| 607 | |
| 608 | p = mspace_memalign (h->mspace, align, size); |
| 609 | |
| 610 | if (PREDICT_FALSE (0 == p)) |
| 611 | { |
| 612 | if (os_out_of_memory_on_failure) |
| 613 | os_out_of_memory (); |
| 614 | return 0; |
| 615 | } |
| 616 | |
| 617 | if (PREDICT_FALSE (h->flags & CLIB_MEM_HEAP_F_TRACED)) |
| 618 | mheap_get_trace (pointer_to_uword (p), clib_mem_size (p)); |
| 619 | |
Damjan Marion | 79934e8 | 2022-04-05 12:40:31 +0200 | [diff] [blame] | 620 | clib_mem_unpoison (p, size); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 621 | return p; |
| 622 | } |
| 623 | |
| 624 | /* Memory allocator which calls os_out_of_memory() when it fails */ |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 625 | __clib_export __clib_flatten void * |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 626 | clib_mem_alloc (uword size) |
| 627 | { |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 628 | return clib_mem_heap_alloc_inline (0, size, CLIB_MEM_MIN_ALIGN, |
| 629 | /* os_out_of_memory */ 1); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 630 | } |
| 631 | |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 632 | __clib_export __clib_flatten void * |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 633 | clib_mem_alloc_aligned (uword size, uword align) |
| 634 | { |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 635 | return clib_mem_heap_alloc_inline (0, size, align, |
| 636 | /* os_out_of_memory */ 1); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | /* Memory allocator which calls os_out_of_memory() when it fails */ |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 640 | __clib_export __clib_flatten void * |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 641 | clib_mem_alloc_or_null (uword size) |
| 642 | { |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 643 | return clib_mem_heap_alloc_inline (0, size, CLIB_MEM_MIN_ALIGN, |
| 644 | /* os_out_of_memory */ 0); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 645 | } |
| 646 | |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 647 | __clib_export __clib_flatten void * |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 648 | clib_mem_alloc_aligned_or_null (uword size, uword align) |
| 649 | { |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 650 | return clib_mem_heap_alloc_inline (0, size, align, |
| 651 | /* os_out_of_memory */ 0); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 652 | } |
| 653 | |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 654 | __clib_export __clib_flatten void * |
| 655 | clib_mem_heap_alloc (void *heap, uword size) |
| 656 | { |
| 657 | return clib_mem_heap_alloc_inline (heap, size, CLIB_MEM_MIN_ALIGN, |
| 658 | /* os_out_of_memory */ 1); |
| 659 | } |
| 660 | |
| 661 | __clib_export __clib_flatten void * |
| 662 | clib_mem_heap_alloc_aligned (void *heap, uword size, uword align) |
| 663 | { |
| 664 | return clib_mem_heap_alloc_inline (heap, size, align, |
| 665 | /* os_out_of_memory */ 1); |
| 666 | } |
| 667 | |
| 668 | __clib_export __clib_flatten void * |
| 669 | clib_mem_heap_alloc_or_null (void *heap, uword size) |
| 670 | { |
| 671 | return clib_mem_heap_alloc_inline (heap, size, CLIB_MEM_MIN_ALIGN, |
| 672 | /* os_out_of_memory */ 0); |
| 673 | } |
| 674 | |
| 675 | __clib_export __clib_flatten void * |
| 676 | clib_mem_heap_alloc_aligned_or_null (void *heap, uword size, uword align) |
| 677 | { |
| 678 | return clib_mem_heap_alloc_inline (heap, size, align, |
| 679 | /* os_out_of_memory */ 0); |
| 680 | } |
| 681 | |
| 682 | __clib_export __clib_flatten void * |
| 683 | clib_mem_heap_realloc_aligned (void *heap, void *p, uword new_size, |
| 684 | uword align) |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 685 | { |
| 686 | uword old_alloc_size; |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 687 | clib_mem_heap_t *h = heap ? heap : clib_mem_get_per_cpu_heap (); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 688 | void *new; |
| 689 | |
| 690 | ASSERT (count_set_bits (align) == 1); |
| 691 | |
| 692 | old_alloc_size = p ? mspace_usable_size (p) : 0; |
| 693 | |
| 694 | if (new_size == old_alloc_size) |
| 695 | return p; |
| 696 | |
| 697 | if (p && pointer_is_aligned (p, align) && |
| 698 | mspace_realloc_in_place (h->mspace, p, new_size)) |
| 699 | { |
Damjan Marion | 79934e8 | 2022-04-05 12:40:31 +0200 | [diff] [blame] | 700 | clib_mem_unpoison (p, new_size); |
Leung Lai Yung | 69be089 | 2022-05-22 13:25:53 +0000 | [diff] [blame^] | 701 | if (PREDICT_FALSE (h->flags & CLIB_MEM_HEAP_F_TRACED)) |
| 702 | { |
| 703 | mheap_put_trace (pointer_to_uword (p), old_alloc_size); |
| 704 | mheap_get_trace (pointer_to_uword (p), clib_mem_size (p)); |
| 705 | } |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 706 | } |
| 707 | else |
| 708 | { |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 709 | new = clib_mem_heap_alloc_inline (h, new_size, align, 1); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 710 | |
Damjan Marion | 79934e8 | 2022-04-05 12:40:31 +0200 | [diff] [blame] | 711 | clib_mem_unpoison (new, new_size); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 712 | if (old_alloc_size) |
| 713 | { |
Damjan Marion | 79934e8 | 2022-04-05 12:40:31 +0200 | [diff] [blame] | 714 | clib_mem_unpoison (p, old_alloc_size); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 715 | clib_memcpy_fast (new, p, clib_min (new_size, old_alloc_size)); |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 716 | clib_mem_heap_free (h, p); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 717 | } |
| 718 | p = new; |
| 719 | } |
| 720 | |
| 721 | return p; |
| 722 | } |
| 723 | |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 724 | __clib_export __clib_flatten void * |
| 725 | clib_mem_heap_realloc (void *heap, void *p, uword new_size) |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 726 | { |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 727 | return clib_mem_heap_realloc_aligned (heap, p, new_size, CLIB_MEM_MIN_ALIGN); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 728 | } |
| 729 | |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 730 | __clib_export __clib_flatten void * |
| 731 | clib_mem_realloc_aligned (void *p, uword new_size, uword align) |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 732 | { |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 733 | return clib_mem_heap_realloc_aligned (0, p, new_size, align); |
| 734 | } |
| 735 | |
| 736 | __clib_export __clib_flatten void * |
| 737 | clib_mem_realloc (void *p, uword new_size) |
| 738 | { |
| 739 | return clib_mem_heap_realloc_aligned (0, p, new_size, CLIB_MEM_MIN_ALIGN); |
| 740 | } |
| 741 | |
| 742 | __clib_export __clib_flatten uword |
| 743 | clib_mem_heap_is_heap_object (void *heap, void *p) |
| 744 | { |
| 745 | clib_mem_heap_t *h = heap ? heap : clib_mem_get_per_cpu_heap (); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 746 | return mspace_is_heap_object (h->mspace, p); |
| 747 | } |
| 748 | |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 749 | __clib_export __clib_flatten uword |
| 750 | clib_mem_is_heap_object (void *p) |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 751 | { |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 752 | return clib_mem_heap_is_heap_object (0, p); |
| 753 | } |
| 754 | |
| 755 | __clib_export __clib_flatten void |
| 756 | clib_mem_heap_free (void *heap, void *p) |
| 757 | { |
| 758 | clib_mem_heap_t *h = heap ? heap : clib_mem_get_per_cpu_heap (); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 759 | uword size = clib_mem_size (p); |
| 760 | |
| 761 | /* Make sure object is in the correct heap. */ |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 762 | ASSERT (clib_mem_heap_is_heap_object (h, p)); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 763 | |
| 764 | if (PREDICT_FALSE (h->flags & CLIB_MEM_HEAP_F_TRACED)) |
| 765 | mheap_put_trace (pointer_to_uword (p), size); |
Damjan Marion | 79934e8 | 2022-04-05 12:40:31 +0200 | [diff] [blame] | 766 | clib_mem_poison (p, clib_mem_size (p)); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 767 | |
| 768 | mspace_free (h->mspace, p); |
| 769 | } |
| 770 | |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 771 | __clib_export __clib_flatten void |
| 772 | clib_mem_free (void *p) |
| 773 | { |
| 774 | clib_mem_heap_free (0, p); |
| 775 | } |
| 776 | |
| 777 | __clib_export __clib_flatten uword |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 778 | clib_mem_size (void *p) |
| 779 | { |
| 780 | return mspace_usable_size (p); |
| 781 | } |
| 782 | |
| 783 | __clib_export void |
| 784 | clib_mem_free_s (void *p) |
| 785 | { |
| 786 | uword size = clib_mem_size (p); |
Damjan Marion | 79934e8 | 2022-04-05 12:40:31 +0200 | [diff] [blame] | 787 | clib_mem_unpoison (p, size); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 788 | memset_s_inline (p, size, 0, size); |
| 789 | clib_mem_free (p); |
| 790 | } |