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_LAST: first flag that can be used for custom purposes
54 * Flags for wrapped memory.
57 GST_MEMORY_FLAG_READONLY = (1 << 0),
58 GST_MEMORY_FLAG_NO_SHARE = (1 << 1),
60 GST_MEMORY_FLAG_LAST = (1 << 16)
67 * A flags word containing #GstMemoryFlags flags set on @mem
69 #define GST_MEMORY_FLAGS(mem) (GST_MEMORY_CAST (mem)->flags)
71 * GST_MEMORY_FLAG_IS_SET:
73 * @flag: the #GstMemoryFlags to check.
75 * Gives the status of a specific flag on a @mem.
77 #define GST_MEMORY_FLAG_IS_SET(mem,flag) !!(GST_MEMORY_FLAGS (mem) & (flag))
80 * GST_MEMORY_IS_READONLY:
83 * Check if @mem is readonly.
85 #define GST_MEMORY_IS_READONLY(mem) GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_READONLY)
89 * @allocator: pointer to the #GstAllocator
90 * @flags: memory flags
92 * @parent: parent memory block
93 * @state: private state
94 * @maxsize: the maximum size allocated
95 * @align: the alignment of the memory
96 * @offset: the offset where valid data starts
97 * @size: the size of valid data
99 * Base structure for memory implementations. Custom memory will put this structure
100 * as the first member of their structure.
103 GstAllocator *allocator;
105 GstMemoryFlags flags;
117 * @GST_MAP_READ: map for read access
118 * @GST_MAP_WRITE: map for write access
119 * @GST_MAP_FLAG_LAST: first flag that can be used for custom purposes
121 * Flags used when mapping memory
124 GST_MAP_READ = (1 << 0),
125 GST_MAP_WRITE = (1 << 1),
127 GST_MAP_FLAG_LAST = (1 << 16)
132 * @memory: a pointer to the mapped memory
133 * @flags: flags used when mapping the memory
134 * @data: a pointer to the mapped data
135 * @size: the valid size in @data
136 * @maxsize: the maximum bytes in @data
137 * @user_data: extra private user_data that the implementation of the memory
138 * can use to store extra info.
140 * A structure containing the result of a map operation such as
141 * gst_memory_map(). It contains the data and size.
150 gpointer user_data[4];
153 #define GST_MAP_INFO_INIT { NULL, 0, NULL, 0, 0, }
158 * Map for readwrite access
160 #define GST_MAP_READWRITE (GST_MAP_READ | GST_MAP_WRITE)
163 * GST_ALLOCATOR_SYSMEM:
165 * The allocator name for the default system memory allocator
167 #define GST_ALLOCATOR_SYSMEM "SystemMemory"
170 * GstAllocatorAllocFunction:
171 * @allocator: a #GstAllocator
172 * @maxsize: the maxsize
173 * @align: the alignment
174 * @user_data: user data
176 * Allocate a new #GstMemory from @allocator that can hold at least @maxsize bytes
177 * and is aligned to (@align + 1) bytes.
179 * @user_data is the data that was used when creating @allocator.
181 * Returns: a newly allocated #GstMemory. Free with gst_memory_unref()
183 typedef GstMemory * (*GstAllocatorAllocFunction) (GstAllocator *allocator,
184 gsize maxsize, gsize align,
188 * GstMemoryMapFunction:
190 * @maxsize: size to map
191 * @flags: access mode for the memory
193 * Get the memory of @mem that can be accessed according to the mode specified
194 * in @flags. The function should return a pointer that contains at least
197 * Returns: a pointer to memory of which at least @maxsize bytes can be
198 * accessed according to the access pattern in @flags.
200 typedef gpointer (*GstMemoryMapFunction) (GstMemory *mem, gsize maxsize, GstMapFlags flags);
203 * GstMemoryUnmapFunction:
206 * Return the pointer previously retrieved with gst_memory_map().
208 * Returns: %TRUE on success.
210 typedef void (*GstMemoryUnmapFunction) (GstMemory *mem);
213 * GstMemoryFreeFunction:
216 * Free the memory used by @mem. This function is usually called when the
217 * refcount of the @mem has reached 0.
219 typedef void (*GstMemoryFreeFunction) (GstMemory *mem);
222 * GstMemoryCopyFunction:
225 * @size: a size or -1
227 * Copy @size bytes from @mem starting at @offset and return them wrapped in a
228 * new GstMemory object.
229 * If @size is set to -1, all bytes starting at @offset are copied.
231 * Returns: a new #GstMemory object wrapping a copy of the requested region in
234 typedef GstMemory * (*GstMemoryCopyFunction) (GstMemory *mem, gssize offset, gssize size);
237 * GstMemoryShareFunction:
240 * @size: a size or -1
242 * Share @size bytes from @mem starting at @offset and return them wrapped in a
243 * new GstMemory object. If @size is set to -1, all bytes starting at @offset are
244 * shared. This function does not make a copy of the bytes in @mem.
246 * Returns: a new #GstMemory object sharing the requested region in @mem.
248 typedef GstMemory * (*GstMemoryShareFunction) (GstMemory *mem, gssize offset, gssize size);
251 * GstMemoryIsSpanFunction:
252 * @mem1: a #GstMemory
253 * @mem2: a #GstMemory
254 * @offset: a result offset
256 * Check if @mem1 and @mem2 occupy contiguous memory and return the offset of
257 * @mem1 in the parent buffer in @offset.
259 * Returns: %TRUE if @mem1 and @mem2 are in contiguous memory.
261 typedef gboolean (*GstMemoryIsSpanFunction) (GstMemory *mem1, GstMemory *mem2, gsize *offset);
265 * @mem_type: the memory type this allocator provides
266 * @alloc: the implementation of the GstAllocatorAllocFunction
267 * @mem_map: the implementation of the GstMemoryMapFunction
268 * @mem_unmap: the implementation of the GstMemoryUnmapFunction
269 * @mem_free: the implementation of the GstMemoryFreeFunction
270 * @mem_copy: the implementation of the GstMemoryCopyFunction
271 * @mem_share: the implementation of the GstMemoryShareFunction
272 * @mem_is_span: the implementation of the GstMemoryIsSpanFunction
274 * The #GstMemoryInfo is used to register new memory allocators and contain
275 * the implementations for various memory operations.
277 struct _GstMemoryInfo {
278 const gchar *mem_type;
280 GstAllocatorAllocFunction alloc;
282 GstMemoryMapFunction mem_map;
283 GstMemoryUnmapFunction mem_unmap;
284 GstMemoryFreeFunction mem_free;
286 GstMemoryCopyFunction mem_copy;
287 GstMemoryShareFunction mem_share;
288 GstMemoryIsSpanFunction mem_is_span;
291 gpointer _gst_reserved[GST_PADDING];
295 GstAllocator * gst_allocator_new (const GstMemoryInfo * info,
296 gpointer user_data, GDestroyNotify notify);
297 const gchar * gst_allocator_get_memory_type (GstAllocator * allocator);
299 GstAllocator * gst_allocator_ref (GstAllocator * allocator);
300 void gst_allocator_unref (GstAllocator * allocator);
302 void gst_allocator_register (const gchar *name, GstAllocator *allocator);
303 GstAllocator * gst_allocator_find (const gchar *name);
305 void gst_allocator_set_default (GstAllocator * allocator);
307 /* allocating memory blocks */
308 GstMemory * gst_allocator_alloc (GstAllocator * allocator,
309 gsize maxsize, gsize align);
311 GstMemory * gst_memory_new_wrapped (GstMemoryFlags flags, gpointer data, gsize maxsize,
312 gsize offset, gsize size, gpointer user_data,
313 GDestroyNotify notify);
316 GstMemory * gst_memory_ref (GstMemory *mem);
317 void gst_memory_unref (GstMemory *mem);
319 gboolean gst_memory_is_exclusive (GstMemory *mem);
321 /* getting/setting memory properties */
322 gsize gst_memory_get_sizes (GstMemory *mem, gsize *offset, gsize *maxsize);
323 void gst_memory_resize (GstMemory *mem, gssize offset, gsize size);
325 /* retrieving data */
326 GstMemory * gst_memory_make_mapped (GstMemory *mem, GstMapInfo *info, GstMapFlags flags);
327 gboolean gst_memory_map (GstMemory *mem, GstMapInfo *info, GstMapFlags flags);
328 void gst_memory_unmap (GstMemory *mem, GstMapInfo *info);
330 /* copy and subregions */
331 GstMemory * gst_memory_copy (GstMemory *mem, gssize offset, gssize size);
332 GstMemory * gst_memory_share (GstMemory *mem, gssize offset, gssize size);
335 gboolean gst_memory_is_span (GstMemory *mem1, GstMemory *mem2, gsize *offset);
339 #endif /* __GST_MEMORY_H__ */