blob: e1c89e0a5fa7b1c9deefbd28c71d97fa40dd0f9c [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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 Copyright (c) 2001, 2002, 2003, 2004 Eliot Dresselhaus
17
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:
25
26 The above copyright notice and this permission notice shall be
27 included in all copies or substantial portions of the Software.
28
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.
36*/
Chris Luke8e5b0412016-07-26 13:06:10 -040037/** @file
38 * @brief Fixed length block allocator.
39 Pools are built from clib vectors and bitmaps. Use pools when
40 repeatedly allocating and freeing fixed-size data. Pools are
41 fast, and avoid memory fragmentation.
42 */
Ed Warnickecb9cada2015-12-08 15:45:58 -070043
44#ifndef included_pool_h
45#define included_pool_h
46
47#include <vppinfra/bitmap.h>
48#include <vppinfra/error.h>
49#include <vppinfra/mheap.h>
50
Ed Warnickecb9cada2015-12-08 15:45:58 -070051
Dave Barachc3799992016-08-15 11:12:27 -040052typedef struct
53{
Chris Luke8e5b0412016-07-26 13:06:10 -040054 /** Bitmap of indices of free objects. */
Dave Barachc3799992016-08-15 11:12:27 -040055 uword *free_bitmap;
Ed Warnickecb9cada2015-12-08 15:45:58 -070056
Chris Luke8e5b0412016-07-26 13:06:10 -040057 /** Vector of free indices. One element for each set bit in bitmap. */
Dave Barachc3799992016-08-15 11:12:27 -040058 u32 *free_indices;
Ed Warnickecb9cada2015-12-08 15:45:58 -070059} pool_header_t;
60
Chris Luke8e5b0412016-07-26 13:06:10 -040061/** Align pool header so that pointers are naturally aligned. */
Ed Warnickecb9cada2015-12-08 15:45:58 -070062#define pool_aligned_header_bytes \
63 vec_aligned_header_bytes (sizeof (pool_header_t), sizeof (void *))
64
Chris Luke8e5b0412016-07-26 13:06:10 -040065/** Get pool header from user pool pointer */
Dave Barachc3799992016-08-15 11:12:27 -040066always_inline pool_header_t *
67pool_header (void *v)
68{
69 return vec_aligned_header (v, sizeof (pool_header_t), sizeof (void *));
70}
Ed Warnickecb9cada2015-12-08 15:45:58 -070071
Chris Luke8e5b0412016-07-26 13:06:10 -040072/** Validate a pool */
Dave Barachc3799992016-08-15 11:12:27 -040073always_inline void
74pool_validate (void *v)
Ed Warnickecb9cada2015-12-08 15:45:58 -070075{
Dave Barachc3799992016-08-15 11:12:27 -040076 pool_header_t *p = pool_header (v);
Ed Warnickecb9cada2015-12-08 15:45:58 -070077 uword i, n_free_bitmap;
78
Dave Barachc3799992016-08-15 11:12:27 -040079 if (!v)
Ed Warnickecb9cada2015-12-08 15:45:58 -070080 return;
81
82 n_free_bitmap = clib_bitmap_count_set_bits (p->free_bitmap);
83 ASSERT (n_free_bitmap == vec_len (p->free_indices));
84 for (i = 0; i < vec_len (p->free_indices); i++)
85 ASSERT (clib_bitmap_get (p->free_bitmap, p->free_indices[i]) == 1);
86}
87
Dave Barachc3799992016-08-15 11:12:27 -040088always_inline void
89pool_header_validate_index (void *v, uword index)
Ed Warnickecb9cada2015-12-08 15:45:58 -070090{
Dave Barachc3799992016-08-15 11:12:27 -040091 pool_header_t *p = pool_header (v);
Ed Warnickecb9cada2015-12-08 15:45:58 -070092
93 if (v)
94 vec_validate (p->free_bitmap, index / BITS (uword));
95}
96
97#define pool_validate_index(v,i) \
98do { \
99 uword __pool_validate_index = (i); \
100 vec_validate_ha ((v), __pool_validate_index, \
101 pool_aligned_header_bytes, /* align */ 0); \
102 pool_header_validate_index ((v), __pool_validate_index); \
103} while (0)
104
Chris Luke8e5b0412016-07-26 13:06:10 -0400105/** Number of active elements in a pool.
106 * @return Number of active elements in a pool
107 */
Dave Barachc3799992016-08-15 11:12:27 -0400108always_inline uword
109pool_elts (void *v)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700110{
111 uword ret = vec_len (v);
112 if (v)
113 ret -= vec_len (pool_header (v)->free_indices);
114 return ret;
115}
116
Chris Luke8e5b0412016-07-26 13:06:10 -0400117/** Number of elements in pool vector.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700118
Chris Luke8e5b0412016-07-26 13:06:10 -0400119 @note You probably want to call pool_elts() instead.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700120*/
121#define pool_len(p) vec_len(p)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122
Chris Luke8e5b0412016-07-26 13:06:10 -0400123/** Number of elements in pool vector (usable as an lvalue)
124
125 @note You probably don't want to use this macro.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700126*/
127#define _pool_len(p) _vec_len(p)
128
Chris Luke8e5b0412016-07-26 13:06:10 -0400129/** Memory usage of pool header. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700130always_inline uword
Dave Barachc3799992016-08-15 11:12:27 -0400131pool_header_bytes (void *v)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700132{
Dave Barachc3799992016-08-15 11:12:27 -0400133 pool_header_t *p = pool_header (v);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134
Dave Barachc3799992016-08-15 11:12:27 -0400135 if (!v)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700136 return 0;
137
138 return vec_bytes (p->free_bitmap) + vec_bytes (p->free_indices);
139}
140
Chris Luke8e5b0412016-07-26 13:06:10 -0400141/** Memory usage of pool. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700142#define pool_bytes(P) (vec_bytes (P) + pool_header_bytes (P))
143
Chris Luke8e5b0412016-07-26 13:06:10 -0400144/** Local variable naming macro. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700145#define _pool_var(v) _pool_##v
146
Chris Luke8e5b0412016-07-26 13:06:10 -0400147/** Queries whether pool has at least N_FREE free elements. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700148always_inline uword
Dave Barachc3799992016-08-15 11:12:27 -0400149pool_free_elts (void *v)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150{
Dave Barachc3799992016-08-15 11:12:27 -0400151 pool_header_t *p = pool_header (v);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700152 uword n_free = 0;
153
Dave Barachc3799992016-08-15 11:12:27 -0400154 if (v)
155 {
156 n_free += vec_len (p->free_indices);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700157
Dave Barachc3799992016-08-15 11:12:27 -0400158 /* Space left at end of vector? */
159 n_free += vec_capacity (v, sizeof (p[0])) - vec_len (v);
160 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161
162 return n_free;
163}
164
Chris Luke8e5b0412016-07-26 13:06:10 -0400165/** Allocate an object E from a pool P (general version).
Ed Warnickecb9cada2015-12-08 15:45:58 -0700166
Chris Luke8e5b0412016-07-26 13:06:10 -0400167 First search free list. If nothing is free extend vector of objects.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168*/
169#define pool_get_aligned(P,E,A) \
170do { \
171 pool_header_t * _pool_var (p) = pool_header (P); \
172 uword _pool_var (l); \
173 \
174 _pool_var (l) = 0; \
175 if (P) \
176 _pool_var (l) = vec_len (_pool_var (p)->free_indices); \
177 \
178 if (_pool_var (l) > 0) \
179 { \
180 /* Return free element from free list. */ \
181 uword _pool_var (i) = _pool_var (p)->free_indices[_pool_var (l) - 1]; \
182 (E) = (P) + _pool_var (i); \
183 _pool_var (p)->free_bitmap = \
184 clib_bitmap_andnoti (_pool_var (p)->free_bitmap, _pool_var (i)); \
185 _vec_len (_pool_var (p)->free_indices) = _pool_var (l) - 1; \
186 } \
187 else \
188 { \
189 /* Nothing on free list, make a new element and return it. */ \
190 P = _vec_resize (P, \
191 /* length_increment */ 1, \
192 /* new size */ (vec_len (P) + 1) * sizeof (P[0]), \
193 pool_aligned_header_bytes, \
194 /* align */ (A)); \
195 E = vec_end (P) - 1; \
196 } \
197} while (0)
198
Chris Luke8e5b0412016-07-26 13:06:10 -0400199/** Allocate an object E from a pool P (unspecified alignment). */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700200#define pool_get(P,E) pool_get_aligned(P,E,0)
201
Chris Luke8e5b0412016-07-26 13:06:10 -0400202/** Use free bitmap to query whether given element is free. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700203#define pool_is_free(P,E) \
204({ \
205 pool_header_t * _pool_var (p) = pool_header (P); \
206 uword _pool_var (i) = (E) - (P); \
207 (_pool_var (i) < vec_len (P)) ? clib_bitmap_get (_pool_var (p)->free_bitmap, _pool_i) : 1; \
208})
Dave Barachc3799992016-08-15 11:12:27 -0400209
Chris Luke8e5b0412016-07-26 13:06:10 -0400210/** Use free bitmap to query whether given index is free */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700211#define pool_is_free_index(P,I) pool_is_free((P),(P)+(I))
212
Chris Luke8e5b0412016-07-26 13:06:10 -0400213/** Free an object E in pool P. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700214#define pool_put(P,E) \
215do { \
216 pool_header_t * _pool_var (p) = pool_header (P); \
217 uword _pool_var (l) = (E) - (P); \
218 ASSERT (vec_is_member (P, E)); \
219 ASSERT (! pool_is_free (P, E)); \
220 \
221 /* Add element to free bitmap and to free list. */ \
222 _pool_var (p)->free_bitmap = \
223 clib_bitmap_ori (_pool_var (p)->free_bitmap, _pool_var (l)); \
224 vec_add1 (_pool_var (p)->free_indices, _pool_var (l)); \
225} while (0)
226
Chris Luke8e5b0412016-07-26 13:06:10 -0400227/** Free pool element with given index. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700228#define pool_put_index(p,i) \
229do { \
230 typeof (p) _e = (p) + (i); \
231 pool_put (p, _e); \
232} while (0)
233
Chris Luke8e5b0412016-07-26 13:06:10 -0400234/** Allocate N more free elements to pool (general version). */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700235#define pool_alloc_aligned(P,N,A) \
236do { \
237 pool_header_t * _p; \
238 (P) = _vec_resize ((P), 0, (vec_len (P) + (N)) * sizeof (P[0]), \
239 pool_aligned_header_bytes, \
240 (A)); \
241 _p = pool_header (P); \
242 vec_resize (_p->free_indices, (N)); \
243 _vec_len (_p->free_indices) -= (N); \
244} while (0)
245
Chris Luke8e5b0412016-07-26 13:06:10 -0400246/** Allocate N more free elements to pool (unspecified alignment). */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700247#define pool_alloc(P,N) pool_alloc_aligned(P,N,0)
248
Chris Luke8e5b0412016-07-26 13:06:10 -0400249/** Low-level free pool operator (do not call directly). */
Dave Barachc3799992016-08-15 11:12:27 -0400250always_inline void *
251_pool_free (void *v)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700252{
Dave Barachc3799992016-08-15 11:12:27 -0400253 pool_header_t *p = pool_header (v);
254 if (!v)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700255 return v;
256 clib_bitmap_free (p->free_bitmap);
257 vec_free (p->free_indices);
258 vec_free_h (v, pool_aligned_header_bytes);
259 return 0;
260}
261
Chris Luke8e5b0412016-07-26 13:06:10 -0400262/** Free a pool. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700263#define pool_free(p) (p) = _pool_free(p)
264
Chris Luke8e5b0412016-07-26 13:06:10 -0400265/** Optimized iteration through pool.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700266
267 @param LO pointer to first element in chunk
268 @param HI pointer to last element in chunk
269 @param POOL pool to iterate across
270 @param BODY operation to perform
271
Dave Barachc3799992016-08-15 11:12:27 -0400272 Optimized version which assumes that BODY is smart enough to
Ed Warnickecb9cada2015-12-08 15:45:58 -0700273 process multiple (LOW,HI) chunks. See also pool_foreach().
274 */
275#define pool_foreach_region(LO,HI,POOL,BODY) \
276do { \
277 uword _pool_var (i), _pool_var (lo), _pool_var (hi), _pool_var (len); \
278 uword _pool_var (bl), * _pool_var (b); \
279 pool_header_t * _pool_var (p); \
280 \
281 _pool_var (p) = pool_header (POOL); \
282 _pool_var (b) = (POOL) ? _pool_var (p)->free_bitmap : 0; \
283 _pool_var (bl) = vec_len (_pool_var (b)); \
284 _pool_var (len) = vec_len (POOL); \
285 _pool_var (lo) = 0; \
286 \
287 for (_pool_var (i) = 0; \
288 _pool_var (i) <= _pool_var (bl); \
289 _pool_var (i)++) \
290 { \
291 uword _pool_var (m), _pool_var (f); \
292 _pool_var (m) = (_pool_var (i) < _pool_var (bl) \
293 ? _pool_var (b) [_pool_var (i)] \
294 : 1); \
295 while (_pool_var (m) != 0) \
296 { \
297 _pool_var (f) = first_set (_pool_var (m)); \
298 _pool_var (hi) = (_pool_var (i) * BITS (_pool_var (b)[0]) \
299 + min_log2 (_pool_var (f))); \
300 _pool_var (hi) = (_pool_var (i) < _pool_var (bl) \
301 ? _pool_var (hi) : _pool_var (len)); \
302 _pool_var (m) ^= _pool_var (f); \
303 if (_pool_var (hi) > _pool_var (lo)) \
304 { \
305 (LO) = _pool_var (lo); \
306 (HI) = _pool_var (hi); \
307 do { BODY; } while (0); \
308 } \
309 _pool_var (lo) = _pool_var (hi) + 1; \
310 } \
311 } \
312} while (0)
313
Chris Luke8e5b0412016-07-26 13:06:10 -0400314/** Iterate through pool.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700315
Chris Luke8e5b0412016-07-26 13:06:10 -0400316 @param VAR A variable of same type as pool vector to be used as an
317 iterator.
318 @param POOL The pool to iterate across.
319 @param BODY The operation to perform, typically a code block. See
320 the example below.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700321
Chris Luke8e5b0412016-07-26 13:06:10 -0400322 This macro will call @c BODY with each active pool element.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700323
324 It is a bad idea to allocate or free pool element from within
Chris Luke8e5b0412016-07-26 13:06:10 -0400325 @c pool_foreach. Build a vector of indices and dispose of them later.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700326
Chris Luke8e5b0412016-07-26 13:06:10 -0400327
328 @par Example
329 @code{.c}
330 proc_t *procs; // a pool of processes.
331 proc_t *proc; // pointer to one process; used as the iterator.
332
333 pool_foreach (proc, procs, ({
334 if (proc->state != PROC_STATE_RUNNING)
335 continue;
336
337 // check a running proc in some way
338 ...
339 }));
340 @endcode
341
342 @warning Because @c pool_foreach is a macro, syntax errors can be
343 difficult to find inside @c BODY, let alone actual code bugs. One
344 can temporarily split a complex @c pool_foreach into a trivial
345 @c pool_foreach which builds a vector of active indices, and a
346 vec_foreach() (or plain for-loop) to walk the active index vector.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700347 */
348#define pool_foreach(VAR,POOL,BODY) \
349do { \
350 uword _pool_foreach_lo, _pool_foreach_hi; \
351 pool_foreach_region (_pool_foreach_lo, _pool_foreach_hi, (POOL), \
352 ({ \
353 for ((VAR) = (POOL) + _pool_foreach_lo; \
354 (VAR) < (POOL) + _pool_foreach_hi; \
355 (VAR)++) \
356 do { BODY; } while (0); \
357 })); \
358} while (0)
359
Chris Luke8e5b0412016-07-26 13:06:10 -0400360/** Returns pointer to element at given index.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700361
Chris Luke8e5b0412016-07-26 13:06:10 -0400362 ASSERTs that the supplied index is valid.
363 Even though one can write correct code of the form
364 @code
365 p = pool_base + index;
366 @endcode
Dave Barachc3799992016-08-15 11:12:27 -0400367 use of @c pool_elt_at_index is strongly suggested.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700368 */
369#define pool_elt_at_index(p,i) \
370({ \
371 typeof (p) _e = (p) + (i); \
372 ASSERT (! pool_is_free (p, _e)); \
373 _e; \
374})
375
Chris Luke8e5b0412016-07-26 13:06:10 -0400376/** Return next occupied pool index after @c i, useful for safe iteration. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700377#define pool_next_index(P,I) \
378({ \
379 pool_header_t * _pool_var (p) = pool_header (P); \
380 uword _pool_var (rv) = (I) + 1; \
381 \
382 _pool_var(rv) = \
383 (_pool_var (rv) < vec_len (P) ? \
384 clib_bitmap_next_clear (_pool_var (p)->free_bitmap, _pool_var(rv)) \
385 : ~0); \
386 _pool_var(rv); \
387})
388
Chris Luke8e5b0412016-07-26 13:06:10 -0400389/** Iterate pool by index. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700390#define pool_foreach_index(i,v,body) \
391 for ((i) = 0; (i) < vec_len (v); (i)++) \
392 { \
393 if (! pool_is_free_index ((v), (i))) \
394 do { body; } while (0); \
395 }
396
397#endif /* included_pool_h */
Dave Barachc3799992016-08-15 11:12:27 -0400398
399/*
400 * fd.io coding-style-patch-verification: ON
401 *
402 * Local Variables:
403 * eval: (c-set-style "gnu")
404 * End:
405 */