ethernet: add sanity checks to p2p_ethernet_add/del
[vpp.git] / src / vppinfra / valloc.h
1 /*
2  * Copyright (c) 2018 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 #ifndef included_valloc_h
17 #define included_valloc_h
18 #include <vppinfra/clib.h>
19 #include <vppinfra/pool.h>
20 #include <vppinfra/bitmap.h>
21 #include <vppinfra/lock.h>
22 #include <vppinfra/hash.h>
23
24 /** @file
25     @brief Simple first-fit virtual space allocator
26 */
27
28 typedef struct
29 {
30   u32 next;                     /**< next chunk pool index */
31   u32 prev;                     /**< previous chunk pool index */
32   uword baseva;                 /**< base VA for this chunk */
33   uword size;                   /**< size in bytes of this chunk */
34   uword flags;                  /**< flags (free/busy)  */
35 } clib_valloc_chunk_t;
36
37 #define CLIB_VALLOC_BUSY        (1<<0) /**< chunk is in use */
38
39 typedef struct
40 {
41   clib_valloc_chunk_t *chunks;  /**< pool of virtual chunks  */
42   uword *chunk_index_by_baseva; /**< chunk by baseva hash */
43   clib_spinlock_t lock;         /**< spinlock */
44   uword flags;                  /**< flags */
45   u32 first_index;              /**< pool index of first chunk in list */
46 } clib_valloc_main_t;
47
48 #define CLIB_VALLOC_INITIALIZED (1<<0) /**< object has been initialized */
49
50 /* doxygen tags in valloc.c */
51 void clib_valloc_init (clib_valloc_main_t * vam,
52                        clib_valloc_chunk_t * template, int need_lock);
53 void
54 clib_valloc_add_chunk (clib_valloc_main_t * vam,
55                        clib_valloc_chunk_t * template);
56
57 format_function_t format_valloc;
58
59 uword clib_valloc_free (clib_valloc_main_t * vam, uword baseva);
60 uword clib_valloc_alloc (clib_valloc_main_t * vam, uword size,
61                          int os_out_of_memory_on_failure);
62
63 #endif /* included_valloc_h */
64
65 /*
66  * fd.io coding-style-patch-verification: ON
67  *
68  * Local Variables:
69  * eval: (c-set-style "gnu")
70  * End:
71  */