PipeWire  0.3.4
array.h
Go to the documentation of this file.
1 /* PipeWire
2  *
3  * Copyright © 2018 Wim Taymans
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24 
25 #ifndef PIPEWIRE_ARRAY_H
26 #define PIPEWIRE_ARRAY_H
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 #include <errno.h>
33 
34 #include <spa/utils/defs.h>
35 
43 struct pw_array {
44  void *data;
45  size_t size;
46  size_t alloc;
47  size_t extend;
48 };
49 
50 #define PW_ARRAY_INIT(extend) (struct pw_array) { NULL, 0, 0, extend }
51 
52 #define pw_array_get_len_s(a,s) ((a)->size / (s))
53 #define pw_array_get_unchecked_s(a,idx,s,t) SPA_MEMBER((a)->data,(idx)*(s),t)
54 #define pw_array_check_index_s(a,idx,s) ((idx) < pw_array_get_len_s(a,s))
55 
57 #define pw_array_get_len(a,t) pw_array_get_len_s(a,sizeof(t))
58 
59 #define pw_array_get_unchecked(a,idx,t) pw_array_get_unchecked_s(a,idx,sizeof(t),t)
60 
61 #define pw_array_check_index(a,idx,t) pw_array_check_index_s(a,idx,sizeof(t))
62 
63 #define pw_array_first(a) ((a)->data)
64 #define pw_array_end(a) SPA_MEMBER((a)->data, (a)->size, void)
65 #define pw_array_check(a,p) (SPA_MEMBER(p,sizeof(*p),void) <= pw_array_end(a))
66 
67 #define pw_array_for_each(pos, array) \
68  for (pos = (__typeof__(pos)) pw_array_first(array); \
69  pw_array_check(array, pos); \
70  (pos)++)
71 
72 #define pw_array_remove(a,p) \
73 ({ \
74  (a)->size -= sizeof(*(p)); \
75  memmove(p, SPA_MEMBER((p), sizeof(*(p)), void), \
76  SPA_PTRDIFF(pw_array_end(a),(p))); \
77 })
78 
80 static inline void pw_array_init(struct pw_array *arr, size_t extend)
81 {
82  arr->data = NULL;
83  arr->size = arr->alloc = 0;
84  arr->extend = extend;
85 }
86 
88 static inline void pw_array_clear(struct pw_array *arr)
89 {
90  free(arr->data);
91 }
92 
94 static inline void pw_array_reset(struct pw_array *arr)
95 {
96  arr->size = 0;
97 }
98 
100 static inline int pw_array_ensure_size(struct pw_array *arr, size_t size)
101 {
102  size_t alloc, need;
103 
104  alloc = arr->alloc;
105  need = arr->size + size;
106 
107  if (SPA_UNLIKELY(alloc < need)) {
108  void *data;
109  alloc = SPA_MAX(alloc, arr->extend);
110  while (alloc < need)
111  alloc *= 2;
112  if (SPA_UNLIKELY((data = realloc(arr->data, alloc)) == NULL))
113  return -errno;
114  arr->data = data;
115  arr->alloc = alloc;
116  }
117  return 0;
118 }
119 
122 static inline void *pw_array_add(struct pw_array *arr, size_t size)
123 {
124  void *p;
125 
126  if (pw_array_ensure_size(arr, size) < 0)
127  return NULL;
128 
129  p = SPA_MEMBER(arr->data, arr->size, void);
130  arr->size += size;
131 
132  return p;
133 }
134 
137 static inline void *pw_array_add_fixed(struct pw_array *arr, size_t size)
138 {
139  void *p;
140 
141  if (SPA_UNLIKELY(arr->alloc < arr->size + size)) {
142  errno = ENOSPC;
143  return NULL;
144  }
145 
146  p = SPA_MEMBER(arr->data, arr->size, void);
147  arr->size += size;
148 
149  return p;
150 }
151 
153 #define pw_array_add_ptr(a,p) \
154  *((void**) pw_array_add(a, sizeof(void*))) = (p)
155 
156 #ifdef __cplusplus
157 } /* extern "C" */
158 #endif
159 
160 #endif /* PIPEWIRE_ARRAY_H */
pw_array::alloc
size_t alloc
number of allocated memory in data
Definition: array.h:46
data
Definition: filter.c:70
pw_array::size
size_t size
length of array in bytes
Definition: array.h:45
pw_array::extend
size_t extend
number of bytes to extend with
Definition: array.h:47
pw_array
An array object.
Definition: array.h:43
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:137
pw_array::pw_array_ensure_size
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:100
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:122
pw_array::data
void * data
pointer to array data
Definition: array.h:44
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:80