2 * Copyright (C) 2009 Wim Taymans <wim.taymans@gmail.be>
4 * gstmemory.h: Header for memory blocks
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 #ifndef __GST_MEMORY_H__
24 #define __GST_MEMORY_H__
26 #include <gst/gstconfig.h>
28 #include <glib-object.h>
29 #include <gst/gstminiobject.h>
30 #include <gst/gstobject.h>
34 GST_API GType _gst_memory_type;
35 #define GST_TYPE_MEMORY (_gst_memory_type)
38 GType gst_memory_get_type(void);
40 typedef struct _GstMemory GstMemory;
41 typedef struct _GstAllocator GstAllocator;
43 #define GST_MEMORY_CAST(mem) ((GstMemory *)(mem))
47 * @GST_MEMORY_FLAG_READONLY: memory is readonly. It is not allowed to map the
48 * memory with #GST_MAP_WRITE.
49 * @GST_MEMORY_FLAG_NO_SHARE: memory must not be shared. Copies will have to be
50 * made when this memory needs to be shared between buffers. (DEPRECATED:
51 * do not use in new code, instead you should create a custom GstAllocator for
52 * memory pooling instead of relying on the GstBuffer they were originally
54 * @GST_MEMORY_FLAG_ZERO_PREFIXED: the memory prefix is filled with 0 bytes
55 * @GST_MEMORY_FLAG_ZERO_PADDED: the memory padding is filled with 0 bytes
56 * @GST_MEMORY_FLAG_PHYSICALLY_CONTIGUOUS: the memory is physically
57 * contiguous. (Since: 1.2)
58 * @GST_MEMORY_FLAG_NOT_MAPPABLE: the memory can't be mapped via
59 * gst_memory_map() without any preconditions. (Since: 1.2)
60 * @GST_MEMORY_FLAG_LAST: first flag that can be used for custom purposes
62 * Flags for wrapped memory.
65 GST_MEMORY_FLAG_READONLY = GST_MINI_OBJECT_FLAG_LOCK_READONLY,
66 GST_MEMORY_FLAG_NO_SHARE = (GST_MINI_OBJECT_FLAG_LAST << 0),
67 GST_MEMORY_FLAG_ZERO_PREFIXED = (GST_MINI_OBJECT_FLAG_LAST << 1),
68 GST_MEMORY_FLAG_ZERO_PADDED = (GST_MINI_OBJECT_FLAG_LAST << 2),
69 GST_MEMORY_FLAG_PHYSICALLY_CONTIGUOUS = (GST_MINI_OBJECT_FLAG_LAST << 3),
70 GST_MEMORY_FLAG_NOT_MAPPABLE = (GST_MINI_OBJECT_FLAG_LAST << 4),
72 GST_MEMORY_FLAG_LAST = (GST_MINI_OBJECT_FLAG_LAST << 16)
79 * A flags word containing #GstMemoryFlags flags set on @mem
81 #define GST_MEMORY_FLAGS(mem) GST_MINI_OBJECT_FLAGS (mem)
83 * GST_MEMORY_FLAG_IS_SET:
85 * @flag: the #GstMemoryFlags to check.
87 * Gives the status of a specific flag on a @mem.
89 #define GST_MEMORY_FLAG_IS_SET(mem,flag) GST_MINI_OBJECT_FLAG_IS_SET (mem,flag)
91 * GST_MEMORY_FLAG_UNSET:
93 * @flag: the #GstMemoryFlags to clear.
95 * Clear a specific flag on a @mem.
97 #define GST_MEMORY_FLAG_UNSET(mem,flag) GST_MINI_OBJECT_FLAG_UNSET (mem, flag)
100 * GST_MEMORY_IS_READONLY:
101 * @mem: a #GstMemory.
103 * Check if @mem is readonly.
105 #define GST_MEMORY_IS_READONLY(mem) GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_READONLY)
107 * GST_MEMORY_IS_NO_SHARE:
108 * @mem: a #GstMemory.
110 * Check if @mem cannot be shared between buffers
112 #define GST_MEMORY_IS_NO_SHARE(mem) GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_NO_SHARE)
114 * GST_MEMORY_IS_ZERO_PREFIXED:
115 * @mem: a #GstMemory.
117 * Check if the prefix in @mem is 0 filled.
119 #define GST_MEMORY_IS_ZERO_PREFIXED(mem) GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_ZERO_PREFIXED)
121 * GST_MEMORY_IS_ZERO_PADDED:
122 * @mem: a #GstMemory.
124 * Check if the padding in @mem is 0 filled.
126 #define GST_MEMORY_IS_ZERO_PADDED(mem) GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_ZERO_PADDED)
129 * GST_MEMORY_IS_PHYSICALLY_CONTIGUOUS:
130 * @mem: a #GstMemory.
132 * Check if @mem is physically contiguous.
136 #define GST_MEMORY_IS_PHYSICALLY_CONTIGUOUS(mem) GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_PHYSICALLY_CONTIGUOUS)
139 * GST_MEMORY_IS_NOT_MAPPABLE:
140 * @mem: a #GstMemory.
142 * Check if @mem can't be mapped via gst_memory_map() without any preconditions
146 #define GST_MEMORY_IS_NOT_MAPPABLE(mem) GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_NOT_MAPPABLE)
150 * @mini_object: parent structure
151 * @allocator: pointer to the #GstAllocator
152 * @parent: parent memory block
153 * @maxsize: the maximum size allocated
154 * @align: the alignment of the memory
155 * @offset: the offset where valid data starts
156 * @size: the size of valid data
158 * Base structure for memory implementations. Custom memory will put this structure
159 * as the first member of their structure.
162 GstMiniObject mini_object;
164 GstAllocator *allocator;
175 * @GST_MAP_READ: map for read access
176 * @GST_MAP_WRITE: map for write access
177 * @GST_MAP_FLAG_LAST: first flag that can be used for custom purposes
179 * Flags used when mapping memory
182 GST_MAP_READ = GST_LOCK_FLAG_READ,
183 GST_MAP_WRITE = GST_LOCK_FLAG_WRITE,
185 GST_MAP_FLAG_LAST = (1 << 16)
189 * GST_MAP_READWRITE: (value 3) (type GstMapFlags)
191 * GstMapFlags value alias for GST_MAP_READ | GST_MAP_WRITE
193 #define GST_MAP_READWRITE ((GstMapFlags) (GST_MAP_READ | GST_MAP_WRITE))
198 * @memory: a pointer to the mapped memory
199 * @flags: flags used when mapping the memory
200 * @data: (array length=size): a pointer to the mapped data
201 * @size: the valid size in @data
202 * @maxsize: the maximum bytes in @data
203 * @user_data: extra private user_data that the implementation of the memory
204 * can use to store extra info.
206 * A structure containing the result of a map operation such as
207 * gst_memory_map(). It contains the data and size.
209 * #GstMapInfo cannot be used with g_auto() because it is ambiguous whether it
210 * needs to be unmapped using gst_buffer_unmap() or gst_memory_unmap(). Instead,
211 * #GstBufferMapInfo and #GstMemoryMapInfo can be used in that case.
220 gpointer user_data[4];
223 gpointer _gst_reserved[GST_PADDING];
229 * Initializer for #GstMapInfo
231 #define GST_MAP_INFO_INIT { NULL, (GstMapFlags) 0, NULL, 0, 0, { NULL, NULL, NULL, NULL}, {NULL, NULL, NULL, NULL}}
234 * GstMemoryMapFunction:
236 * @maxsize: size to map
237 * @flags: access mode for the memory
239 * Get the memory of @mem that can be accessed according to the mode specified
240 * in @flags. The function should return a pointer that contains at least
243 * Returns: a pointer to memory of which at least @maxsize bytes can be
244 * accessed according to the access pattern in @flags.
246 typedef gpointer (*GstMemoryMapFunction) (GstMemory *mem, gsize maxsize, GstMapFlags flags);
249 * GstMemoryMapFullFunction:
251 * @info: the #GstMapInfo to map with
252 * @maxsize: size to map
254 * Get the memory of @mem that can be accessed according to the mode specified
255 * in @info's flags. The function should return a pointer that contains at least
258 * Returns: a pointer to memory of which at least @maxsize bytes can be
259 * accessed according to the access pattern in @info's flags.
261 typedef gpointer (*GstMemoryMapFullFunction) (GstMemory *mem, GstMapInfo * info, gsize maxsize);
264 * GstMemoryUnmapFunction:
267 * Release the pointer previously retrieved with gst_memory_map().
269 typedef void (*GstMemoryUnmapFunction) (GstMemory *mem);
272 * GstMemoryUnmapFullFunction:
274 * @info: a #GstMapInfo
276 * Release the pointer previously retrieved with gst_memory_map() with @info.
278 typedef void (*GstMemoryUnmapFullFunction) (GstMemory *mem, GstMapInfo * info);
281 * GstMemoryCopyFunction:
284 * @size: a size or -1
286 * Copy @size bytes from @mem starting at @offset and return them wrapped in a
287 * new GstMemory object.
288 * If @size is set to -1, all bytes starting at @offset are copied.
290 * Returns: a new #GstMemory object wrapping a copy of the requested region in
293 typedef GstMemory * (*GstMemoryCopyFunction) (GstMemory *mem, gssize offset, gssize size);
296 * GstMemoryShareFunction:
299 * @size: a size or -1
301 * Share @size bytes from @mem starting at @offset and return them wrapped in a
302 * new GstMemory object. If @size is set to -1, all bytes starting at @offset are
303 * shared. This function does not make a copy of the bytes in @mem.
305 * Returns: a new #GstMemory object sharing the requested region in @mem.
307 typedef GstMemory * (*GstMemoryShareFunction) (GstMemory *mem, gssize offset, gssize size);
310 * GstMemoryIsSpanFunction:
311 * @mem1: a #GstMemory
312 * @mem2: a #GstMemory
313 * @offset: a result offset
315 * Check if @mem1 and @mem2 occupy contiguous memory and return the offset of
316 * @mem1 in the parent buffer in @offset.
318 * Returns: %TRUE if @mem1 and @mem2 are in contiguous memory.
320 typedef gboolean (*GstMemoryIsSpanFunction) (GstMemory *mem1, GstMemory *mem2, gsize *offset);
323 void gst_memory_init (GstMemory *mem, GstMemoryFlags flags,
324 GstAllocator *allocator, GstMemory *parent,
325 gsize maxsize, gsize align,
326 gsize offset, gsize size);
328 gboolean gst_memory_is_type (GstMemory *mem, const gchar *mem_type);
330 #ifndef GST_DISABLE_MINIOBJECT_INLINE_FUNCTIONS
332 static inline GstMemory *
333 gst_memory_ref (GstMemory * memory)
335 return (GstMemory *) gst_mini_object_ref (GST_MINI_OBJECT_CAST (memory));
339 gst_memory_unref (GstMemory * memory)
341 gst_mini_object_unref (GST_MINI_OBJECT_CAST (memory));
343 #else /* GST_DISABLE_MINIOBJECT_INLINE_FUNCTIONS */
345 GstMemory * gst_memory_ref (GstMemory * memory);
348 void gst_memory_unref (GstMemory * memory);
349 #endif /* GST_DISABLE_MINIOBJECT_INLINE_FUNCTIONS */
351 /* getting/setting memory properties */
354 gsize gst_memory_get_sizes (GstMemory *mem, gsize *offset, gsize *maxsize);
357 void gst_memory_resize (GstMemory *mem, gssize offset, gsize size);
359 #define gst_memory_lock(m,f) gst_mini_object_lock (GST_MINI_OBJECT_CAST (m), (f))
360 #define gst_memory_unlock(m,f) gst_mini_object_unlock (GST_MINI_OBJECT_CAST (m), (f))
361 #define gst_memory_is_writable(m) gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (m))
363 * gst_memory_make_writable:
364 * @m: (transfer full): a #GstMemory
366 * Returns a writable copy of @m. If the source memory is
367 * already writable, this will simply return the same memory.
369 * Returns: (transfer full) (nullable): a writable memory (which may or may not be the
370 * same as @m) or %NULL if copying is required but not possible.
372 #define gst_memory_make_writable(m) GST_MEMORY_CAST (gst_mini_object_make_writable (GST_MINI_OBJECT_CAST (m)))
374 /* retrieving data */
377 GstMemory * gst_memory_make_mapped (GstMemory *mem, GstMapInfo *info, GstMapFlags flags) G_GNUC_WARN_UNUSED_RESULT;
380 gboolean gst_memory_map (GstMemory *mem, GstMapInfo *info, GstMapFlags flags);
383 void gst_memory_unmap (GstMemory *mem, GstMapInfo *info);
385 /* copy and subregions */
388 GstMemory * gst_memory_copy (GstMemory *mem, gssize offset, gssize size) G_GNUC_WARN_UNUSED_RESULT;
391 GstMemory * gst_memory_share (GstMemory *mem, gssize offset, gssize size) G_GNUC_WARN_UNUSED_RESULT;
396 gboolean gst_memory_is_span (GstMemory *mem1, GstMemory *mem2, gsize *offset);
398 G_DEFINE_AUTOPTR_CLEANUP_FUNC(GstMemory, gst_memory_unref)
400 G_DEFINE_AUTOPTR_CLEANUP_FUNC(GstAllocator, gst_object_unref)
403 * GstMemoryMapInfo: (skip):
405 * Alias for #GstMapInfo to be used with g_auto():
407 * void my_func(GstMemory *mem)
409 * g_auto(GstMemoryMapInfo) map = GST_MAP_INFO_INIT;
410 * if (!gst_memory_map(mem, &map, GST_MAP_READWRITE))
413 * // No need to call gst_memory_unmap()
417 * #GstMapInfo cannot be used with g_auto() because it is ambiguous whether it
418 * needs to be unmapped using gst_buffer_unmap() or gst_memory_unmap().
420 * See also #GstBufferMapInfo.
424 typedef GstMapInfo GstMemoryMapInfo;
426 static inline void _gst_memory_map_info_clear(GstMemoryMapInfo *info)
428 if (G_LIKELY (info->memory)) {
429 gst_memory_unmap (info->memory, info);
433 G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(GstMemoryMapInfo, _gst_memory_map_info_clear)
437 #endif /* __GST_MEMORY_H__ */