PipeWire  0.3.66
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 
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 {
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 
38 static 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 = {
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 
51 static 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 */
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:316
Definition: builder.h:45
Definition: builder.h:53
void * data
Definition: builder.h:54
uint32_t size
Definition: builder.h:55
Definition: dynamic.h:18
uint32_t extend
Definition: dynamic.h:21
uint32_t _padding
Definition: dynamic.h:22
struct spa_pod_builder b
Definition: dynamic.h:19
void * data
Definition: dynamic.h:20