PipeWire 0.3.77
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;
33};
35#define PW_ARRAY_INIT(extend) ((struct pw_array) { NULL, 0, 0, (extend) })
37#define pw_array_get_len_s(a,s) ((a)->size / (s))
38#define pw_array_get_unchecked_s(a,idx,s,t) SPA_PTROFF((a)->data,(idx)*(s),t)
39#define pw_array_check_index_s(a,idx,s) ((idx) < pw_array_get_len_s(a,s))
42#define pw_array_get_len(a,t) pw_array_get_len_s(a,sizeof(t))
44#define pw_array_get_unchecked(a,idx,t) pw_array_get_unchecked_s(a,idx,sizeof(t),t)
46#define pw_array_check_index(a,idx,t) pw_array_check_index_s(a,idx,sizeof(t))
48#define pw_array_first(a) ((a)->data)
49#define pw_array_end(a) SPA_PTROFF((a)->data, (a)->size, void)
50#define pw_array_check(a,p) (SPA_PTROFF(p,sizeof(*(p)),void) <= pw_array_end(a))
52#define pw_array_for_each(pos, array) \
53 for ((pos) = (__typeof__(pos)) pw_array_first(array); \
54 pw_array_check(array, pos); \
55 (pos)++)
56
57#define pw_array_consume(pos, array) \
58 for ((pos) = (__typeof__(pos)) pw_array_first(array); \
59 pw_array_check(array, pos); \
60 (pos) = (__typeof__(pos)) pw_array_first(array))
61
62#define pw_array_remove(a,p) \
63({ \
64 (a)->size -= sizeof(*(p)); \
65 memmove(p, SPA_PTROFF((p), sizeof(*(p)), void), \
66 SPA_PTRDIFF(pw_array_end(a),(p))); \
67})
68
70static inline void pw_array_init(struct pw_array *arr, size_t extend)
71{
72 arr->data = NULL;
73 arr->size = arr->alloc = 0;
74 arr->extend = extend;
76
78static inline void pw_array_clear(struct pw_array *arr)
79{
80 free(arr->data);
81 pw_array_init(arr, arr->extend);
82}
85static inline void pw_array_reset(struct pw_array *arr)
86{
87 arr->size = 0;
88}
89
91static inline int pw_array_ensure_size(struct pw_array *arr, size_t size)
92{
93 size_t alloc, need;
94
95 alloc = arr->alloc;
96 need = arr->size + size;
97
98 if (SPA_UNLIKELY(alloc < need)) {
99 void *data;
100 alloc = SPA_MAX(alloc, arr->extend);
101 spa_assert(alloc != 0); /* forgot pw_array_init */
102 while (alloc < need)
103 alloc *= 2;
104 if (SPA_UNLIKELY((data = realloc(arr->data, alloc)) == NULL))
105 return -errno;
106 arr->data = data;
107 arr->alloc = alloc;
108 }
109 return 0;
110}
111
114static inline void *pw_array_add(struct pw_array *arr, size_t size)
115{
116 void *p;
117
118 if (pw_array_ensure_size(arr, size) < 0)
119 return NULL;
120
121 p = SPA_PTROFF(arr->data, arr->size, void);
122 arr->size += size;
123
124 return p;
125}
126
129static inline void *pw_array_add_fixed(struct pw_array *arr, size_t size)
130{
131 void *p;
132
133 if (SPA_UNLIKELY(arr->alloc < arr->size + size)) {
134 errno = ENOSPC;
135 return NULL;
136 }
137
138 p = SPA_PTROFF(arr->data, arr->size, void);
139 arr->size += size;
140
141 return p;
142}
143
145#define pw_array_add_ptr(a,p) \
146 *((void**) pw_array_add(a, sizeof(void*))) = (p)
147
152#ifdef __cplusplus
153} /* extern "C" */
154#endif
155
156#endif /* PIPEWIRE_ARRAY_H */
spa/utils/defs.h
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:96
static void pw_array_init(struct pw_array *arr, size_t extend)
Initialize the array with given extend.
Definition: array.h:75
static void pw_array_clear(struct pw_array *arr)
Clear the array.
Definition: array.h:83
static void * pw_array_add(struct pw_array *arr, size_t size)
Add ref size bytes to arr.
Definition: array.h:119
static void * pw_array_add_fixed(struct pw_array *arr, size_t size)
Add ref size bytes to arr.
Definition: array.h:134
static void pw_array_reset(struct pw_array *arr)
Reset the array.
Definition: array.h:90
#define SPA_UNLIKELY(x)
Definition: defs.h:347
#define SPA_PTROFF(ptr_, offset_, type_)
Return the address (buffer + offset) as pointer of type.
Definition: defs.h:194
#define spa_assert(expr)
Definition: defs.h:402
#define SPA_MAX(a, b)
Definition: defs.h:157
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
Definition: array.h:36
void * data
pointer to array data
Definition: array.h:33