PipeWire: pipewire/array.h Source File

PipeWire 0.3.51

array.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_ARRAY_H
26#define PIPEWIRE_ARRAY_H
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
32#include <errno.h>
33
34#include <spa/utils/defs.h>
35
48struct pw_array {
49 void *data;
50 size_t size;
51 size_t alloc;
52 size_t extend;
53};
54
55#define PW_ARRAY_INIT(extend) (struct pw_array) { NULL, 0, 0, extend }
56
57#define pw_array_get_len_s(a,s) ((a)->size / (s))
58#define pw_array_get_unchecked_s(a,idx,s,t) SPA_PTROFF((a)->data,(idx)*(s),t)
59#define pw_array_check_index_s(a,idx,s) ((idx) < pw_array_get_len_s(a,s))
60
62#define pw_array_get_len(a,t) pw_array_get_len_s(a,sizeof(t))
64#define pw_array_get_unchecked(a,idx,t) pw_array_get_unchecked_s(a,idx,sizeof(t),t)
66#define pw_array_check_index(a,idx,t) pw_array_check_index_s(a,idx,sizeof(t))
67
68#define pw_array_first(a) ((a)->data)
69#define pw_array_end(a) SPA_PTROFF((a)->data, (a)->size, void)
70#define pw_array_check(a,p) (SPA_PTROFF(p,sizeof(*p),void) <= pw_array_end(a))
71
72#define pw_array_for_each(pos, array) \
73 for (pos = (__typeof__(pos)) pw_array_first(array); \
74 pw_array_check(array, pos); \
75 (pos)++)
76
77#define pw_array_consume(pos, array) \
78 for (pos = (__typeof__(pos)) pw_array_first(array); \
79 pw_array_check(array, pos); \
80 pos = (__typeof__(pos)) pw_array_first(array))
81
82#define pw_array_remove(a,p) \
83({ \
84 (a)->size -= sizeof(*(p)); \
85 memmove(p, SPA_PTROFF((p), sizeof(*(p)), void), \
86 SPA_PTRDIFF(pw_array_end(a),(p))); \
87})
88
90static inline void pw_array_init(struct pw_array *arr, size_t extend)
91{
92 arr->data = NULL;
93 arr->size = arr->alloc = 0;
94 arr->extend = extend;
95}
96
98static inline void pw_array_clear(struct pw_array *arr)
99{
100 free(arr->data);
101 pw_array_init(arr, arr->extend);
102}
103
105static inline void pw_array_reset(struct pw_array *arr)
106{
107 arr->size = 0;
108}
109
111static inline int pw_array_ensure_size(struct pw_array *arr, size_t size)
112{
113 size_t alloc, need;
114
115 alloc = arr->alloc;
116 need = arr->size + size;
117
118 if (SPA_UNLIKELY(alloc < need)) {
119 void *data;
120 alloc = SPA_MAX(alloc, arr->extend);
121 spa_assert(alloc != 0); /* forgot pw_array_init */
122 while (alloc < need)
123 alloc *= 2;
124 if (SPA_UNLIKELY((data = realloc(arr->data, alloc)) == NULL))
125 return -errno;
126 arr->data = data;
127 arr->alloc = alloc;
128 }
129 return 0;
130}
131
134static inline void *pw_array_add(struct pw_array *arr, size_t size)
135{
136 void *p;
137
138 if (pw_array_ensure_size(arr, size) < 0)
139 return NULL;
140
141 p = SPA_PTROFF(arr->data, arr->size, void);
142 arr->size += size;
143
144 return p;
145}
146
149static inline void *pw_array_add_fixed(struct pw_array *arr, size_t size)
150{
151 void *p;
152
153 if (SPA_UNLIKELY(arr->alloc < arr->size + size)) {
154 errno = ENOSPC;
155 return NULL;
156 }
157
158 p = SPA_PTROFF(arr->data, arr->size, void);
159 arr->size += size;
160
161 return p;
162}
163
165#define pw_array_add_ptr(a,p) \
166 *((void**) pw_array_add(a, sizeof(void*))) = (p)
167
172#ifdef __cplusplus
173} /* extern "C" */
174#endif
175
176#endif /* 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:116
pw_array_init
static void pw_array_init(struct pw_array *arr, size_t extend)
Initialize the array with given extend.
Definition: array.h:95
pw_array_clear
static void pw_array_clear(struct pw_array *arr)
Clear the array.
Definition: array.h:103
pw_array_add
static void * pw_array_add(struct pw_array *arr, size_t size)
Add ref size bytes to arr.
Definition: array.h:139
pw_array_add_fixed
static void * pw_array_add_fixed(struct pw_array *arr, size_t size)
Add ref size bytes to arr.
Definition: array.h:154
pw_array_reset
static void pw_array_reset(struct pw_array *arr)
Reset the array.
Definition: array.h:110
SPA_UNLIKELY
#define SPA_UNLIKELY(x)
Definition: defs.h:315
SPA_PTROFF
#define SPA_PTROFF(ptr_, offset_, type_)
Return the address (buffer + offset) as pointer of type.
Definition: defs.h:183
spa_assert
#define spa_assert(expr)
Definition: defs.h:370
SPA_MAX
#define SPA_MAX(a, b)
Definition: defs.h:152
pw_array
Definition: array.h:52
pw_array::size
size_t size
length of array in bytes
Definition: array.h:54
pw_array::alloc
size_t alloc
number of allocated memory in data
Definition: array.h:55
pw_array::extend
size_t extend
number of bytes to extend with
Definition: array.h:56
pw_array::data
void * data
pointer to array data
Definition: array.h:53