PipeWire: spa/pod/dynamic.h Source File

PipeWire 1.6.7

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#include <spa/pod/builder.h>
9#include <spa/utils/cleanup.h>
10
11#ifdef __cplusplus
12extern "C" {
13#endif
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
23struct 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 if (extend > 0)
57 spa_pod_builder_set_callbacks(&builder->b, &spa_pod_dynamic_builder_callbacks, builder);
58 builder->extend = extend;
59 builder->data = data;
60}
61
62SPA_API_POD_DYNAMIC void spa_pod_dynamic_builder_continue(struct spa_pod_dynamic_builder *builder,
63 struct spa_pod_builder *b)
64{
65 uint32_t remain = b->state.offset >= b->size ? 0 : b->size - b->state.offset;
66 spa_pod_dynamic_builder_init(builder,
67 remain ? SPA_PTROFF(b->data, b->state.offset, void) : NULL,
68 remain, b->callbacks.funcs == NULL ? 0 : 4096);
69}
70
71SPA_API_POD_DYNAMIC void spa_pod_dynamic_builder_clean(struct spa_pod_dynamic_builder *builder)
72{
73 if (builder->data != builder->b.data) {
74 free(builder->b.data);
75 builder->b.data = NULL;
76 }
77}
78
79SPA_DEFINE_AUTO_CLEANUP(spa_pod_dynamic_builder, struct spa_pod_dynamic_builder, {
80 spa_pod_dynamic_builder_clean(thing);
81})
82
83#ifdef __cplusplus
84} /* extern "C" */
85#endif
86
87#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:86
SPA_ROUND_UP_N
#define SPA_ROUND_UP_N(num, align)
Definition defs.h:364
SPA_PTROFF
#define SPA_PTROFF(ptr_, offset_, type_)
Return the address (buffer + offset) as pointer of type.
Definition defs.h:222
spa_callbacks::funcs
const void * funcs
Definition hook.h:127
spa_pod_builder_callbacks
Definition builder.h:55
spa_pod_builder_state::offset
uint32_t offset
Definition builder.h:43
spa_pod_builder
Definition builder.h:63
spa_pod_builder::callbacks
struct spa_callbacks callbacks
Definition builder.h:68
spa_pod_builder::data
void * data
Definition builder.h:64
spa_pod_builder::state
struct spa_pod_builder_state state
Definition builder.h:67
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