PipeWire  0.2.0
map.h
Go to the documentation of this file.
1 /* PipeWire
2  * Copyright (C) 2016 Wim Taymans <wim.taymans@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 #ifndef __PIPEWIRE_MAP_H__
21 #define __PIPEWIRE_MAP_H__
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 #include <string.h>
28 
29 #include <spa/utils/defs.h>
30 #include <pipewire/array.h>
31 
38 union pw_map_item {
39  uint32_t next;
40  void *data;
41 };
42 
44 struct pw_map {
45  struct pw_array items;
46  uint32_t free_list;
47 };
48 
49 #define PW_MAP_INIT(extend) (struct pw_map) { PW_ARRAY_INIT(extend), 0 }
50 
51 #define pw_map_get_size(m) pw_array_get_len(&(m)->items, union pw_map_item)
52 #define pw_map_get_item(m,id) pw_array_get_unchecked(&(m)->items,id,union pw_map_item)
53 #define pw_map_item_is_free(item) ((item)->next & 0x1)
54 #define pw_map_id_is_free(m,id) (pw_map_item_is_free(pw_map_get_item(m,id)))
55 #define pw_map_check_id(m,id) ((id) < pw_map_get_size(m))
56 #define pw_map_has_item(m,id) (pw_map_check_id(m,id) && !pw_map_id_is_free(m, id))
57 #define pw_map_lookup_unchecked(m,id) pw_map_get_item(m,id)->data
58 
60 #define PW_MAP_ID_TO_PTR(id) (SPA_UINT32_TO_PTR((id)<<1))
61 
62 #define PW_MAP_PTR_TO_ID(p) (SPA_PTR_TO_UINT32(p)>>1)
63 
70 static inline void pw_map_init(struct pw_map *map, size_t size, size_t extend)
71 {
72  pw_array_init(&map->items, extend);
73  pw_array_ensure_size(&map->items, size * sizeof(union pw_map_item));
74  map->free_list = 0;
75 }
76 
81 static inline void pw_map_clear(struct pw_map *map)
82 {
83  pw_array_clear(&map->items);
84 }
85 
92 static inline uint32_t pw_map_insert_new(struct pw_map *map, void *data)
93 {
94  union pw_map_item *start, *item;
95  uint32_t id;
96 
97  if (map->free_list) {
98  start = (union pw_map_item *) map->items.data;
99  item = &start[map->free_list >> 1];
100  map->free_list = item->next;
101  } else {
102  item = (union pw_map_item *) pw_array_add(&map->items, sizeof(union pw_map_item));
103  if (!item)
104  return SPA_ID_INVALID;
105  start = (union pw_map_item *) map->items.data;
106  }
107  item->data = data;
108  id = (item - start);
109  return id;
110 }
111 
119 static inline bool pw_map_insert_at(struct pw_map *map, uint32_t id, void *data)
120 {
121  size_t size = pw_map_get_size(map);
122  union pw_map_item *item;
123 
124  if (id > size)
125  return false;
126  else if (id == size)
127  item = (union pw_map_item *) pw_array_add(&map->items, sizeof(union pw_map_item));
128  else
129  item = pw_map_get_item(map, id);
130 
131  item->data = data;
132  return true;
133 }
134 
140 static inline void pw_map_remove(struct pw_map *map, uint32_t id)
141 {
142  pw_map_get_item(map, id)->next = map->free_list;
143  map->free_list = (id << 1) | 1;
144 }
145 
152 static inline void *pw_map_lookup(struct pw_map *map, uint32_t id)
153 {
154  if (SPA_LIKELY(pw_map_check_id(map, id))) {
155  union pw_map_item *item = pw_map_get_item(map, id);
156  if (!pw_map_item_is_free(item))
157  return item->data;
158  }
159  return NULL;
160 }
161 
168 static inline void pw_map_for_each(struct pw_map *map, void (*func) (void *, void *), void *data)
169 {
170  union pw_map_item *item;
171 
172  pw_array_for_each(item, &map->items) {
173  if (!pw_map_item_is_free(item))
174  func(item->data, data);
175  }
176 }
177 
178 #ifdef __cplusplus
179 } /* extern "C" */
180 #endif
181 
182 #endif /* __PIPEWIRE_MAP_H__ */
static void pw_map_clear(struct pw_map *map)
Clear a map.
Definition: map.h:81
#define pw_map_get_size(m)
Definition: map.h:51
static bool pw_map_insert_at(struct pw_map *map, uint32_t id, void *data)
Insert data in the map at an index.
Definition: map.h:119
static void * pw_array_add(struct pw_array *arr, size_t size)
Add ref size bytes to arr.
Definition: array.h:98
uint32_t free_list
the free items
Definition: map.h:46
#define pw_map_get_item(m, id)
Definition: map.h:52
#define pw_array_for_each(pos, array)
Definition: array.h:56
static void pw_map_for_each(struct pw_map *map, void(*func)(void *, void *), void *data)
Iterate all map items.
Definition: map.h:168
void * data
pointer to array data
Definition: array.h:37
uint32_t next
next free index
Definition: map.h:39
#define pw_map_item_is_free(item)
Definition: map.h:53
static void pw_map_init(struct pw_map *map, size_t size, size_t extend)
Initialize a map.
Definition: map.h:70
#define pw_map_check_id(m, id)
Definition: map.h:55
static void pw_array_init(struct pw_array *arr, size_t extend)
Initialize the array with given extend.
Definition: array.h:62
static bool pw_array_ensure_size(struct pw_array *arr, size_t size)
Make sure size bytes can be added to the array.
Definition: array.h:76
static void * pw_map_lookup(struct pw_map *map, uint32_t id)
Find an item in the map.
Definition: map.h:152
An array object.
Definition: array.h:36
An entry in the map.
Definition: map.h:38
static void pw_map_remove(struct pw_map *map, uint32_t id)
Remove an item at index.
Definition: map.h:140
size_t extend
number of bytes to extend with
Definition: array.h:40
static uint32_t pw_map_insert_new(struct pw_map *map, void *data)
Insert data in the map.
Definition: map.h:92
void * data
data of this item, must be an even address
Definition: map.h:40
size_t size
length of array in bytes
Definition: array.h:38
struct pw_array items
an array with the map items
Definition: map.h:45
A map.
Definition: map.h:44