2 * Copyright (c) 2020 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 #include <vppinfra/clib.h>
17 #include <vppinfra/mem.h>
18 #include <vppinfra/time.h>
19 #include <vppinfra/format.h>
20 #include <vppinfra/clib_error.h>
22 /* while usage of dlmalloc APIs is genrally discouraged, in this particular
23 * case there is significant benefit of calling them directly due to
24 * smaller memory consuption (no wwp and headroom space) */
25 #include <vppinfra/dlmalloc.h>
27 #define CLIB_MEM_BULK_DEFAULT_MIN_ELTS_PER_CHUNK 32
29 typedef struct clib_mem_bulk_chunk_hdr
33 struct clib_mem_bulk_chunk_hdr *prev, *next;
34 } clib_mem_bulk_chunk_hdr_t;
44 clib_mem_bulk_chunk_hdr_t *full_chunks, *avail_chunks;
47 __clib_export clib_mem_bulk_handle_t
48 clib_mem_bulk_init (u32 elt_sz, u32 align, u32 min_elts_per_chunk)
50 clib_mem_heap_t *heap = clib_mem_get_heap ();
54 if ((b = mspace_memalign (heap->mspace, 16, sizeof (clib_mem_bulk_t))) == 0)
60 if (min_elts_per_chunk == 0)
61 min_elts_per_chunk = CLIB_MEM_BULK_DEFAULT_MIN_ELTS_PER_CHUNK;
63 clib_memset (b, 0, sizeof (clib_mem_bulk_t));
64 b->mspace = heap->mspace;
66 b->elt_sz = round_pow2 (elt_sz, align);
67 b->chunk_hdr_sz = round_pow2 (sizeof (clib_mem_bulk_chunk_hdr_t), align);
68 b->elts_per_chunk = min_elts_per_chunk;
69 sz = b->elts_per_chunk * b->elt_sz + b->chunk_hdr_sz;
70 b->chunk_align = max_pow2 (sz);
71 b->elts_per_chunk += (b->chunk_align - sz) / b->elt_sz;
76 clib_mem_bulk_destroy (clib_mem_bulk_handle_t h)
78 clib_mem_bulk_t *b = h;
79 clib_mem_bulk_chunk_hdr_t *c, *next;
87 mspace_free (b->mspace, c);
98 mspace_free (b->mspace, b);
102 get_chunk_elt_ptr (clib_mem_bulk_t *b, clib_mem_bulk_chunk_hdr_t *c, u32 index)
104 return (u8 *) c + b->chunk_hdr_sz + index * b->elt_sz;
108 add_to_chunk_list (clib_mem_bulk_chunk_hdr_t **first,
109 clib_mem_bulk_chunk_hdr_t *c)
119 remove_from_chunk_list (clib_mem_bulk_chunk_hdr_t **first,
120 clib_mem_bulk_chunk_hdr_t *c)
123 c->next->prev = c->prev;
125 c->prev->next = c->next;
131 clib_mem_bulk_alloc (clib_mem_bulk_handle_t h)
133 clib_mem_bulk_t *b = h;
134 clib_mem_bulk_chunk_hdr_t *c = b->avail_chunks;
137 if (b->avail_chunks == 0)
139 u32 i, sz = b->chunk_hdr_sz + b->elts_per_chunk * b->elt_sz;
140 c = mspace_memalign (b->mspace, b->chunk_align, sz);
141 clib_memset (c, 0, sizeof (clib_mem_bulk_chunk_hdr_t));
143 c->n_free = b->elts_per_chunk;
145 /* populate freelist */
146 for (i = 0; i < b->elts_per_chunk - 1; i++)
147 *((u32 *) get_chunk_elt_ptr (b, c, i)) = i + 1;
148 *((u32 *) get_chunk_elt_ptr (b, c, i)) = ~0;
151 ASSERT (c->freelist != ~0);
152 elt_idx = c->freelist;
153 c->freelist = *((u32 *) get_chunk_elt_ptr (b, c, elt_idx));
159 ASSERT (c->freelist == ~0);
160 remove_from_chunk_list (&b->avail_chunks, c);
161 add_to_chunk_list (&b->full_chunks, c);
164 return get_chunk_elt_ptr (b, c, elt_idx);
168 clib_mem_bulk_free (clib_mem_bulk_handle_t h, void *p)
170 clib_mem_bulk_t *b = h;
171 uword offset = (uword) p & (b->chunk_align - 1);
172 clib_mem_bulk_chunk_hdr_t *c = (void *) ((u8 *) p - offset);
173 u32 elt_idx = (offset - b->chunk_hdr_sz) / b->elt_sz;
175 ASSERT (elt_idx < b->elts_per_chunk);
176 ASSERT (get_chunk_elt_ptr (b, c, elt_idx) == p);
180 if (c->n_free == b->elts_per_chunk)
182 /* chunk is empty - give it back */
183 remove_from_chunk_list (&b->avail_chunks, c);
184 mspace_free (b->mspace, c);
190 /* move chunk to avail chunks */
191 remove_from_chunk_list (&b->full_chunks, c);
192 add_to_chunk_list (&b->avail_chunks, c);
195 /* add elt to freelist */
196 *(u32 *) p = c->freelist;
197 c->freelist = elt_idx;
201 format_clib_mem_bulk (u8 *s, va_list *args)
203 clib_mem_bulk_t *b = va_arg (*args, clib_mem_bulk_handle_t);
204 clib_mem_bulk_chunk_hdr_t *c;
205 uword n_chunks = 0, n_free_elts = 0, n_elts, chunk_sz;
218 n_free_elts += c->n_free;
222 n_elts = n_chunks * b->elts_per_chunk;
223 chunk_sz = b->chunk_hdr_sz + b->elts_per_chunk * b->elt_sz;
225 s = format (s, "%u bytes/elt, align %u, chunk-align %u, ", b->elt_sz,
226 b->align, b->chunk_align);
227 s = format (s, "%u elts-per-chunk, chunk size %lu bytes", b->elts_per_chunk,
231 return format (s, "\nempty");
233 s = format (s, "\n%lu chunks allocated, ", n_chunks);
234 s = format (s, "%lu / %lu free elts (%.1f%%), ", n_free_elts, n_elts,
235 (f64) n_free_elts * 100 / n_elts);
236 s = format (s, "%lu bytes of memory consumed", n_chunks * chunk_sz);