PipeWire: map.h Source File

PipeWire 0.3.34

map.h
Go to the documentation of this file.
1 /* PipeWire
2 *
3 * Copyright © 2018 Wim Taymans
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 #ifndef PIPEWIRE_MAP_H
26 #define PIPEWIRE_MAP_H
27
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31
32 #include <string.h>
33 #include <errno.h>
34
35 #include <spa/utils/defs.h>
36 #include <pipewire/array.h>
37
49 union pw_map_item {
50 uint32_t next;
51 void *data;
52 };
53
55 struct pw_map {
56 struct pw_array items;
57 uint32_t free_list;
58 };
59
60 #define PW_MAP_INIT(extend) (struct pw_map) { PW_ARRAY_INIT(extend), SPA_ID_INVALID }
61
62 #define pw_map_get_size(m) pw_array_get_len(&(m)->items, union pw_map_item)
63 #define pw_map_get_item(m,id) pw_array_get_unchecked(&(m)->items,id,union pw_map_item)
64 #define pw_map_item_is_free(item) ((item)->next & 0x1)
65 #define pw_map_id_is_free(m,id) (pw_map_item_is_free(pw_map_get_item(m,id)))
66 #define pw_map_check_id(m,id) ((id) < pw_map_get_size(m))
67 #define pw_map_has_item(m,id) (pw_map_check_id(m,id) && !pw_map_id_is_free(m, id))
68 #define pw_map_lookup_unchecked(m,id) pw_map_get_item(m,id)->data
69
71 #define PW_MAP_ID_TO_PTR(id) (SPA_UINT32_TO_PTR((id)<<1))
73 #define PW_MAP_PTR_TO_ID(p) (SPA_PTR_TO_UINT32(p)>>1)
74
80 static inline void pw_map_init(struct pw_map *map, size_t size, size_t extend)
81 {
82 pw_array_init(&map->items, extend);
83 pw_array_ensure_size(&map->items, size * sizeof(union pw_map_item));
84 map->free_list = SPA_ID_INVALID;
85 }
86
90 static inline void pw_map_clear(struct pw_map *map)
91 {
92 pw_array_clear(&map->items);
93 }
94
95 static inline void pw_map_reset(struct pw_map *map)
96 {
97 pw_array_reset(&map->items);
98 map->free_list = SPA_ID_INVALID;
99 }
100
107 static inline uint32_t pw_map_insert_new(struct pw_map *map, void *data)
108 {
109 union pw_map_item *start, *item;
110 uint32_t id;
111
112 if (map->free_list != SPA_ID_INVALID) {
113 start = (union pw_map_item *) map->items.data;
114 item = &start[map->free_list >> 1];
115 map->free_list = item->next;
116 } else {
117 item = (union pw_map_item *) pw_array_add(&map->items, sizeof(union pw_map_item));
118 if (item == NULL)
119 return SPA_ID_INVALID;
120 start = (union pw_map_item *) map->items.data;
121 }
122 item->data = data;
123 id = (item - start);
124 return id;
125 }
126
134 static inline int pw_map_insert_at(struct pw_map *map, uint32_t id, void *data)
135 {
136 size_t size = pw_map_get_size(map);
137 union pw_map_item *item;
138
139 if (id > size)
140 return -ENOSPC;
141 else if (id == size) {
142 item = (union pw_map_item *) pw_array_add(&map->items, sizeof(union pw_map_item));
143 if (item == NULL)
144 return -errno;
145 }
146 else {
147 item = pw_map_get_item(map, id);
148 }
149 item->data = data;
150 return 0;
151 }
152
157 static inline void pw_map_remove(struct pw_map *map, uint32_t id)
158 {
159 pw_map_get_item(map, id)->next = map->free_list;
160 map->free_list = (id << 1) | 1;
161 }
162
168 static inline void *pw_map_lookup(struct pw_map *map, uint32_t id)
169 {
170 if (SPA_LIKELY(pw_map_check_id(map, id))) {
171 union pw_map_item *item = pw_map_get_item(map, id);
172 if (!pw_map_item_is_free(item))
173 return item->data;
174 }
175 return NULL;
176 }
177
186 static inline int pw_map_for_each(struct pw_map *map,
187 int (*func) (void *item_data, void *data), void *data)
188 {
189 union pw_map_item *item;
190 int res = 0;
191
192 pw_array_for_each(item, &map->items) {
193 if (!pw_map_item_is_free(item))
194 if ((res = func(item->data, data)) != 0)
195 break;
196 }
197 return res;
198 }
199
204 #ifdef __cplusplus
205 } /* extern "C" */
206 #endif
207
208 #endif /* PIPEWIRE_MAP_H */
array.h
defs.h
pw_array_for_each
#define pw_array_for_each(pos, array)
Definition: array.h:72
pw_map_check_id
#define pw_map_check_id(m, id)
Definition: map.h:66
pw_map_get_size
#define pw_map_get_size(m)
Definition: map.h:62
pw_map_item_is_free
#define pw_map_item_is_free(item)
Definition: map.h:64
pw_map_get_item
#define pw_map_get_item(m, id)
Definition: map.h:63
SPA_ID_INVALID
#define SPA_ID_INVALID
Definition: defs.h:182
SPA_LIKELY
#define SPA_LIKELY(x)
Definition: defs.h:234
start
int(* start)(struct sm_media_session *sess)
Definition: media-session.c:2381
string.h
data
user data to add to an object
Definition: filter.c:75
item
Definition: impl-metadata.c:49
pw_array
Definition: array.h:48
pw_array::size
size_t size
length of array in bytes
Definition: array.h:50
pw_array::extend
size_t extend
number of bytes to extend with
Definition: array.h:52
pw_array::data
void * data
pointer to array data
Definition: array.h:49
pw_map
A map.
Definition: map.h:55
pw_map::items
struct pw_array items
an array with the map items
Definition: map.h:56
pw_map::free_list
uint32_t free_list
the free items
Definition: map.h:57
pw_map_item
An entry in the map.
Definition: map.h:49
pw_map_item::next
uint32_t next
next free index
Definition: map.h:50
pw_map_item::data
void * data
data of this item, must be an even address
Definition: map.h:51