PipeWire 0.2.6

array.h
Go to the documentation of this file.
1 /* PipeWire
2 * Copyright (C) 2016 Wim Taymans <wim.taymans@gmail.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 #ifndef __PIPEWIRE_ARRAY_H__
21 #define __PIPEWIRE_ARRAY_H__
22
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26
27 #include <spa/utils/defs.h>
28
36 struct pw_array {
37 void *data;
38 size_t size;
39 size_t alloc;
40 size_t extend;
41 };
42
43 #define PW_ARRAY_INIT(extend) (struct pw_array) { NULL, 0, 0, extend }
44
45 #define pw_array_get_len_s(a,s) ((a)->size / (s))
46 #define pw_array_get_unchecked_s(a,idx,s,t) SPA_MEMBER((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))
51
52 #define pw_array_get_unchecked(a,idx,t) pw_array_get_unchecked_s(a,idx,sizeof(t),t)
53
54 #define pw_array_check_index(a,idx,t) pw_array_check_index_s(a,idx,sizeof(t))
55
56 #define pw_array_for_each(pos, array) \
57 for (pos = (__typeof__(pos)) (array)->data; \
58 (const uint8_t *) pos < ((const uint8_t *) (array)->data + (array)->size); \
59 (pos)++)
60
62 static inline void pw_array_init(struct pw_array *arr, size_t extend)
63 {
64 arr->data = NULL;
65 arr->size = arr->alloc = 0;
66 arr->extend = extend;
67 }
68
70 static inline void pw_array_clear(struct pw_array *arr)
71 {
72 free(arr->data);
73 }
74
76 static inline bool pw_array_ensure_size(struct pw_array *arr, size_t size)
77 {
78 size_t alloc, need;
79
80 alloc = arr->alloc;
81 need = arr->size + size;
82
83 if (SPA_UNLIKELY(alloc < need)) {
84 void *data;
85 alloc = SPA_MAX(alloc, arr->extend);
86 while (alloc < need)
87 alloc *= 2;
88 if (SPA_UNLIKELY((data = realloc(arr->data, alloc)) == NULL))
89 return false;
90 arr->data = data;
91 arr->alloc = alloc;
92 }
93 return true;
94 }
95
98 static inline void *pw_array_add(struct pw_array *arr, size_t size)
99 {
100 void *p;
101
102 if (!pw_array_ensure_size(arr, size))
103 return NULL;
104
105 p = SPA_MEMBER(arr->data, arr->size, void);
106 arr->size += size;
107
108 return p;
109 }
110
113 static inline void *pw_array_add_fixed(struct pw_array *arr, size_t size)
114 {
115 void *p;
116
117 if (SPA_UNLIKELY(arr->alloc < arr->size + size))
118 return NULL;
119
120 p = SPA_MEMBER(arr->data, arr->size, void);
121 arr->size += size;
122
123 return p;
124 }
125
127 #define pw_array_add_ptr(a,p) \
128 *((void**) pw_array_add(a, sizeof(void*))) = (p)
129
130 #ifdef __cplusplus
131 } /* extern "C" */
132 #endif
133
134 #endif /* __PIPEWIRE_ARRAY_H__ */
pw_array::pw_array_add
static void * pw_array_add(struct pw_array *arr, size_t size)
Add ref size bytes to arr.
Definition: array.h:98
pw_array::data
void * data
pointer to array data
Definition: array.h:37
pw_array::pw_array_init
static void pw_array_init(struct pw_array *arr, size_t extend)
Initialize the array with given extend.
Definition: array.h:62
pw_array::pw_array_ensure_size
static bool pw_array_ensure_size(struct pw_array *arr, size_t size)
Make sure size bytes can be added to the array.
Definition: array.h:76
pw_array
An array object.
Definition: array.h:36
pw_array::alloc
size_t alloc
number of allocated memory in data
Definition: array.h:39
pw_array::extend
size_t extend
number of bytes to extend with
Definition: array.h:40
pw_array::size
size_t size
length of array in bytes
Definition: array.h:38
pw_array::pw_array_add_fixed
static void * pw_array_add_fixed(struct pw_array *arr, size_t size)
Add ref size bytes to arr.
Definition: array.h:113