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_ALLOCATION_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;
47 * gst_memory_alignment:
49 * The default memory alignment in bytes - 1
50 * an alignment of 7 would be the same as what malloc() guarantees.
52 GST_EXPORT gsize gst_memory_alignment;
54 #define GST_MEMORY_CAST(mem) ((GstMemory *)(mem))
58 * @GST_MEMORY_FLAG_READONLY: memory is readonly. It is not allowed to map the
59 * memory with #GST_MAP_WRITE.
60 * @GST_MEMORY_FLAG_NO_SHARE: memory must not be shared. Copies will have to be
61 * made when this memory needs to be shared between buffers.
62 * @GST_MEMORY_FLAG_ZERO_PREFIXED: the memory prefix is filled with 0 bytes
63 * @GST_MEMORY_FLAG_ZERO_PADDED: the memory padding is filled with 0 bytes
64 * @GST_MEMORY_FLAG_LAST: first flag that can be used for custom purposes
66 * Flags for wrapped memory.
69 GST_MEMORY_FLAG_READONLY = (GST_MINI_OBJECT_FLAG_LAST << 0),
70 GST_MEMORY_FLAG_NO_SHARE = (GST_MINI_OBJECT_FLAG_LAST << 1),
71 GST_MEMORY_FLAG_ZERO_PREFIXED = (GST_MINI_OBJECT_FLAG_LAST << 2),
72 GST_MEMORY_FLAG_ZERO_PADDED = (GST_MINI_OBJECT_FLAG_LAST << 3),
74 GST_MEMORY_FLAG_LAST = (GST_MINI_OBJECT_FLAG_LAST << 16)
81 * A flags word containing #GstMemoryFlags flags set on @mem
83 #define GST_MEMORY_FLAGS(mem) GST_MINI_OBJECT_FLAGS (mem)
85 * GST_MEMORY_FLAG_IS_SET:
87 * @flag: the #GstMemoryFlags to check.
89 * Gives the status of a specific flag on a @mem.
91 #define GST_MEMORY_FLAG_IS_SET(mem,flag) GST_MINI_OBJECT_FLAG_IS_SET (mem,flag)
93 * GST_MEMORY_FLAG_UNSET:
95 * @flag: the #GstMemoryFlags to clear.
97 * Clear a specific flag on a @mem.
99 #define GST_MEMORY_FLAG_UNSET(mem,flag) GST_MINI_OBJECT_FLAG_UNSET (mem, flag)
102 * GST_MEMORY_IS_READONLY:
103 * @mem: a #GstMemory.
105 * Check if @mem is readonly.
107 #define GST_MEMORY_IS_READONLY(mem) GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_READONLY)
109 * GST_MEMORY_IS_NO_SHARE:
110 * @mem: a #GstMemory.
112 * Check if @mem cannot be shared between buffers
114 #define GST_MEMORY_IS_NO_SHARE(mem) GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_NO_SHARE)
116 * GST_MEMORY_IS_ZERO_PREFIXED:
117 * @mem: a #GstMemory.
119 * Check if the prefix in @mem is 0 filled.
121 #define GST_MEMORY_IS_ZERO_PREFIXED(mem) GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_ZERO_PREFIXED)
123 * GST_MEMORY_IS_ZERO_PADDED:
124 * @mem: a #GstMemory.
126 * Check if the padding in @mem is 0 filled.
128 #define GST_MEMORY_IS_ZERO_PADDED(mem) GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_ZERO_PADDED)
133 * @mini_object: parent structure
134 * @allocator: pointer to the #GstAllocator
135 * @parent: parent memory block
136 * @state: private state
137 * @maxsize: the maximum size allocated
138 * @align: the alignment of the memory
139 * @offset: the offset where valid data starts
140 * @size: the size of valid data
142 * Base structure for memory implementations. Custom memory will put this structure
143 * as the first member of their structure.
146 GstMiniObject mini_object;
148 GstAllocator *allocator;
160 * @GST_LOCK_FLAG_READ: lock for read access
161 * @GST_LOCK_FLAG_WRITE: lock for write access
162 * @GST_LOCK_FLAG_EXCLUSIVE: lock for exclusive access
163 * @GST_LOCK_FLAG_LAST: first flag that can be used for custom purposes
165 * Flags used when locking memory
168 GST_LOCK_FLAG_READ = (1 << 0),
169 GST_LOCK_FLAG_WRITE = (1 << 1),
170 GST_LOCK_FLAG_EXCLUSIVE = (1 << 2),
172 GST_LOCK_FLAG_LAST = (1 << 4)
176 * GST_LOCK_FLAG_READWRITE:
178 * GstLockFlags value alias for GST_LOCK_FLAG_READ | GST_LOCK_FLAG_WRITE
180 #define GST_LOCK_FLAG_READWRITE (GST_LOCK_FLAG_READ | GST_LOCK_FLAG_WRITE)
184 * @GST_MAP_READ: map for read access
185 * @GST_MAP_WRITE: map for write access
186 * @GST_MAP_FLAG_LAST: first flag that can be used for custom purposes
188 * Flags used when mapping memory
191 GST_MAP_READ = GST_LOCK_FLAG_READ,
192 GST_MAP_WRITE = GST_LOCK_FLAG_WRITE,
194 GST_MAP_FLAG_LAST = (1 << 16)
199 * GstMapFlags value alias for GST_MAP_READ | GST_MAP_WRITE
201 #define GST_MAP_READWRITE (GST_MAP_READ | GST_MAP_WRITE)
206 * @memory: a pointer to the mapped memory
207 * @flags: flags used when mapping the memory
208 * @data: (array length=size): a pointer to the mapped data
209 * @size: the valid size in @data
210 * @maxsize: the maximum bytes in @data
211 * @user_data: extra private user_data that the implementation of the memory
212 * can use to store extra info.
214 * A structure containing the result of a map operation such as
215 * gst_memory_map(). It contains the data and size.
224 gpointer user_data[4];
230 * Initializer for #GstMapInfo
232 #define GST_MAP_INFO_INIT { NULL, 0, NULL, 0, 0, }
235 * GST_ALLOCATOR_SYSMEM:
237 * The allocator name for the default system memory allocator
239 #define GST_ALLOCATOR_SYSMEM "SystemMemory"
242 * GstAllocationParams:
243 * @flags: flags to control allocation
244 * @align: the desired alignment of the memory
245 * @prefix: the desired prefix
246 * @padding: the desired padding
248 * Parameters to control the allocation of memory
250 struct _GstAllocationParams {
251 GstMemoryFlags flags;
257 gpointer _gst_reserved[GST_PADDING];
261 * GstAllocatorAllocFunction:
262 * @allocator: a #GstAllocator
264 * @params: allocator params
265 * @user_data: user data
267 * Allocate a new #GstMemory from @allocator that can hold at least @size
268 * bytes (+ padding) and is aligned to (@align + 1) bytes.
270 * The offset and size of the memory should be set and the prefix/padding must
271 * be filled with 0 if @params flags contains #GST_MEMORY_FLAG_ZERO_PREFIXED and
272 * #GST_MEMORY_FLAG_ZERO_PADDED respectively.
274 * @user_data is extra data passed to this function. The default
275 * gst_allocator_alloc() passes the user_data that was used when creating
278 * Returns: a newly allocated #GstMemory. Free with gst_memory_unref()
280 typedef GstMemory * (*GstAllocatorAllocFunction) (GstAllocator *allocator,
281 gsize size, GstAllocationParams *params,
285 * GstMemoryMapFunction:
287 * @maxsize: size to map
288 * @flags: access mode for the memory
290 * Get the memory of @mem that can be accessed according to the mode specified
291 * in @flags. The function should return a pointer that contains at least
294 * Returns: a pointer to memory of which at least @maxsize bytes can be
295 * accessed according to the access pattern in @flags.
297 typedef gpointer (*GstMemoryMapFunction) (GstMemory *mem, gsize maxsize, GstMapFlags flags);
300 * GstMemoryUnmapFunction:
303 * Return the pointer previously retrieved with gst_memory_map().
305 * Returns: %TRUE on success.
307 typedef void (*GstMemoryUnmapFunction) (GstMemory *mem);
310 * GstMemoryFreeFunction:
313 * Free the memory used by @mem. This function is usually called when the
314 * refcount of the @mem has reached 0.
316 typedef void (*GstMemoryFreeFunction) (GstMemory *mem);
319 * GstMemoryCopyFunction:
322 * @size: a size or -1
324 * Copy @size bytes from @mem starting at @offset and return them wrapped in a
325 * new GstMemory object.
326 * If @size is set to -1, all bytes starting at @offset are copied.
328 * Returns: a new #GstMemory object wrapping a copy of the requested region in
331 typedef GstMemory * (*GstMemoryCopyFunction) (GstMemory *mem, gssize offset, gssize size);
334 * GstMemoryShareFunction:
337 * @size: a size or -1
339 * Share @size bytes from @mem starting at @offset and return them wrapped in a
340 * new GstMemory object. If @size is set to -1, all bytes starting at @offset are
341 * shared. This function does not make a copy of the bytes in @mem.
343 * Returns: a new #GstMemory object sharing the requested region in @mem.
345 typedef GstMemory * (*GstMemoryShareFunction) (GstMemory *mem, gssize offset, gssize size);
348 * GstMemoryIsSpanFunction:
349 * @mem1: a #GstMemory
350 * @mem2: a #GstMemory
351 * @offset: a result offset
353 * Check if @mem1 and @mem2 occupy contiguous memory and return the offset of
354 * @mem1 in the parent buffer in @offset.
356 * Returns: %TRUE if @mem1 and @mem2 are in contiguous memory.
358 typedef gboolean (*GstMemoryIsSpanFunction) (GstMemory *mem1, GstMemory *mem2, gsize *offset);
362 * @mem_type: the memory type this allocator provides
363 * @alloc: the implementation of the GstAllocatorAllocFunction
364 * @mem_map: the implementation of the GstMemoryMapFunction
365 * @mem_unmap: the implementation of the GstMemoryUnmapFunction
366 * @mem_free: the implementation of the GstMemoryFreeFunction
367 * @mem_copy: the implementation of the GstMemoryCopyFunction
368 * @mem_share: the implementation of the GstMemoryShareFunction
369 * @mem_is_span: the implementation of the GstMemoryIsSpanFunction
371 * The #GstMemoryInfo is used to register new memory allocators and contain
372 * the implementations for various memory operations.
374 struct _GstMemoryInfo {
375 const gchar *mem_type;
377 GstAllocatorAllocFunction alloc;
379 GstMemoryMapFunction mem_map;
380 GstMemoryUnmapFunction mem_unmap;
381 GstMemoryFreeFunction mem_free;
383 GstMemoryCopyFunction mem_copy;
384 GstMemoryShareFunction mem_share;
385 GstMemoryIsSpanFunction mem_is_span;
388 gpointer _gst_reserved[GST_PADDING];
394 * An opaque type returned from gst_allocator_new() or gst_allocator_find()
395 * that can be used to allocator memory.
399 GstAllocator * gst_allocator_new (const GstMemoryInfo * info,
400 gpointer user_data, GDestroyNotify notify);
401 const gchar * gst_allocator_get_memory_type (GstAllocator * allocator);
405 * @allocator: The allocator to refcount
407 * Increase the refcount of this allocator.
409 * Returns: (transfer full): @allocator (for convenience when doing assignments)
411 #ifdef _FOOL_GTK_DOC_
412 G_INLINE_FUNC GstAllocator * gst_allocator_ref (GstAllocator * allocator);
415 static inline GstAllocator *
416 gst_allocator_ref (GstAllocator * allocator)
418 return (GstAllocator *) gst_mini_object_ref (GST_MINI_OBJECT_CAST (allocator));
422 * gst_allocator_unref:
423 * @allocator: (transfer full): the allocator to refcount
425 * Decrease the refcount of an allocator, freeing it if the refcount reaches 0.
427 #ifdef _FOOL_GTK_DOC_
428 G_INLINE_FUNC void gst_allocator_unref (GstAllocator * allocator);
432 gst_allocator_unref (GstAllocator * allocator)
434 gst_mini_object_unref (GST_MINI_OBJECT_CAST (allocator));
437 void gst_allocator_register (const gchar *name, GstAllocator *allocator);
438 GstAllocator * gst_allocator_find (const gchar *name);
440 void gst_allocator_set_default (GstAllocator * allocator);
442 /* allocating memory blocks */
443 void gst_allocation_params_init (GstAllocationParams *params);
444 GstAllocationParams *
445 gst_allocation_params_copy (const GstAllocationParams *params) G_GNUC_MALLOC;
446 void gst_allocation_params_free (GstAllocationParams *params);
448 GstMemory * gst_allocator_alloc (GstAllocator * allocator, gsize size,
449 GstAllocationParams *params);
451 GstMemory * gst_memory_new_wrapped (GstMemoryFlags flags, gpointer data, gsize maxsize,
452 gsize offset, gsize size, gpointer user_data,
453 GDestroyNotify notify);
458 * @memory: The memory to refcount
460 * Increase the refcount of this memory.
462 * Returns: (transfer full): @memory (for convenience when doing assignments)
464 #ifdef _FOOL_GTK_DOC_
465 G_INLINE_FUNC GstMemory * gst_memory_ref (GstMemory * memory);
468 static inline GstMemory *
469 gst_memory_ref (GstMemory * memory)
471 return (GstMemory *) gst_mini_object_ref (GST_MINI_OBJECT_CAST (memory));
476 * @memory: (transfer full): the memory to refcount
478 * Decrease the refcount of an memory, freeing it if the refcount reaches 0.
480 #ifdef _FOOL_GTK_DOC_
481 G_INLINE_FUNC void gst_memory_unref (GstMemory * memory);
485 gst_memory_unref (GstMemory * memory)
487 gst_mini_object_unref (GST_MINI_OBJECT_CAST (memory));
492 gboolean gst_memory_is_exclusive (GstMemory *mem);
494 gboolean gst_memory_lock (GstMemory *mem, GstLockFlags flags);
495 void gst_memory_unlock (GstMemory *mem, GstLockFlags flags);
497 /* getting/setting memory properties */
498 gsize gst_memory_get_sizes (GstMemory *mem, gsize *offset, gsize *maxsize);
499 void gst_memory_resize (GstMemory *mem, gssize offset, gsize size);
501 /* retrieving data */
502 GstMemory * gst_memory_make_mapped (GstMemory *mem, GstMapInfo *info, GstMapFlags flags);
503 gboolean gst_memory_map (GstMemory *mem, GstMapInfo *info, GstMapFlags flags);
504 void gst_memory_unmap (GstMemory *mem, GstMapInfo *info);
506 /* copy and subregions */
507 GstMemory * gst_memory_copy (GstMemory *mem, gssize offset, gssize size);
508 GstMemory * gst_memory_share (GstMemory *mem, gssize offset, gssize size);
511 gboolean gst_memory_is_span (GstMemory *mem1, GstMemory *mem2, gsize *offset);
515 #endif /* __GST_MEMORY_H__ */