PipeWire: pipewire/map.h Source File

PipeWire 1.0.3

Loading...
Searching...
No Matches
map.h
Go to the documentation of this file.
1/* PipeWire */
2/* SPDX-FileCopyrightText: Copyright © 2018 Wim Taymans */
3/* SPDX-License-Identifier: MIT */
4
5#ifndef PIPEWIRE_MAP_H
6#define PIPEWIRE_MAP_H
7
8#ifdef __cplusplus
9extern "C" {
10#endif
11
12#include <string.h>
13#include <errno.h>
14
15#include <spa/utils/defs.h>
16#include <pipewire/array.h>
17
56union pw_map_item {
57 uintptr_t next; /* next free index */
58 void *data; /* data of this item, must be an even address */
59};
60
62struct pw_map {
63 struct pw_array items; /* an array with the map items */
64 uint32_t free_list; /* first free index */
65};
66
68#define PW_MAP_INIT(extend) ((struct pw_map) { PW_ARRAY_INIT(extend), SPA_ID_INVALID })
69
76#define pw_map_get_size(m) pw_array_get_len(&(m)->items, union pw_map_item)
77#define pw_map_get_item(m,id) pw_array_get_unchecked(&(m)->items,id,union pw_map_item)
78#define pw_map_item_is_free(item) ((item)->next & 0x1)
79#define pw_map_id_is_free(m,id) (pw_map_item_is_free(pw_map_get_item(m,id)))
81#define pw_map_check_id(m,id) ((id) < pw_map_get_size(m))
83#define pw_map_has_item(m,id) (pw_map_check_id(m,id) && !pw_map_id_is_free(m, id))
84#define pw_map_lookup_unchecked(m,id) pw_map_get_item(m,id)->data
85
87#define PW_MAP_ID_TO_PTR(id) (SPA_UINT32_TO_PTR((id)<<1))
89#define PW_MAP_PTR_TO_ID(p) (SPA_PTR_TO_UINT32(p)>>1)
90
96 static inline void pw_map_init(struct pw_map *map, size_t size, size_t extend)
97{
98 pw_array_init(&map->items, extend * sizeof(union pw_map_item));
99 pw_array_ensure_size(&map->items, size * sizeof(union pw_map_item));
100 map->free_list = SPA_ID_INVALID;
101}
102
106static inline void pw_map_clear(struct pw_map *map)
107{
108 pw_array_clear(&map->items);
109}
110
114static inline void pw_map_reset(struct pw_map *map)
115{
116 pw_array_reset(&map->items);
117 map->free_list = SPA_ID_INVALID;
118}
119
126static inline uint32_t pw_map_insert_new(struct pw_map *map, void *data)
127{
128 union pw_map_item *start, *item;
129 uint32_t id;
130
131 if (map->free_list != SPA_ID_INVALID) {
132 start = (union pw_map_item *) map->items.data;
133 item = &start[map->free_list >> 1]; /* lsb always 1, see pw_map_remove */
134 map->free_list = item->next;
135 } else {
136 item = (union pw_map_item *) pw_array_add(&map->items, sizeof(union pw_map_item));
137 if (item == NULL)
138 return SPA_ID_INVALID;
139 start = (union pw_map_item *) map->items.data;
140 }
141 item->data = data;
142 id = (item - start);
143 return id;
144}
145
153static inline int pw_map_insert_at(struct pw_map *map, uint32_t id, void *data)
154{
155 size_t size = pw_map_get_size(map);
156 union pw_map_item *item;
157
158 if (id > size)
159 return -ENOSPC;
160 else if (id == size) {
161 item = (union pw_map_item *) pw_array_add(&map->items, sizeof(union pw_map_item));
162 if (item == NULL)
163 return -errno;
164 } else {
165 item = pw_map_get_item(map, id);
166 if (pw_map_item_is_free(item))
167 return -EINVAL;
168 }
169 item->data = data;
170 return 0;
171}
172
178static inline void pw_map_remove(struct pw_map *map, uint32_t id)
179{
180 if (pw_map_id_is_free(map, id))
181 return;
182
183 pw_map_get_item(map, id)->next = map->free_list;
184 map->free_list = (id << 1) | 1;
185}
186
192static inline void *pw_map_lookup(const struct pw_map *map, uint32_t id)
193{
194 if (SPA_LIKELY(pw_map_check_id(map, id))) {
195 union pw_map_item *item = pw_map_get_item(map, id);
196 if (!pw_map_item_is_free(item))
197 return item->data;
198 }
199 return NULL;
200}
201
210static inline int pw_map_for_each(const struct pw_map *map,
211 int (*func) (void *item_data, void *data), void *data)
212{
213 union pw_map_item *item;
214 int res = 0;
215
216 pw_array_for_each(item, &map->items) {
217 if (!pw_map_item_is_free(item))
218 if ((res = func(item->data, data)) != 0)
219 break;
220 }
221 return res;
222}
223
228#ifdef __cplusplus
229} /* extern "C" */
230#endif
231
232#endif /* PIPEWIRE_MAP_H */
array.h
pipewire/array.h
defs.h
spa/utils/defs.h
pw_array_ensure_size
static int pw_array_ensure_size(struct pw_array *arr, size_t size)
Make sure size bytes can be added to the array.
Definition array.h:96
pw_array_init
static void pw_array_init(struct pw_array *arr, size_t extend)
Initialize the array with given extend.
Definition array.h:75
pw_array_clear
static void pw_array_clear(struct pw_array *arr)
Clear the array.
Definition array.h:83
pw_array_for_each
#define pw_array_for_each(pos, array)
Definition array.h:57
pw_array_add
static void * pw_array_add(struct pw_array *arr, size_t size)
Add ref size bytes to arr.
Definition array.h:119
pw_array_reset
static void pw_array_reset(struct pw_array *arr)
Reset the array.
Definition array.h:90
pw_map_remove
static void pw_map_remove(struct pw_map *map, uint32_t id)
Remove an item at index.
Definition map.h:185
pw_map_check_id
#define pw_map_check_id(m, id)
Definition map.h:86
pw_map_insert_at
static int pw_map_insert_at(struct pw_map *map, uint32_t id, void *data)
Replace the data in the map at an index.
Definition map.h:160
pw_map_get_size
#define pw_map_get_size(m)
Get the number of currently allocated elements in the map.
Definition map.h:81
pw_map_insert_new
static uint32_t pw_map_insert_new(struct pw_map *map, void *data)
Insert data in the map.
Definition map.h:133
pw_map_for_each
static int pw_map_for_each(const struct pw_map *map, int(*func)(void *item_data, void *data), void *data)
Iterate all map items.
Definition map.h:217
pw_map_reset
static void pw_map_reset(struct pw_map *map)
Reset a map but keep previously allocated storage.
Definition map.h:121
pw_map_clear
static void pw_map_clear(struct pw_map *map)
Clear a map and free the data storage.
Definition map.h:113
pw_map_item_is_free
#define pw_map_item_is_free(item)
Definition map.h:83
pw_map_id_is_free
#define pw_map_id_is_free(m, id)
Definition map.h:84
pw_map_init
static void pw_map_init(struct pw_map *map, size_t size, size_t extend)
Initialize a map.
Definition map.h:103
pw_map_lookup
static void * pw_map_lookup(const struct pw_map *map, uint32_t id)
Find an item in the map.
Definition map.h:199
pw_map_get_item
#define pw_map_get_item(m, id)
Definition map.h:82
SPA_ID_INVALID
#define SPA_ID_INVALID
Definition defs.h:232
SPA_LIKELY
#define SPA_LIKELY(x)
Definition defs.h:349
string.h
spa/utils/string.h
pw_array
Definition array.h:32
pw_array::size
size_t size
length of array in bytes
Definition array.h:34
pw_array::extend
size_t extend
number of bytes to extend with
Definition array.h:36
pw_array::data
void * data
pointer to array data
Definition array.h:33
pw_map
A map.
Definition map.h:66
pw_map::items
struct pw_array items
Definition map.h:67
pw_map::free_list
uint32_t free_list
Definition map.h:68