PipeWire 1.6.8

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
12#ifdef __cplusplus
13 extern "C" {
14#endif
15
16#ifndef PW_API_ARRAY
17#define PW_API_ARRAY static inline
18#endif
19
20
28
33 struct pw_array {
34 void *data;
35 size_t size;
36 size_t alloc;
37 size_t extend;
38 * data should not expand */
39};
40
42#define PW_ARRAY_INIT(extend) ((struct pw_array) { NULL, 0, 0, (extend) })
43
45#define pw_array_get_len_s(a,s) ((a)->size / (s))
46#define pw_array_get_unchecked_s(a,idx,s,t) SPA_PTROFF((a)->data,(idx)*(s),t)
47#define pw_array_check_index_s(a,idx,s) ((idx) < pw_array_get_len_s(a,s))
48
50#define pw_array_get_len(a,t) pw_array_get_len_s(a,sizeof(t))
52#define pw_array_get_unchecked(a,idx,t) pw_array_get_unchecked_s(a,idx,sizeof(t),t)
54#define pw_array_check_index(a,idx,t) pw_array_check_index_s(a,idx,sizeof(t))
55
56#define pw_array_first(a) ((a)->data)
57#define pw_array_end(a) SPA_PTROFF((a)->data, (a)->size, void)
58#define pw_array_check(a,p) (SPA_PTROFF(p,sizeof(*(p)),void) <= pw_array_end(a))
59
60#define pw_array_for_each(pos, array) \
61 for ((pos) = (__typeof__(pos)) pw_array_first(array); \
62 pw_array_check(array, pos); \
63 (pos)++)
64
65#define pw_array_consume(pos, array) \
66 for ((pos) = (__typeof__(pos)) pw_array_first(array); \
67 pw_array_check(array, pos); \
68 (pos) = (__typeof__(pos)) pw_array_first(array))
69
70#define pw_array_remove(a,p) \
71({ \
72 (a)->size -= sizeof(*(p)); \
73 memmove(p, SPA_PTROFF((p), sizeof(*(p)), void), \
74 SPA_PTRDIFF(pw_array_end(a),(p))); \
75})
76
79 PW_API_ARRAY void pw_array_init(struct pw_array *arr, size_t extend)
80{
81 arr->data = NULL;
82 arr->size = arr->alloc = 0;
83 arr->extend = extend;
84}
85
87 PW_API_ARRAY void pw_array_clear(struct pw_array *arr)
88{
89 if (arr->extend > 0)
90 free(arr->data);
91 pw_array_init(arr, arr->extend);
92}
93
95 PW_API_ARRAY void pw_array_init_static(struct pw_array *arr, void *data, size_t size)
96{
97 arr->data = data;
98 arr->alloc = size;
99 arr->size = arr->extend = 0;
100}
101
103 PW_API_ARRAY void pw_array_reset(struct pw_array *arr)
104{
105 arr->size = 0;
106}
107
109 PW_API_ARRAY int pw_array_ensure_size(struct pw_array *arr, size_t size)
110{
111 size_t alloc, need;
112
113 alloc = arr->alloc;
114 need = arr->size + size;
115
116 if (SPA_UNLIKELY(alloc < need)) {
117 void *data;
118 if (arr->extend == 0)
119 return -ENOSPC;
120 alloc = SPA_ROUND_UP(need, arr->extend);
121 if (SPA_UNLIKELY((data = realloc(arr->data, alloc)) == NULL))
122 return -errno;
123 arr->data = data;
124 arr->alloc = alloc;
125 }
126 return 0;
127}
128
132 PW_API_ARRAY void *pw_array_add(struct pw_array *arr, size_t size)
133{
134 void *p;
135
136 if (pw_array_ensure_size(arr, size) < 0)
137 return NULL;
138
139 p = SPA_PTROFF(arr->data, arr->size, void);
140 arr->size += size;
141
142 return p;
143}
144
147 PW_API_ARRAY int pw_array_add_ptr(struct pw_array *arr, void *ptr)
148{
149 void **p = (void **)pw_array_add(arr, sizeof(void*));
150 if (p == NULL)
151 return -errno;
152 *p = ptr;
153 return 0;
154}
155
159
160#ifdef __cplusplus
161} /* extern "C" */
162#endif
163
164#endif /* PIPEWIRE_ARRAY_H */
PW_API_ARRAY
#define PW_API_ARRAY
Definition array.h:22
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:115
pw_array_reset
PW_API_ARRAY void pw_array_reset(struct pw_array *arr)
Reset the array.
Definition array.h:109
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:153
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:101
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:138
pw_array_clear
PW_API_ARRAY void pw_array_clear(struct pw_array *arr)
Clear the array.
Definition array.h:93
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:85
SPA_ROUND_UP
#define SPA_ROUND_UP(num, value)
Definition defs.h:352
SPA_UNLIKELY
#define SPA_UNLIKELY(x)
Definition defs.h:398
SPA_PTROFF
#define SPA_PTROFF(ptr_, offset_, type_)
Return the address (buffer + offset) as pointer of type.
Definition defs.h:222
pw_array
Definition array.h:38
pw_array::size
size_t size
length of array in bytes
Definition array.h:40
pw_array::alloc
size_t alloc
number of allocated memory in data
Definition array.h:41
pw_array::extend
size_t extend
number of bytes to extend with, 0 when the data should not expand
Definition array.h:42
pw_array::data
void * data
pointer to array data
Definition array.h:39