PipeWire: spa/pod/dynamic.h Source File

PipeWire 1.0.3

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
15struct spa_pod_dynamic_builder {
16 struct spa_pod_builder b;
17 void *data;
18 uint32_t extend;
19 uint32_t _padding;
20};
21
22 static int spa_pod_dynamic_builder_overflow(void *data, uint32_t size)
23{
24 struct spa_pod_dynamic_builder *d = (struct spa_pod_dynamic_builder*)data;
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 = {
44 SPA_VERSION_POD_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 */
builder.h
spa/pod/builder.h
spa_pod_builder_set_callbacks
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
SPA_POD_BUILDER_INIT
#define SPA_POD_BUILDER_INIT(buffer, size)
Definition builder.h:62
SPA_VERSION_POD_BUILDER_CALLBACKS
#define SPA_VERSION_POD_BUILDER_CALLBACKS
Definition builder.h:47
SPA_ROUND_UP_N
#define SPA_ROUND_UP_N(num, align)
Definition defs.h:324
spa_pod_builder_callbacks
Definition builder.h:45
spa_pod_builder_callbacks::overflow
int(* overflow)(void *data, uint32_t size)
Definition builder.h:50
spa_pod_builder
Definition builder.h:53
spa_pod_builder::data
void * data
Definition builder.h:54
spa_pod_builder::size
uint32_t size
Definition builder.h:55
spa_pod_dynamic_builder
Definition dynamic.h:19
spa_pod_dynamic_builder::extend
uint32_t extend
Definition dynamic.h:22
spa_pod_dynamic_builder::_padding
uint32_t _padding
Definition dynamic.h:23
spa_pod_dynamic_builder::b
struct spa_pod_builder b
Definition dynamic.h:20
spa_pod_dynamic_builder::data
void * data
Definition dynamic.h:21