PipeWire 1.4.1
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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#ifdef __cplusplus
9extern "C" {
10#endif
11
12#include <errno.h>
13
14#include <spa/utils/defs.h>
15
16#ifndef PW_API_ARRAY
17#define PW_API_ARRAY static inline
18#endif
19
20
33struct pw_array {
34 void *data;
35 size_t size;
36 size_t alloc;
37 size_t extend;
39};
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))
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)++)
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})
79PW_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}
87PW_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}
95PW_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}
103PW_API_ARRAY void pw_array_reset(struct pw_array *arr)
104{
105 arr->size = 0;
106}
107
109PW_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;
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
132PW_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;
139 p = SPA_PTROFF(arr->data, arr->size, void);
140 arr->size += size;
141
142 return p;
143}
144
147PW_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
160#ifdef __cplusplus
161} /* extern "C" */
162#endif
163
164#endif /* PIPEWIRE_ARRAY_H */
#define PW_API_ARRAY
Definition array.h:22
spa/utils/defs.h
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_API_ARRAY void pw_array_reset(struct pw_array *arr)
Reset the array.
Definition array.h:109
PW_API_ARRAY int pw_array_add_ptr(struct pw_array *arr, void *ptr)
Add a pointer to array.
Definition array.h:153
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_API_ARRAY void * pw_array_add(struct pw_array *arr, size_t size)
Add ref size bytes to arr.
Definition array.h:138
PW_API_ARRAY void pw_array_clear(struct pw_array *arr)
Clear the array.
Definition array.h:93
PW_API_ARRAY void pw_array_init(struct pw_array *arr, size_t extend)
Initialize the array with given extend.
Definition array.h:85
#define SPA_ROUND_UP(num, value)
Definition defs.h:348
#define SPA_UNLIKELY(x)
Definition defs.h:394
#define SPA_PTROFF(ptr_, offset_, type_)
Return the address (buffer + offset) as pointer of type.
Definition defs.h:222
Definition array.h:38
size_t size
length of array in bytes
Definition array.h:40
size_t alloc
number of allocated memory in data
Definition array.h:41
size_t extend
number of bytes to extend with, 0 when the data should not expand
Definition array.h:42
void * data
pointer to array data
Definition array.h:39