PipeWire
1.7.0
Toggle main menu visibility
Loading...
Searching...
No Matches
array.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_ARRAY_H
6
#define PIPEWIRE_ARRAY_H
7
8
#include <errno.h>
9
10
#include <
spa/utils/defs.h
>
11
#include <spa/utils/overflow.h>
12
13
#ifdef __cplusplus
14
extern
"C"
{
15
#endif
16
17
#ifndef PW_API_ARRAY
18
#define PW_API_ARRAY static inline
19
#endif
20
21
29
34
struct
pw_array
{
35
void
*data;
36
size_t
size
;
37
size_t
alloc
;
38
size_t
extend
;
39
* data should not expand */
40
};
41
43
#define PW_ARRAY_INIT(extend) ((struct pw_array) { NULL, 0, 0, (extend) })
44
46
#define pw_array_get_len_s(a,s) ((a)->size / (s))
47
#define pw_array_get_unchecked_s(a,idx,s,t) SPA_PTROFF((a)->data,(idx)*(s),t)
48
#define pw_array_check_index_s(a,idx,s) ((idx) < pw_array_get_len_s(a,s))
49
51
#define pw_array_get_len(a,t) pw_array_get_len_s(a,sizeof(t))
53
#define pw_array_get_unchecked(a,idx,t) pw_array_get_unchecked_s(a,idx,sizeof(t),t)
55
#define pw_array_check_index(a,idx,t) pw_array_check_index_s(a,idx,sizeof(t))
56
57
#define pw_array_first(a) ((a)->data)
58
#define pw_array_end(a) SPA_PTROFF((a)->data, (a)->size, void)
59
#define pw_array_check(a,p) (SPA_PTROFF(p,sizeof(*(p)),void) <= pw_array_end(a))
60
61
#define pw_array_for_each(pos, array) \
62
for ((pos) = (__typeof__(pos)) pw_array_first(array); \
63
pw_array_check(array, pos); \
64
(pos)++)
65
66
#define pw_array_consume(pos, array) \
67
for ((pos) = (__typeof__(pos)) pw_array_first(array); \
68
pw_array_check(array, pos); \
69
(pos) = (__typeof__(pos)) pw_array_first(array))
70
71
#define pw_array_remove(a,p) \
72
({ \
73
(a)->size -= sizeof(*(p)); \
74
memmove(p, SPA_PTROFF((p), sizeof(*(p)), void), \
75
SPA_PTRDIFF(pw_array_end(a),(p))); \
76
})
77
80
PW_API_ARRAY
void
pw_array_init
(
struct
pw_array
*arr,
size_t
extend)
81
{
82
arr->
data
= NULL;
83
arr->
size
= arr->
alloc
= 0;
84
arr->
extend
= extend;
85
}
86
88
PW_API_ARRAY
void
pw_array_clear
(
struct
pw_array
*arr)
89
{
90
if
(arr->
extend
> 0)
91
free(arr->
data
);
92
pw_array_init
(arr, arr->
extend
);
93
}
94
96
PW_API_ARRAY
void
pw_array_init_static
(
struct
pw_array
*arr,
void
*data,
size_t
size)
97
{
98
arr->
data
= data;
99
arr->
alloc
= size;
100
arr->
size
= arr->
extend
= 0;
101
}
102
104
PW_API_ARRAY
void
pw_array_reset
(
struct
pw_array
*arr)
105
{
106
arr->
size
= 0;
107
}
108
110
PW_API_ARRAY
int
pw_array_ensure_size
(
struct
pw_array
*arr,
size_t
size)
111
{
112
size_t
alloc, need;
113
114
alloc = arr->
alloc
;
115
if
(
SPA_UNLIKELY
(spa_overflow_add(arr->
size
, size, &need)))
116
return
-ENOMEM;
117
118
if
(
SPA_UNLIKELY
(alloc < need)) {
119
void
*data;
120
if
(arr->
extend
== 0)
121
return
-ENOSPC;
122
alloc =
SPA_ROUND_UP
(need, arr->
extend
);
123
if
(
SPA_UNLIKELY
((data = realloc(arr->
data
, alloc)) == NULL))
124
return
-errno;
125
arr->
data
= data;
126
arr->
alloc
= alloc;
127
}
128
return
0;
129
}
130
134
PW_API_ARRAY
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
149
PW_API_ARRAY
int
pw_array_add_ptr
(
struct
pw_array
*arr,
void
*ptr)
150
{
151
void
**p = (
void
**)
pw_array_add
(arr,
sizeof
(
void
*));
152
if
(p == NULL)
153
return
-errno;
154
*p = ptr;
155
return
0;
156
}
157
161
162
#ifdef __cplusplus
163
}
/* extern "C" */
164
#endif
165
166
#endif
/* PIPEWIRE_ARRAY_H */
PW_API_ARRAY
#define PW_API_ARRAY
Definition
array.h:23
defs.h
spa/utils/defs.h
pw_array_ensure_size
PW_API_ARRAY 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_reset
PW_API_ARRAY void pw_array_reset(struct pw_array *arr)
Reset the array.
Definition
array.h:110
pw_array_add_ptr
PW_API_ARRAY int pw_array_add_ptr(struct pw_array *arr, void *ptr)
Add a pointer to array.
Definition
array.h:155
pw_array_init_static
PW_API_ARRAY void pw_array_init_static(struct pw_array *arr, void *data, size_t size)
Initialize a static array.
Definition
array.h:102
pw_array_add
PW_API_ARRAY void * pw_array_add(struct pw_array *arr, size_t size)
Add ref size bytes to arr.
Definition
array.h:140
pw_array_clear
PW_API_ARRAY void pw_array_clear(struct pw_array *arr)
Clear the array.
Definition
array.h:94
pw_array_init
PW_API_ARRAY void pw_array_init(struct pw_array *arr, size_t extend)
Initialize the array with given extend.
Definition
array.h:86
SPA_ROUND_UP
#define SPA_ROUND_UP(num, value)
Definition
defs.h:353
SPA_UNLIKELY
#define SPA_UNLIKELY(x)
Definition
defs.h:443
SPA_PTROFF
#define SPA_PTROFF(ptr_, offset_, type_)
Return the address (buffer + offset) as pointer of type.
Definition
defs.h:223
pw_array
Definition
array.h:39
pw_array::size
size_t size
length of array in bytes
Definition
array.h:41
pw_array::alloc
size_t alloc
number of allocated memory in data
Definition
array.h:42
pw_array::extend
size_t extend
number of bytes to extend with, 0 when the data should not expand
Definition
array.h:43
pw_array::data
void * data
pointer to array data
Definition
array.h:40
pipewire
array.h
Generated by
1.18.0