PipeWire 1.2.6
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#ifdef __cplusplus
9extern "C" {
10#endif
11
12#include <errno.h>
13
14#include <spa/utils/defs.h>
15
28struct pw_array {
29 void *data;
30 size_t size;
31 size_t alloc;
32 size_t extend;
34};
37#define PW_ARRAY_INIT(extend) ((struct pw_array) { NULL, 0, 0, (extend) })
38
40#define pw_array_get_len_s(a,s) ((a)->size / (s))
41#define pw_array_get_unchecked_s(a,idx,s,t) SPA_PTROFF((a)->data,(idx)*(s),t)
42#define pw_array_check_index_s(a,idx,s) ((idx) < pw_array_get_len_s(a,s))
43
45#define pw_array_get_len(a,t) pw_array_get_len_s(a,sizeof(t))
47#define pw_array_get_unchecked(a,idx,t) pw_array_get_unchecked_s(a,idx,sizeof(t),t)
49#define pw_array_check_index(a,idx,t) pw_array_check_index_s(a,idx,sizeof(t))
51#define pw_array_first(a) ((a)->data)
52#define pw_array_end(a) SPA_PTROFF((a)->data, (a)->size, void)
53#define pw_array_check(a,p) (SPA_PTROFF(p,sizeof(*(p)),void) <= pw_array_end(a))
55#define pw_array_for_each(pos, array) \
56 for ((pos) = (__typeof__(pos)) pw_array_first(array); \
57 pw_array_check(array, pos); \
58 (pos)++)
59
60#define pw_array_consume(pos, array) \
61 for ((pos) = (__typeof__(pos)) pw_array_first(array); \
62 pw_array_check(array, pos); \
63 (pos) = (__typeof__(pos)) pw_array_first(array))
64
65#define pw_array_remove(a,p) \
66({ \
67 (a)->size -= sizeof(*(p)); \
68 memmove(p, SPA_PTROFF((p), sizeof(*(p)), void), \
69 SPA_PTRDIFF(pw_array_end(a),(p))); \
70})
71
74static inline void pw_array_init(struct pw_array *arr, size_t extend)
75{
76 arr->data = NULL;
77 arr->size = arr->alloc = 0;
78 arr->extend = extend;
80
82static inline void pw_array_clear(struct pw_array *arr)
83{
84 if (arr->extend > 0)
85 free(arr->data);
86 pw_array_init(arr, arr->extend);
88
90static inline void pw_array_init_static(struct pw_array *arr, void *data, size_t size)
91{
92 arr->data = data;
93 arr->alloc = size;
94 arr->size = arr->extend = 0;
96
98static inline void pw_array_reset(struct pw_array *arr)
99{
100 arr->size = 0;
101}
102
104static inline int pw_array_ensure_size(struct pw_array *arr, size_t size)
105{
106 size_t alloc, need;
107
108 alloc = arr->alloc;
109 need = arr->size + size;
110
111 if (SPA_UNLIKELY(alloc < need)) {
112 void *data;
113 if (arr->extend == 0)
114 return -ENOSPC;
115 alloc = SPA_ROUND_UP(need, arr->extend);
116 if (SPA_UNLIKELY((data = realloc(arr->data, alloc)) == NULL))
117 return -errno;
118 arr->data = data;
119 arr->alloc = alloc;
120 }
121 return 0;
122}
123
127static inline void *pw_array_add(struct pw_array *arr, size_t size)
128{
129 void *p;
130
131 if (pw_array_ensure_size(arr, size) < 0)
132 return NULL;
133
134 p = SPA_PTROFF(arr->data, arr->size, void);
135 arr->size += size;
136
137 return p;
138}
139
142static inline int pw_array_add_ptr(struct pw_array *arr, void *ptr)
143{
144 void **p = (void **)pw_array_add(arr, sizeof(void*));
145 if (p == NULL)
146 return -errno;
147 *p = ptr;
148 return 0;
149}
150
155#ifdef __cplusplus
156} /* extern "C" */
157#endif
158
159#endif /* PIPEWIRE_ARRAY_H */
spa/utils/defs.h
static void pw_array_init_static(struct pw_array *arr, void *data, size_t size)
Initialize a static array.
Definition array.h:95
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:109
static void pw_array_init(struct pw_array *arr, size_t extend)
Initialize the array with given extend.
Definition array.h:79
static int pw_array_add_ptr(struct pw_array *arr, void *ptr)
Add a pointer to array.
Definition array.h:147
static void pw_array_clear(struct pw_array *arr)
Clear the array.
Definition array.h:87
static void * pw_array_add(struct pw_array *arr, size_t size)
Add ref size bytes to arr.
Definition array.h:132
static void pw_array_reset(struct pw_array *arr)
Reset the array.
Definition array.h:103
#define SPA_ROUND_UP(num, value)
Definition defs.h:330
#define SPA_UNLIKELY(x)
Definition defs.h:369
#define SPA_PTROFF(ptr_, offset_, type_)
Return the address (buffer + offset) as pointer of type.
Definition defs.h:222
Definition array.h:32
size_t size
length of array in bytes
Definition array.h:34
size_t alloc
number of allocated memory in data
Definition array.h:35
size_t extend
number of bytes to extend with, 0 when the data should not expand
Definition array.h:36
void * data
pointer to array data
Definition array.h:33