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 #define GST_TYPE_ALLOCATOR_PARAMS (gst_allocation_params_get_type())
39 GType gst_allocation_params_get_type(void);
41 typedef struct _GstMemory GstMemory;
42 typedef struct _GstMemoryInfo GstMemoryInfo;
43 typedef struct _GstAllocator GstAllocator;
44 typedef struct _GstAllocationParams GstAllocationParams;
46 GST_EXPORT gsize gst_memory_alignment;
48 #define GST_MEMORY_CAST(mem) ((GstMemory *)(mem))
52 * @GST_MEMORY_FLAG_READONLY: memory is readonly. It is not allowed to map the
53 * memory with #GST_MAP_WRITE.
54 * @GST_MEMORY_FLAG_NO_SHARE: memory must not be shared. Copies will have to be
55 * made when this memory needs to be shared between buffers.
56 * @GST_MEMORY_FLAG_ZERO_PREFIXED: the memory prefix is filled with 0 bytes
57 * @GST_MEMORY_FLAG_ZERO_PADDED: the memory padding is filled with 0 bytes
58 * @GST_MEMORY_FLAG_LAST: first flag that can be used for custom purposes
60 * Flags for wrapped memory.
63 GST_MEMORY_FLAG_READONLY = (1 << 0),
64 GST_MEMORY_FLAG_NO_SHARE = (1 << 1),
65 GST_MEMORY_FLAG_ZERO_PREFIXED = (1 << 2),
66 GST_MEMORY_FLAG_ZERO_PADDED = (1 << 3),
68 GST_MEMORY_FLAG_LAST = (1 << 16)
75 * A flags word containing #GstMemoryFlags flags set on @mem
77 #define GST_MEMORY_FLAGS(mem) (GST_MEMORY_CAST (mem)->flags)
79 * GST_MEMORY_FLAG_IS_SET:
81 * @flag: the #GstMemoryFlags to check.
83 * Gives the status of a specific flag on a @mem.
85 #define GST_MEMORY_FLAG_IS_SET(mem,flag) !!(GST_MEMORY_FLAGS (mem) & (flag))
87 * GST_MEMORY_FLAG_UNSET:
89 * @flag: the #GstMemoryFlags to clear.
91 * Clear a specific flag on a @mem.
93 #define GST_MEMORY_FLAG_UNSET(mem,flag) (GST_MEMORY_FLAGS (mem) &= ~(flag))
96 * GST_MEMORY_IS_READONLY:
99 * Check if @mem is readonly.
101 #define GST_MEMORY_IS_READONLY(mem) GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_READONLY)
103 * GST_MEMORY_IS_ZERO_PREFIXED:
104 * @mem: a #GstMemory.
106 * Check if the prefix in @mem is 0 filled.
108 #define GST_MEMORY_IS_ZERO_PREFIXED(mem) GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_ZERO_PREFIXED)
110 * GST_MEMORY_IS_ZERO_PADDED:
111 * @mem: a #GstMemory.
113 * Check if the padding in @mem is 0 filled.
115 #define GST_MEMORY_IS_ZERO_PADDED(mem) GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_ZERO_PADDED)
120 * @allocator: pointer to the #GstAllocator
121 * @flags: memory flags
122 * @refcount: refcount
123 * @parent: parent memory block
124 * @state: private state
125 * @maxsize: the maximum size allocated
126 * @align: the alignment of the memory
127 * @offset: the offset where valid data starts
128 * @size: the size of valid data
130 * Base structure for memory implementations. Custom memory will put this structure
131 * as the first member of their structure.
134 GstAllocator *allocator;
136 GstMemoryFlags flags;
148 * @GST_MAP_READ: map for read access
149 * @GST_MAP_WRITE: map for write access
150 * @GST_MAP_FLAG_LAST: first flag that can be used for custom purposes
152 * Flags used when mapping memory
155 GST_MAP_READ = (1 << 0),
156 GST_MAP_WRITE = (1 << 1),
158 GST_MAP_FLAG_LAST = (1 << 16)
163 * @memory: a pointer to the mapped memory
164 * @flags: flags used when mapping the memory
165 * @data: a pointer to the mapped data
166 * @size: the valid size in @data
167 * @maxsize: the maximum bytes in @data
168 * @user_data: extra private user_data that the implementation of the memory
169 * can use to store extra info.
171 * A structure containing the result of a map operation such as
172 * gst_memory_map(). It contains the data and size.
181 gpointer user_data[4];
184 #define GST_MAP_INFO_INIT { NULL, 0, NULL, 0, 0, }
189 * Map for readwrite access
191 #define GST_MAP_READWRITE (GST_MAP_READ | GST_MAP_WRITE)
194 * GST_ALLOCATOR_SYSMEM:
196 * The allocator name for the default system memory allocator
198 #define GST_ALLOCATOR_SYSMEM "SystemMemory"
201 * GstAllocationParams:
202 * @flags: flags to control allocation
203 * @align: the desired alignment of the memory
204 * @prefix: the disired prefix
205 * @padding: the desired padding
207 * Parameters to control the allocation of memory
209 struct _GstAllocationParams {
210 GstMemoryFlags flags;
216 gpointer _gst_reserved[GST_PADDING];
220 * GstAllocatorAllocFunction:
221 * @allocator: a #GstAllocator
223 * @params: allocator params
224 * @user_data: user data
226 * Allocate a new #GstMemory from @allocator that can hold at least @size
227 * bytes (+ padding) and is aligned to (@align + 1) bytes.
229 * The offset and size of the memory should be set and the prefix/padding must
230 * be filled with 0 if @params flags contains #GST_MEMORY_FLAG_ZERO_PREFIXED and
231 * #GST_MEMORY_FLAG_ZERO_PADDED respectively.
233 * @user_data is the data that was used when creating @allocator.
235 * Returns: a newly allocated #GstMemory. Free with gst_memory_unref()
237 typedef GstMemory * (*GstAllocatorAllocFunction) (GstAllocator *allocator,
238 gsize size, GstAllocationParams *params,
242 * GstMemoryMapFunction:
244 * @maxsize: size to map
245 * @flags: access mode for the memory
247 * Get the memory of @mem that can be accessed according to the mode specified
248 * in @flags. The function should return a pointer that contains at least
251 * Returns: a pointer to memory of which at least @maxsize bytes can be
252 * accessed according to the access pattern in @flags.
254 typedef gpointer (*GstMemoryMapFunction) (GstMemory *mem, gsize maxsize, GstMapFlags flags);
257 * GstMemoryUnmapFunction:
260 * Return the pointer previously retrieved with gst_memory_map().
262 * Returns: %TRUE on success.
264 typedef void (*GstMemoryUnmapFunction) (GstMemory *mem);
267 * GstMemoryFreeFunction:
270 * Free the memory used by @mem. This function is usually called when the
271 * refcount of the @mem has reached 0.
273 typedef void (*GstMemoryFreeFunction) (GstMemory *mem);
276 * GstMemoryCopyFunction:
279 * @size: a size or -1
281 * Copy @size bytes from @mem starting at @offset and return them wrapped in a
282 * new GstMemory object.
283 * If @size is set to -1, all bytes starting at @offset are copied.
285 * Returns: a new #GstMemory object wrapping a copy of the requested region in
288 typedef GstMemory * (*GstMemoryCopyFunction) (GstMemory *mem, gssize offset, gssize size);
291 * GstMemoryShareFunction:
294 * @size: a size or -1
296 * Share @size bytes from @mem starting at @offset and return them wrapped in a
297 * new GstMemory object. If @size is set to -1, all bytes starting at @offset are
298 * shared. This function does not make a copy of the bytes in @mem.
300 * Returns: a new #GstMemory object sharing the requested region in @mem.
302 typedef GstMemory * (*GstMemoryShareFunction) (GstMemory *mem, gssize offset, gssize size);
305 * GstMemoryIsSpanFunction:
306 * @mem1: a #GstMemory
307 * @mem2: a #GstMemory
308 * @offset: a result offset
310 * Check if @mem1 and @mem2 occupy contiguous memory and return the offset of
311 * @mem1 in the parent buffer in @offset.
313 * Returns: %TRUE if @mem1 and @mem2 are in contiguous memory.
315 typedef gboolean (*GstMemoryIsSpanFunction) (GstMemory *mem1, GstMemory *mem2, gsize *offset);
319 * @mem_type: the memory type this allocator provides
320 * @alloc: the implementation of the GstAllocatorAllocFunction
321 * @mem_map: the implementation of the GstMemoryMapFunction
322 * @mem_unmap: the implementation of the GstMemoryUnmapFunction
323 * @mem_free: the implementation of the GstMemoryFreeFunction
324 * @mem_copy: the implementation of the GstMemoryCopyFunction
325 * @mem_share: the implementation of the GstMemoryShareFunction
326 * @mem_is_span: the implementation of the GstMemoryIsSpanFunction
328 * The #GstMemoryInfo is used to register new memory allocators and contain
329 * the implementations for various memory operations.
331 struct _GstMemoryInfo {
332 const gchar *mem_type;
334 GstAllocatorAllocFunction alloc;
336 GstMemoryMapFunction mem_map;
337 GstMemoryUnmapFunction mem_unmap;
338 GstMemoryFreeFunction mem_free;
340 GstMemoryCopyFunction mem_copy;
341 GstMemoryShareFunction mem_share;
342 GstMemoryIsSpanFunction mem_is_span;
345 gpointer _gst_reserved[GST_PADDING];
349 GstAllocator * gst_allocator_new (const GstMemoryInfo * info,
350 gpointer user_data, GDestroyNotify notify);
351 const gchar * gst_allocator_get_memory_type (GstAllocator * allocator);
353 GstAllocator * gst_allocator_ref (GstAllocator * allocator);
354 void gst_allocator_unref (GstAllocator * allocator);
356 void gst_allocator_register (const gchar *name, GstAllocator *allocator);
357 GstAllocator * gst_allocator_find (const gchar *name);
359 void gst_allocator_set_default (GstAllocator * allocator);
361 /* allocating memory blocks */
362 void gst_allocation_params_init (GstAllocationParams *params);
363 GstAllocationParams *
364 gst_allocation_params_copy (const GstAllocationParams *params) G_GNUC_MALLOC;
365 void gst_allocation_params_free (GstAllocationParams *params);
367 GstMemory * gst_allocator_alloc (GstAllocator * allocator, gsize size,
368 GstAllocationParams *params);
370 GstMemory * gst_memory_new_wrapped (GstMemoryFlags flags, gpointer data, gsize maxsize,
371 gsize offset, gsize size, gpointer user_data,
372 GDestroyNotify notify);
375 GstMemory * gst_memory_ref (GstMemory *mem);
376 void gst_memory_unref (GstMemory *mem);
378 gboolean gst_memory_is_exclusive (GstMemory *mem);
380 /* getting/setting memory properties */
381 gsize gst_memory_get_sizes (GstMemory *mem, gsize *offset, gsize *maxsize);
382 void gst_memory_resize (GstMemory *mem, gssize offset, gsize size);
384 /* retrieving data */
385 GstMemory * gst_memory_make_mapped (GstMemory *mem, GstMapInfo *info, GstMapFlags flags);
386 gboolean gst_memory_map (GstMemory *mem, GstMapInfo *info, GstMapFlags flags);
387 void gst_memory_unmap (GstMemory *mem, GstMapInfo *info);
389 /* copy and subregions */
390 GstMemory * gst_memory_copy (GstMemory *mem, gssize offset, gssize size);
391 GstMemory * gst_memory_share (GstMemory *mem, gssize offset, gssize size);
394 gboolean gst_memory_is_span (GstMemory *mem1, GstMemory *mem2, gsize *offset);
398 #endif /* __GST_MEMORY_H__ */