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;
46 * @GST_MEMORY_FLAG_READONLY: memory is readonly. It is not allowed to map the
47 * memory with #GST_MAP_WRITE.
48 * @GST_MEMORY_FLAG_NO_SHARE: memory must not be shared. Copies will have to be
49 * made when this memory needs to be shared between buffers.
50 * @GST_MEMORY_FLAG_LAST: first flag that can be used for custom purposes
52 * Flags for wrapped memory.
55 GST_MEMORY_FLAG_READONLY = (1 << 0),
56 GST_MEMORY_FLAG_NO_SHARE = (1 << 1),
58 GST_MEMORY_FLAG_LAST = (1 << 16)
63 * @allocator: pointer to the #GstAllocator
64 * @flags: memory flags
66 * @parent: parent memory block
67 * @state: private state
68 * @maxsize: the maximum size allocated
69 * @align: the alignment of the memory
70 * @offset: the offset where valid data starts
71 * @size: the size of valid data
73 * Base structure for memory implementations. Custom memory will put this structure
74 * as the first member of their structure.
77 GstAllocator *allocator;
91 * @GST_MAP_READ: map for read access
92 * @GST_MAP_WRITE: map for write access
93 * @GST_MAP_FLAG_LAST: first flag that can be used for custom purposes
95 * Flags used when mapping memory
98 GST_MAP_READ = (1 << 0),
99 GST_MAP_WRITE = (1 << 1),
101 GST_MAP_FLAG_LAST = (1 << 16)
106 * @memory: a pointer to the mapped memory
107 * @flags: flags used when mapping the memory
108 * @data: a pointer to the mapped data
109 * @size: the valid size in @data
110 * @maxsize: the maximum bytes in @data
112 * A structure containing the result of a map operation such as
113 * gst_memory_map(). It contains the data and size.
123 #define GST_MAP_INFO_INIT { NULL, 0, NULL, 0, 0 }
128 * Map for readwrite access
130 #define GST_MAP_READWRITE (GST_MAP_READ | GST_MAP_WRITE)
133 * GST_ALLOCATOR_SYSMEM:
135 * The allocator name for the default system memory allocator
137 #define GST_ALLOCATOR_SYSMEM "SystemMemory"
140 * GstAllocatorAllocFunction:
141 * @allocator: a #GstAllocator
142 * @maxsize: the maxsize
143 * @align: the alignment
144 * @user_data: user data
146 * Allocate a new #GstMemory from @allocator that can hold at least @maxsize bytes
147 * and is aligned to (@align + 1) bytes.
149 * @user_data is the data that was used when creating @allocator.
151 * Returns: a newly allocated #GstMemory. Free with gst_memory_unref()
153 typedef GstMemory * (*GstAllocatorAllocFunction) (GstAllocator *allocator,
154 gsize maxsize, gsize align,
158 * GstMemoryMapFunction:
160 * @maxsize: size to map
161 * @flags: access mode for the memory
163 * Get the memory of @mem that can be accessed according to the mode specified
164 * in @flags. The function should return a pointer that contains at least
167 * Returns: a pointer to memory of which at least @maxsize bytes can be
168 * accessed according to the access pattern in @flags.
170 typedef gpointer (*GstMemoryMapFunction) (GstMemory *mem, gsize maxsize, GstMapFlags flags);
173 * GstMemoryUnmapFunction:
176 * Return the pointer previously retrieved with gst_memory_map().
178 * Returns: %TRUE on success.
180 typedef void (*GstMemoryUnmapFunction) (GstMemory *mem);
183 * GstMemoryFreeFunction:
186 * Free the memory used by @mem. This function is usually called when the
187 * refcount of the @mem has reached 0.
189 typedef void (*GstMemoryFreeFunction) (GstMemory *mem);
192 * GstMemoryCopyFunction:
195 * @size: a size or -1
197 * Copy @size bytes from @mem starting at @offset and return them wrapped in a
198 * new GstMemory object.
199 * If @size is set to -1, all bytes starting at @offset are copied.
201 * Returns: a new #GstMemory object wrapping a copy of the requested region in
204 typedef GstMemory * (*GstMemoryCopyFunction) (GstMemory *mem, gssize offset, gssize size);
207 * GstMemoryShareFunction:
210 * @size: a size or -1
212 * Share @size bytes from @mem starting at @offset and return them wrapped in a
213 * new GstMemory object. If @size is set to -1, all bytes starting at @offset are
214 * shared. This function does not make a copy of the bytes in @mem.
216 * Returns: a new #GstMemory object sharing the requested region in @mem.
218 typedef GstMemory * (*GstMemoryShareFunction) (GstMemory *mem, gssize offset, gssize size);
221 * GstMemoryIsSpanFunction:
222 * @mem1: a #GstMemory
223 * @mem2: a #GstMemory
224 * @offset: a result offset
226 * Check if @mem1 and @mem2 occupy contiguous memory and return the offset of
227 * @mem1 in the parent buffer in @offset.
229 * Returns: %TRUE if @mem1 and @mem2 are in contiguous memory.
231 typedef gboolean (*GstMemoryIsSpanFunction) (GstMemory *mem1, GstMemory *mem2, gsize *offset);
235 * @type: the memory type this allocator provides
236 * @alloc: the implementation of the GstAllocatorAllocFunction
237 * @mem_map: the implementation of the GstMemoryMapFunction
238 * @mem_unmap: the implementation of the GstMemoryUnmapFunction
239 * @mem_free: the implementation of the GstMemoryFreeFunction
240 * @mem_copy: the implementation of the GstMemoryCopyFunction
241 * @mem_share: the implementation of the GstMemoryShareFunction
242 * @mem_is_span: the implementation of the GstMemoryIsSpanFunction
244 * The #GstMemoryInfo is used to register new memory allocators and contain
245 * the implementations for various memory operations.
247 struct _GstMemoryInfo {
248 const gchar *mem_type;
250 GstAllocatorAllocFunction alloc;
252 GstMemoryMapFunction mem_map;
253 GstMemoryUnmapFunction mem_unmap;
254 GstMemoryFreeFunction mem_free;
256 GstMemoryCopyFunction mem_copy;
257 GstMemoryShareFunction mem_share;
258 GstMemoryIsSpanFunction mem_is_span;
261 gpointer _gst_reserved[GST_PADDING];
265 GstAllocator * gst_allocator_new (const GstMemoryInfo * info,
266 gpointer user_data, GDestroyNotify notify);
267 const gchar * gst_allocator_get_memory_type (GstAllocator * allocator);
269 GstAllocator * gst_allocator_ref (GstAllocator * allocator);
270 void gst_allocator_unref (GstAllocator * allocator);
272 void gst_allocator_register (const gchar *name, GstAllocator *alloc);
273 GstAllocator * gst_allocator_find (const gchar *name);
275 void gst_allocator_set_default (GstAllocator * allocator);
277 /* allocating memory blocks */
278 GstMemory * gst_allocator_alloc (GstAllocator * allocator,
279 gsize maxsize, gsize align);
281 GstMemory * gst_memory_new_wrapped (GstMemoryFlags flags, gpointer data, GFreeFunc free_func,
282 gsize maxsize, gsize offset, gsize size);
285 GstMemory * gst_memory_ref (GstMemory *mem);
286 void gst_memory_unref (GstMemory *mem);
288 /* getting/setting memory properties */
289 gsize gst_memory_get_sizes (GstMemory *mem, gsize *offset, gsize *maxsize);
290 void gst_memory_resize (GstMemory *mem, gssize offset, gsize size);
292 /* retrieving data */
293 gboolean gst_memory_is_writable (GstMemory *mem);
295 GstMemory * gst_memory_make_mapped (GstMemory *mem, GstMapInfo *info, GstMapFlags flags);
296 gboolean gst_memory_map (GstMemory *mem, GstMapInfo *info, GstMapFlags flags);
297 void gst_memory_unmap (GstMemory *mem, GstMapInfo *info);
299 /* copy and subregions */
300 GstMemory * gst_memory_copy (GstMemory *mem, gssize offset, gssize size);
301 GstMemory * gst_memory_share (GstMemory *mem, gssize offset, gssize size);
304 gboolean gst_memory_is_span (GstMemory *mem1, GstMemory *mem2, gsize *offset);
308 #endif /* __GST_MEMORY_H__ */