PipeWire 1.1.0
Loading...
Searching...
No Matches
dynamic.h
1/* Simple Plugin API */
2/* SPDX-FileCopyrightText: Copyright © 2018 Wim Taymans */
3/* SPDX-License-Identifier: MIT */
4
5#ifndef SPA_POD_DYNAMIC_H
6#define SPA_POD_DYNAMIC_H
7
8#ifdef __cplusplus
9extern "C" {
10#endif
11
12#include <spa/pod/builder.h>
13#include <spa/utils/cleanup.h>
14
16 struct spa_pod_builder b;
17 void *data;
18 uint32_t extend;
19 uint32_t _padding;
20};
22static int spa_pod_dynamic_builder_overflow(void *data, uint32_t size)
25 int32_t old_size = d->b.size;
26 int32_t new_size = SPA_ROUND_UP_N(size, d->extend);
27 void *old_data = d->b.data, *new_data;
28
29 if (old_data == d->data)
30 d->b.data = NULL;
31 if ((new_data = realloc(d->b.data, new_size)) == NULL)
32 return -errno;
33 if (old_data == d->data && new_data != old_data && old_size > 0)
34 memcpy(new_data, old_data, old_size);
35 d->b.data = new_data;
36 d->b.size = new_size;
37 return 0;
38}
39
40static inline void spa_pod_dynamic_builder_init(struct spa_pod_dynamic_builder *builder,
41 void *data, uint32_t size, uint32_t extend)
42{
43 static const struct spa_pod_builder_callbacks spa_pod_dynamic_builder_callbacks = {
45 .overflow = spa_pod_dynamic_builder_overflow
46 };
47 builder->b = SPA_POD_BUILDER_INIT(data, size);
48 spa_pod_builder_set_callbacks(&builder->b, &spa_pod_dynamic_builder_callbacks, builder);
49 builder->extend = extend;
50 builder->data = data;
51}
52
53static inline void spa_pod_dynamic_builder_clean(struct spa_pod_dynamic_builder *builder)
54{
55 if (builder->data != builder->b.data)
56 free(builder->b.data);
57}
58
59SPA_DEFINE_AUTO_CLEANUP(spa_pod_dynamic_builder, struct spa_pod_dynamic_builder, {
60 spa_pod_dynamic_builder_clean(thing);
61})
62
63#ifdef __cplusplus
64} /* extern "C" */
65#endif
66
67#endif /* SPA_POD_DYNAMIC_H */
spa/pod/builder.h
static void spa_pod_builder_set_callbacks(struct spa_pod_builder *builder, const struct spa_pod_builder_callbacks *callbacks, void *data)
Definition builder.h:71
#define SPA_POD_BUILDER_INIT(buffer, size)
Definition builder.h:62
#define SPA_VERSION_POD_BUILDER_CALLBACKS
Definition builder.h:47
#define SPA_ROUND_UP_N(num, align)
Definition defs.h:332
Definition builder.h:45
Definition builder.h:53
void * data
Definition builder.h:54
uint32_t size
Definition builder.h:55
Definition dynamic.h:19
uint32_t extend
Definition dynamic.h:22
uint32_t _padding
Definition dynamic.h:23
struct spa_pod_builder b
Definition dynamic.h:20
void * data
Definition dynamic.h:21