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., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 #ifndef __GST_MEMORY_H__
24 #define __GST_MEMORY_H__
26 #include <gst/gstconfig.h>
28 #include <glib-object.h>
32 #define GST_TYPE_MEMORY (gst_memory_get_type())
33 GType gst_memory_get_type(void);
35 #define GST_TYPE_ALLOCATOR (gst_allocator_get_type())
36 GType gst_allocator_get_type(void);
38 typedef struct _GstMemory GstMemory;
39 typedef struct _GstMemoryInfo GstMemoryInfo;
40 typedef struct _GstAllocator GstAllocator;
42 GST_EXPORT gsize gst_memory_alignment;
44 #define GST_MEMORY_CAST(mem) ((GstMemory *)(mem))
48 * @GST_MEMORY_FLAG_READONLY: memory is readonly. It is not allowed to map the
49 * memory with #GST_MAP_WRITE.
50 * @GST_MEMORY_FLAG_NO_SHARE: memory must not be shared. Copies will have to be
51 * made when this memory needs to be shared between buffers.
52 * @GST_MEMORY_FLAG_ZERO_PREFIXED: the memory prefix is filled with 0 bytes
53 * @GST_MEMORY_FLAG_ZERO_PADDED: the memory padding is filled with 0 bytes
54 * @GST_MEMORY_FLAG_LAST: first flag that can be used for custom purposes
56 * Flags for wrapped memory.
59 GST_MEMORY_FLAG_READONLY = (1 << 0),
60 GST_MEMORY_FLAG_NO_SHARE = (1 << 1),
61 GST_MEMORY_FLAG_ZERO_PREFIXED = (1 << 2),
62 GST_MEMORY_FLAG_ZERO_PADDED = (1 << 3),
64 GST_MEMORY_FLAG_LAST = (1 << 16)
71 * A flags word containing #GstMemoryFlags flags set on @mem
73 #define GST_MEMORY_FLAGS(mem) (GST_MEMORY_CAST (mem)->flags)
75 * GST_MEMORY_FLAG_IS_SET:
77 * @flag: the #GstMemoryFlags to check.
79 * Gives the status of a specific flag on a @mem.
81 #define GST_MEMORY_FLAG_IS_SET(mem,flag) !!(GST_MEMORY_FLAGS (mem) & (flag))
83 * GST_MEMORY_FLAG_UNSET:
85 * @flag: the #GstMemoryFlags to clear.
87 * Clear a specific flag on a @mem.
89 #define GST_MEMORY_FLAG_UNSET(mem,flag) (GST_MEMORY_FLAGS (mem) &= ~(flag))
92 * GST_MEMORY_IS_READONLY:
95 * Check if @mem is readonly.
97 #define GST_MEMORY_IS_READONLY(mem) GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_READONLY)
99 * GST_MEMORY_IS_ZERO_PREFIXED:
100 * @mem: a #GstMemory.
102 * Check if the prefix in @mem is 0 filled.
104 #define GST_MEMORY_IS_ZERO_PREFIXED(mem) GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_ZERO_PREFIXED)
106 * GST_MEMORY_IS_ZERO_PADDED:
107 * @mem: a #GstMemory.
109 * Check if the padding in @mem is 0 filled.
111 #define GST_MEMORY_IS_ZERO_PADDED(mem) GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_ZERO_PADDED)
116 * @allocator: pointer to the #GstAllocator
117 * @flags: memory flags
118 * @refcount: refcount
119 * @parent: parent memory block
120 * @state: private state
121 * @maxsize: the maximum size allocated
122 * @align: the alignment of the memory
123 * @offset: the offset where valid data starts
124 * @size: the size of valid data
126 * Base structure for memory implementations. Custom memory will put this structure
127 * as the first member of their structure.
130 GstAllocator *allocator;
132 GstMemoryFlags flags;
144 * @GST_MAP_READ: map for read access
145 * @GST_MAP_WRITE: map for write access
146 * @GST_MAP_FLAG_LAST: first flag that can be used for custom purposes
148 * Flags used when mapping memory
151 GST_MAP_READ = (1 << 0),
152 GST_MAP_WRITE = (1 << 1),
154 GST_MAP_FLAG_LAST = (1 << 16)
159 * @memory: a pointer to the mapped memory
160 * @flags: flags used when mapping the memory
161 * @data: a pointer to the mapped data
162 * @size: the valid size in @data
163 * @maxsize: the maximum bytes in @data
164 * @user_data: extra private user_data that the implementation of the memory
165 * can use to store extra info.
167 * A structure containing the result of a map operation such as
168 * gst_memory_map(). It contains the data and size.
177 gpointer user_data[4];
180 #define GST_MAP_INFO_INIT { NULL, 0, NULL, 0, 0, }
185 * Map for readwrite access
187 #define GST_MAP_READWRITE (GST_MAP_READ | GST_MAP_WRITE)
190 * GST_ALLOCATOR_SYSMEM:
192 * The allocator name for the default system memory allocator
194 #define GST_ALLOCATOR_SYSMEM "SystemMemory"
197 * GstAllocatorAllocFunction:
198 * @allocator: a #GstAllocator
200 * @maxsize: the maxsize
201 * @offset: the offset
203 * @align: the alignment
204 * @user_data: user data
206 * Allocate a new #GstMemory from @allocator that can hold at least @maxsize bytes
207 * and is aligned to (@align + 1) bytes.
209 * The offset and size of the memory should be set and the prefix/padding must
210 * be filled with 0 if @flags contains #GST_MEMORY_FLAG_ZERO_PREFIXED and
211 * #GST_MEMORY_FLAG_ZERO_PADDED respectively.
213 * @user_data is the data that was used when creating @allocator.
215 * Returns: a newly allocated #GstMemory. Free with gst_memory_unref()
217 typedef GstMemory * (*GstAllocatorAllocFunction) (GstAllocator *allocator,
218 GstMemoryFlags flags,
219 gsize maxsize, gsize offset,
220 gsize size, gsize align,
224 * GstMemoryMapFunction:
226 * @maxsize: size to map
227 * @flags: access mode for the memory
229 * Get the memory of @mem that can be accessed according to the mode specified
230 * in @flags. The function should return a pointer that contains at least
233 * Returns: a pointer to memory of which at least @maxsize bytes can be
234 * accessed according to the access pattern in @flags.
236 typedef gpointer (*GstMemoryMapFunction) (GstMemory *mem, gsize maxsize, GstMapFlags flags);
239 * GstMemoryUnmapFunction:
242 * Return the pointer previously retrieved with gst_memory_map().
244 * Returns: %TRUE on success.
246 typedef void (*GstMemoryUnmapFunction) (GstMemory *mem);
249 * GstMemoryFreeFunction:
252 * Free the memory used by @mem. This function is usually called when the
253 * refcount of the @mem has reached 0.
255 typedef void (*GstMemoryFreeFunction) (GstMemory *mem);
258 * GstMemoryCopyFunction:
261 * @size: a size or -1
263 * Copy @size bytes from @mem starting at @offset and return them wrapped in a
264 * new GstMemory object.
265 * If @size is set to -1, all bytes starting at @offset are copied.
267 * Returns: a new #GstMemory object wrapping a copy of the requested region in
270 typedef GstMemory * (*GstMemoryCopyFunction) (GstMemory *mem, gssize offset, gssize size);
273 * GstMemoryShareFunction:
276 * @size: a size or -1
278 * Share @size bytes from @mem starting at @offset and return them wrapped in a
279 * new GstMemory object. If @size is set to -1, all bytes starting at @offset are
280 * shared. This function does not make a copy of the bytes in @mem.
282 * Returns: a new #GstMemory object sharing the requested region in @mem.
284 typedef GstMemory * (*GstMemoryShareFunction) (GstMemory *mem, gssize offset, gssize size);
287 * GstMemoryIsSpanFunction:
288 * @mem1: a #GstMemory
289 * @mem2: a #GstMemory
290 * @offset: a result offset
292 * Check if @mem1 and @mem2 occupy contiguous memory and return the offset of
293 * @mem1 in the parent buffer in @offset.
295 * Returns: %TRUE if @mem1 and @mem2 are in contiguous memory.
297 typedef gboolean (*GstMemoryIsSpanFunction) (GstMemory *mem1, GstMemory *mem2, gsize *offset);
301 * @mem_type: the memory type this allocator provides
302 * @alloc: the implementation of the GstAllocatorAllocFunction
303 * @mem_map: the implementation of the GstMemoryMapFunction
304 * @mem_unmap: the implementation of the GstMemoryUnmapFunction
305 * @mem_free: the implementation of the GstMemoryFreeFunction
306 * @mem_copy: the implementation of the GstMemoryCopyFunction
307 * @mem_share: the implementation of the GstMemoryShareFunction
308 * @mem_is_span: the implementation of the GstMemoryIsSpanFunction
310 * The #GstMemoryInfo is used to register new memory allocators and contain
311 * the implementations for various memory operations.
313 struct _GstMemoryInfo {
314 const gchar *mem_type;
316 GstAllocatorAllocFunction alloc;
318 GstMemoryMapFunction mem_map;
319 GstMemoryUnmapFunction mem_unmap;
320 GstMemoryFreeFunction mem_free;
322 GstMemoryCopyFunction mem_copy;
323 GstMemoryShareFunction mem_share;
324 GstMemoryIsSpanFunction mem_is_span;
327 gpointer _gst_reserved[GST_PADDING];
331 GstAllocator * gst_allocator_new (const GstMemoryInfo * info,
332 gpointer user_data, GDestroyNotify notify);
333 const gchar * gst_allocator_get_memory_type (GstAllocator * allocator);
335 GstAllocator * gst_allocator_ref (GstAllocator * allocator);
336 void gst_allocator_unref (GstAllocator * allocator);
338 void gst_allocator_register (const gchar *name, GstAllocator *allocator);
339 GstAllocator * gst_allocator_find (const gchar *name);
341 void gst_allocator_set_default (GstAllocator * allocator);
343 /* allocating memory blocks */
344 GstMemory * gst_allocator_alloc (GstAllocator * allocator, GstMemoryFlags flags,
345 gsize maxsize, gsize offset, gsize size,
348 GstMemory * gst_memory_new_wrapped (GstMemoryFlags flags, gpointer data, gsize maxsize,
349 gsize offset, gsize size, gpointer user_data,
350 GDestroyNotify notify);
353 GstMemory * gst_memory_ref (GstMemory *mem);
354 void gst_memory_unref (GstMemory *mem);
356 gboolean gst_memory_is_exclusive (GstMemory *mem);
358 /* getting/setting memory properties */
359 gsize gst_memory_get_sizes (GstMemory *mem, gsize *offset, gsize *maxsize);
360 void gst_memory_resize (GstMemory *mem, gssize offset, gsize size);
362 /* retrieving data */
363 GstMemory * gst_memory_make_mapped (GstMemory *mem, GstMapInfo *info, GstMapFlags flags);
364 gboolean gst_memory_map (GstMemory *mem, GstMapInfo *info, GstMapFlags flags);
365 void gst_memory_unmap (GstMemory *mem, GstMapInfo *info);
367 /* copy and subregions */
368 GstMemory * gst_memory_copy (GstMemory *mem, gssize offset, gssize size);
369 GstMemory * gst_memory_share (GstMemory *mem, gssize offset, gssize size);
372 gboolean gst_memory_is_span (GstMemory *mem1, GstMemory *mem2, gsize *offset);
376 #endif /* __GST_MEMORY_H__ */