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 
36 /** \class pw_array
37  *
38  * \brief An array object
39  *
40  * The array is a dynamically resizable data structure that can
41  * hold items of the same size.
42  */
43 struct pw_array {
44 	void *data;		/**< pointer to array data */
45 	size_t size;		/**< length of array in bytes */
46 	size_t alloc;		/**< number of allocated memory in \a data */
47 	size_t extend;		/**< number of bytes to extend with */
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 
56 /** Get the number of items of type \a t in array \memberof pw_array */
57 #define pw_array_get_len(a,t)			pw_array_get_len_s(a,sizeof(t))
58 /** Get the item with index \a idx and type \a t from array \memberof pw_array */
59 #define pw_array_get_unchecked(a,idx,t)		pw_array_get_unchecked_s(a,idx,sizeof(t),t)
60 /** Check if an item with index \a idx and type \a t exist in array \memberof pw_array */
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_consume(pos, array)					\
73 	for (pos = (__typeof__(pos)) pw_array_first(array);		\
74 	     pw_array_check(array, pos);				\
75 	     pos = (__typeof__(pos)) pw_array_first(array))
76 
77 #define pw_array_remove(a,p)						\
78 ({									\
79 	(a)->size -= sizeof(*(p));					\
80 	memmove(p, SPA_MEMBER((p), sizeof(*(p)), void),			\
81                 SPA_PTRDIFF(pw_array_end(a),(p)));			\
82 })
83 
84 /** Initialize the array with given extend \memberof pw_array */
pw_array_init(struct pw_array * arr,size_t extend)85 static inline void pw_array_init(struct pw_array *arr, size_t extend)
86 {
87 	arr->data = NULL;
88 	arr->size = arr->alloc = 0;
89 	arr->extend = extend;
90 }
91 
92 /** Clear the array */
pw_array_clear(struct pw_array * arr)93 static inline void pw_array_clear(struct pw_array *arr)
94 {
95 	free(arr->data);
96 }
97 
98 /** Reset the array */
pw_array_reset(struct pw_array * arr)99 static inline void pw_array_reset(struct pw_array *arr)
100 {
101 	arr->size = 0;
102 }
103 
104 /** Make sure \a size bytes can be added to the array \memberof pw_array */
pw_array_ensure_size(struct pw_array * arr,size_t size)105 static inline int pw_array_ensure_size(struct pw_array *arr, size_t size)
106 {
107 	size_t alloc, need;
108 
109 	alloc = arr->alloc;
110 	need = arr->size + size;
111 
112 	if (SPA_UNLIKELY(alloc < need)) {
113 		void *data;
114 		alloc = SPA_MAX(alloc, arr->extend);
115 		while (alloc < need)
116 			alloc *= 2;
117 		if (SPA_UNLIKELY((data = realloc(arr->data, alloc)) == NULL))
118 			return -errno;
119 		arr->data = data;
120 		arr->alloc = alloc;
121 	}
122 	return 0;
123 }
124 
125 /** Add \a ref size bytes to \a arr. A pointer to memory that can
126  * hold at least \a size bytes is returned \memberof pw_array */
pw_array_add(struct pw_array * arr,size_t size)127 static 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_MEMBER(arr->data, arr->size, void);
135 	arr->size += size;
136 
137 	return p;
138 }
139 
140 /** Add \a ref size bytes to \a arr. When there is not enough memory to
141  * hold \a size bytes, NULL is returned \memberof pw_array */
pw_array_add_fixed(struct pw_array * arr,size_t size)142 static inline void *pw_array_add_fixed(struct pw_array *arr, size_t size)
143 {
144 	void *p;
145 
146 	if (SPA_UNLIKELY(arr->alloc < arr->size + size)) {
147 		errno = ENOSPC;
148 		return NULL;
149 	}
150 
151 	p = SPA_MEMBER(arr->data, arr->size, void);
152 	arr->size += size;
153 
154 	return p;
155 }
156 
157 /** Add a pointer to array \memberof pw_array */
158 #define pw_array_add_ptr(a,p)					\
159 	*((void**) pw_array_add(a, sizeof(void*))) = (p)
160 
161 #ifdef __cplusplus
162 }  /* extern "C" */
163 #endif
164 
165 #endif /* PIPEWIRE_ARRAY_H */
166