Loading...
Searching...
No Matches
array.h
Go to the documentation of this file.
/* PipeWire */
/* SPDX-FileCopyrightText: Copyright © 2018 Wim Taymans */
/* SPDX-License-Identifier: MIT */
#ifndef PIPEWIRE_ARRAY_H
#define PIPEWIRE_ARRAY_H
#include <errno.h>
#include <spa/utils/defs.h>
#ifdef __cplusplus
extern "C" {
#endif
#ifndef PW_API_ARRAY
#define PW_API_ARRAY static inline
#endif
struct pw_array {
void *data;
size_t size;
size_t alloc;
size_t extend;
* data should not expand */
};
#define PW_ARRAY_INIT(extend) ((struct pw_array) { NULL, 0, 0, (extend) })
#define pw_array_get_len_s(a,s) ((a)->size / (s))
#define pw_array_get_unchecked_s(a,idx,s,t) SPA_PTROFF((a)->data,(idx)*(s),t)
#define pw_array_check_index_s(a,idx,s) ((idx) < pw_array_get_len_s(a,s))
#define pw_array_get_len(a,t) pw_array_get_len_s(a,sizeof(t))
#define pw_array_get_unchecked(a,idx,t) pw_array_get_unchecked_s(a,idx,sizeof(t),t)
#define pw_array_check_index(a,idx,t) pw_array_check_index_s(a,idx,sizeof(t))
#define pw_array_first(a) ((a)->data)
#define pw_array_end(a) SPA_PTROFF((a)->data, (a)->size, void)
#define pw_array_check(a,p) (SPA_PTROFF(p,sizeof(*(p)),void) <= pw_array_end(a))
#define pw_array_for_each(pos, array) \
for ((pos) = (__typeof__(pos)) pw_array_first(array); \
pw_array_check(array, pos); \
(pos)++)
#define pw_array_consume(pos, array) \
for ((pos) = (__typeof__(pos)) pw_array_first(array); \
pw_array_check(array, pos); \
(pos) = (__typeof__(pos)) pw_array_first(array))
#define pw_array_remove(a,p) \
({ \
(a)->size -= sizeof(*(p)); \
memmove(p, SPA_PTROFF((p), sizeof(*(p)), void), \
SPA_PTRDIFF(pw_array_end(a),(p))); \
})
PW_API_ARRAY void pw_array_init(struct pw_array *arr, size_t extend)
{
arr->data = NULL;
arr->size = arr->alloc = 0;
arr->extend = extend;
}
PW_API_ARRAY void pw_array_clear(struct pw_array *arr)
{
if (arr->extend > 0)
free(arr->data);
pw_array_init(arr, arr->extend);
}
PW_API_ARRAY void pw_array_init_static(struct pw_array *arr, void *data, size_t size)
{
arr->data = data;
arr->alloc = size;
arr->size = arr->extend = 0;
}
PW_API_ARRAY void pw_array_reset(struct pw_array *arr)
{
arr->size = 0;
}
PW_API_ARRAY int pw_array_ensure_size(struct pw_array *arr, size_t size)
{
size_t alloc, need;
alloc = arr->alloc;
need = arr->size + size;
if (SPA_UNLIKELY(alloc < need)) {
void *data;
if (arr->extend == 0)
return -ENOSPC;
alloc = SPA_ROUND_UP(need, arr->extend);
if (SPA_UNLIKELY((data = realloc(arr->data, alloc)) == NULL))
return -errno;
arr->data = data;
arr->alloc = alloc;
}
return 0;
}
PW_API_ARRAY void *pw_array_add(struct pw_array *arr, size_t size)
{
void *p;
if (pw_array_ensure_size(arr, size) < 0)
return NULL;
p = SPA_PTROFF(arr->data, arr->size, void);
arr->size += size;
return p;
}
PW_API_ARRAY int pw_array_add_ptr(struct pw_array *arr, void *ptr)
{
void **p = (void **)pw_array_add(arr, sizeof(void*));
if (p == NULL)
return -errno;
*p = ptr;
return 0;
}
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* PIPEWIRE_ARRAY_H */
PW_API_ARRAY
#define PW_API_ARRAY
Definition array.h:22
defs.h
spa/utils/defs.h
pw_array_ensure_size
PW_API_ARRAY 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:115
pw_array_reset
PW_API_ARRAY void pw_array_reset(struct pw_array *arr)
Reset the array.
Definition array.h:109
pw_array_add_ptr
PW_API_ARRAY int pw_array_add_ptr(struct pw_array *arr, void *ptr)
Add a pointer to array.
Definition array.h:153
pw_array_init_static
PW_API_ARRAY void pw_array_init_static(struct pw_array *arr, void *data, size_t size)
Initialize a static array.
Definition array.h:101
pw_array_add
PW_API_ARRAY void * pw_array_add(struct pw_array *arr, size_t size)
Add ref size bytes to arr.
Definition array.h:138
pw_array_clear
PW_API_ARRAY void pw_array_clear(struct pw_array *arr)
Clear the array.
Definition array.h:93
pw_array_init
PW_API_ARRAY void pw_array_init(struct pw_array *arr, size_t extend)
Initialize the array with given extend.
Definition array.h:85
SPA_ROUND_UP
#define SPA_ROUND_UP(num, value)
Definition defs.h:352
SPA_UNLIKELY
#define SPA_UNLIKELY(x)
Definition defs.h:398
SPA_PTROFF
#define SPA_PTROFF(ptr_, offset_, type_)
Return the address (buffer + offset) as pointer of type.
Definition defs.h:222
pw_array
Definition array.h:38
pw_array::size
size_t size
length of array in bytes
Definition array.h:40
pw_array::alloc
size_t alloc
number of allocated memory in data
Definition array.h:41
pw_array::extend
size_t extend
number of bytes to extend with, 0 when the data should not expand
Definition array.h:42
pw_array::data
void * data
pointer to array data
Definition array.h:39