PipeWire 1.4.5 |

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
9 extern "C" {
10#endif
11
12#include <spa/pod/builder.h>
13#include <spa/utils/cleanup.h>
14
15#ifndef SPA_API_POD_DYNAMIC
16 #ifdef SPA_API_IMPL
17 #define SPA_API_POD_DYNAMIC SPA_API_IMPL
18 #else
19 #define SPA_API_POD_DYNAMIC static inline
20 #endif
21#endif
22
23 struct spa_pod_dynamic_builder {
24 struct spa_pod_builder b;
25 void *data;
26 uint32_t extend;
27 uint32_t _padding;
28};
29
30 static int spa_pod_dynamic_builder_overflow(void *data, uint32_t size)
31{
32 struct spa_pod_dynamic_builder *d = (struct spa_pod_dynamic_builder*)data;
33 int32_t old_size = d->b.size;
34 int32_t new_size = SPA_ROUND_UP_N(size, d->extend);
35 void *old_data = d->b.data, *new_data;
36
37 if (old_data == d->data)
38 d->b.data = NULL;
39 if ((new_data = realloc(d->b.data, new_size)) == NULL)
40 return -errno;
41 if (old_data == d->data && new_data != old_data && old_size > 0)
42 memcpy(new_data, old_data, old_size);
43 d->b.data = new_data;
44 d->b.size = new_size;
45 return 0;
46}
47
48SPA_API_POD_DYNAMIC void spa_pod_dynamic_builder_init(struct spa_pod_dynamic_builder *builder,
49 void *data, uint32_t size, uint32_t extend)
50{
51 static const struct spa_pod_builder_callbacks spa_pod_dynamic_builder_callbacks = {
52 .version = SPA_VERSION_POD_BUILDER_CALLBACKS,
53 .overflow = spa_pod_dynamic_builder_overflow
54 };
55 builder->b = SPA_POD_BUILDER_INIT(data, size);
56 spa_pod_builder_set_callbacks(&builder->b, &spa_pod_dynamic_builder_callbacks, builder);
57 builder->extend = extend;
58 builder->data = data;
59}
60
61SPA_API_POD_DYNAMIC void spa_pod_dynamic_builder_clean(struct spa_pod_dynamic_builder *builder)
62{
63 if (builder->data != builder->b.data)
64 free(builder->b.data);
65}
66
67SPA_DEFINE_AUTO_CLEANUP(spa_pod_dynamic_builder, struct spa_pod_dynamic_builder, {
68 spa_pod_dynamic_builder_clean(thing);
69})
70
71#ifdef __cplusplus
72} /* extern "C" */
73#endif
74
75#endif /* SPA_POD_DYNAMIC_H */
builder.h
spa/pod/builder.h
SPA_POD_BUILDER_INIT
#define SPA_POD_BUILDER_INIT(buffer, size)
Definition builder.h:72
SPA_VERSION_POD_BUILDER_CALLBACKS
#define SPA_VERSION_POD_BUILDER_CALLBACKS
Definition builder.h:57
spa_pod_builder_set_callbacks
SPA_API_POD_BUILDER void spa_pod_builder_set_callbacks(struct spa_pod_builder *builder, const struct spa_pod_builder_callbacks *callbacks, void *data)
Definition builder.h:81
SPA_ROUND_UP_N
#define SPA_ROUND_UP_N(num, align)
Definition defs.h:360
spa_pod_builder_callbacks
Definition builder.h:55
spa_pod_builder::data
void * data
Definition builder.h:64
spa_pod_builder::size
uint32_t size
Definition builder.h:65
spa_pod_dynamic_builder
Definition dynamic.h:29
spa_pod_dynamic_builder::extend
uint32_t extend
Definition dynamic.h:32
spa_pod_dynamic_builder::_padding
uint32_t _padding
Definition dynamic.h:33
spa_pod_dynamic_builder::b
struct spa_pod_builder b
Definition dynamic.h:30
spa_pod_dynamic_builder::data
void * data
Definition dynamic.h:31