1/* Copyright (C) 2023 Netflix Inc.
2 * Author: Xavier Claessens <xavier.claessens@collabora.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#pragma once
21
22#include <glib.h>
23#include <gst/gstconfig.h>
24
25G_BEGIN_DECLS
26
27/**
28 * GstByteArrayInterface:
29 * @data: A pointer to an array of bytes.
30 * @len: Number of bytes in @data.
31 * @resize: Reallocate @data.
32 *
33 * Interface for an array of bytes. It is expected to be subclassed to implement
34 * @resize virtual method using language native array implementation, such as
35 * GLib's #GByteArray, C++'s `std::vector<uint8_t>` or Rust's `Vec<u8>`.
36 *
37 * @resize implementation could allocate more than requested to avoid repeated
38 * reallocations. It can return %FALSE, or be set to %NULL, in the case the
39 * array cannot grow.
40 *
41 * Since: 1.24
42 */
43typedef struct _GstByteArrayInterface GstByteArrayInterface;
44struct _GstByteArrayInterface
45{
46 guint8 *data;
47 gsize len;
48 gboolean (*resize) (GstByteArrayInterface *self, gsize length);
49
50 /* < private > */
51 gpointer _gst_reserved[GST_PADDING];
52};
53
54/**
55 * gst_byte_array_interface_init:
56 * @self: A #GstByteArrayInterface.
57 * @length: New size.
58 *
59 * Initialize #GstByteArrayInterface structure.
60 *
61 * Since: 1.24
62 */
63static inline void
64gst_byte_array_interface_init (GstByteArrayInterface *self)
65{
66 memset (s: self, c: 0, n: sizeof (GstByteArrayInterface));
67}
68
69/**
70 * gst_byte_array_interface_set_size:
71 * @self: A #GstByteArrayInterface.
72 * @length: New size.
73 *
74 * Reallocate data pointer to fit at least @length bytes. @self->len is updated
75 * to @length.
76 *
77 * Returns: %TRUE on success, %FALSE otherwise.
78 * Since: 1.24
79 */
80static inline gboolean
81gst_byte_array_interface_set_size (GstByteArrayInterface *self, gsize length)
82{
83 if (self->resize == NULL || !self->resize (self, length))
84 return FALSE;
85 self->len = length;
86 return TRUE;
87}
88
89/**
90 * gst_byte_array_interface_append:
91 * @self: A #GstByteArrayInterface.
92 * @size: Number of bytes to append to the array.
93 *
94 * Grow the array by @size bytes and return a pointer to the newly added memory.
95 *
96 * Returns: Pointer to added memory, or %NULL if reallocation failed.
97 * Since: 1.24
98 */
99static inline guint8 *
100gst_byte_array_interface_append (GstByteArrayInterface *self, gsize size)
101{
102 gsize orig = self->len;
103 if (!gst_byte_array_interface_set_size (self, length: self->len + size))
104 return NULL;
105 return self->data + orig;
106}
107
108/**
109 * gst_byte_array_interface_append_data:
110 * @self: A #GstByteArrayInterface.
111 * @data: Source data.
112 * @size: Size of @data.
113 *
114 * Append @size bytes from @data, reallocating @self->data pointer if necessary.
115 *
116 * Returns: %TRUE on success, %FALSE otherwise.
117 * Since: 1.24
118 */
119static inline gboolean
120gst_byte_array_interface_append_data (GstByteArrayInterface *self, const guint8 *data, gsize size)
121{
122 guint8 *ptr = gst_byte_array_interface_append (self, size);
123 if (ptr == NULL)
124 return FALSE;
125 memcpy (dest: ptr, src: data, n: size);
126 return TRUE;
127}
128
129G_END_DECLS
130

source code of include/gstreamer-1.0/gst/gstbytearrayinterface.h