PipeWire: dynamic.h Source File

PipeWire 0.3.79

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
14struct spa_pod_dynamic_builder {
15 struct spa_pod_builder b;
16 void *data;
17 uint32_t extend;
18 uint32_t _padding;
19};
20
21 static int spa_pod_dynamic_builder_overflow(void *data, uint32_t size)
22{
23 struct spa_pod_dynamic_builder *d = (struct spa_pod_dynamic_builder*)data;
24 int32_t old_size = d->b.size;
25 int32_t new_size = SPA_ROUND_UP_N(size, d->extend);
26 void *old_data = d->b.data;
27
28 if (old_data == d->data)
29 d->b.data = NULL;
30 if ((d->b.data = realloc(d->b.data, new_size)) == NULL)
31 return -errno;
32 if (old_data == d->data && d->b.data != old_data && old_size > 0)
33 memcpy(d->b.data, old_data, old_size);
34 d->b.size = new_size;
35 return 0;
36}
37
38static inline void spa_pod_dynamic_builder_init(struct spa_pod_dynamic_builder *builder,
39 void *data, uint32_t size, uint32_t extend)
40{
41 static const struct spa_pod_builder_callbacks spa_pod_dynamic_builder_callbacks = {
42 SPA_VERSION_POD_BUILDER_CALLBACKS,
43 .overflow = spa_pod_dynamic_builder_overflow
44 };
45 builder->b = SPA_POD_BUILDER_INIT(data, size);
46 spa_pod_builder_set_callbacks(&builder->b, &spa_pod_dynamic_builder_callbacks, builder);
47 builder->extend = extend;
48 builder->data = data;
49}
50
51static inline void spa_pod_dynamic_builder_clean(struct spa_pod_dynamic_builder *builder)
52{
53 if (builder->data != builder->b.data)
54 free(builder->b.data);
55}
56
57#ifdef __cplusplus
58} /* extern "C" */
59#endif
60
61#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:320
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:18
spa_pod_dynamic_builder::extend
uint32_t extend
Definition dynamic.h:21
spa_pod_dynamic_builder::_padding
uint32_t _padding
Definition dynamic.h:22
spa_pod_dynamic_builder::b
struct spa_pod_builder b
Definition dynamic.h:19
spa_pod_dynamic_builder::data
void * data
Definition dynamic.h:20