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 = (1 << 0),
70 GST_MEMORY_FLAG_NO_SHARE = (1 << 1),
71 GST_MEMORY_FLAG_ZERO_PREFIXED = (1 << 2),
72 GST_MEMORY_FLAG_ZERO_PADDED = (1 << 3),
74 GST_MEMORY_FLAG_LAST = (1 << 16)
81 * A flags word containing #GstMemoryFlags flags set on @mem
83 #define GST_MEMORY_FLAGS(mem) (GST_MEMORY_CAST (mem)->flags)
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_MEMORY_FLAGS (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_MEMORY_FLAGS (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_ZERO_PREFIXED:
110 * @mem: a #GstMemory.
112 * Check if the prefix in @mem is 0 filled.
114 #define GST_MEMORY_IS_ZERO_PREFIXED(mem) GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_ZERO_PREFIXED)
116 * GST_MEMORY_IS_ZERO_PADDED:
117 * @mem: a #GstMemory.
119 * Check if the padding in @mem is 0 filled.
121 #define GST_MEMORY_IS_ZERO_PADDED(mem) GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_ZERO_PADDED)
126 * @allocator: pointer to the #GstAllocator
127 * @flags: memory flags
128 * @refcount: refcount
129 * @parent: parent memory block
130 * @state: private state
131 * @maxsize: the maximum size allocated
132 * @align: the alignment of the memory
133 * @offset: the offset where valid data starts
134 * @size: the size of valid data
136 * Base structure for memory implementations. Custom memory will put this structure
137 * as the first member of their structure.
140 GstAllocator *allocator;
142 GstMemoryFlags flags;
154 * @GST_MAP_READ: map for read access
155 * @GST_MAP_WRITE: map for write access
156 * @GST_MAP_FLAG_LAST: first flag that can be used for custom purposes
158 * Flags used when mapping memory
161 GST_MAP_READ = (1 << 0),
162 GST_MAP_WRITE = (1 << 1),
164 GST_MAP_FLAG_LAST = (1 << 16)
169 * @memory: a pointer to the mapped memory
170 * @flags: flags used when mapping the memory
171 * @data: a pointer to the mapped data
172 * @size: the valid size in @data
173 * @maxsize: the maximum bytes in @data
174 * @user_data: extra private user_data that the implementation of the memory
175 * can use to store extra info.
177 * A structure containing the result of a map operation such as
178 * gst_memory_map(). It contains the data and size.
187 gpointer user_data[4];
193 * Initializer for #GstMapInfo
195 #define GST_MAP_INFO_INIT { NULL, 0, NULL, 0, 0, }
200 * Map for readwrite access
202 #define GST_MAP_READWRITE (GST_MAP_READ | GST_MAP_WRITE)
205 * GST_ALLOCATOR_SYSMEM:
207 * The allocator name for the default system memory allocator
209 #define GST_ALLOCATOR_SYSMEM "SystemMemory"
212 * GstAllocationParams:
213 * @flags: flags to control allocation
214 * @align: the desired alignment of the memory
215 * @prefix: the disired prefix
216 * @padding: the desired padding
218 * Parameters to control the allocation of memory
220 struct _GstAllocationParams {
221 GstMemoryFlags flags;
227 gpointer _gst_reserved[GST_PADDING];
231 * GstAllocatorAllocFunction:
232 * @allocator: a #GstAllocator
234 * @params: allocator params
235 * @user_data: user data
237 * Allocate a new #GstMemory from @allocator that can hold at least @size
238 * bytes (+ padding) and is aligned to (@align + 1) bytes.
240 * The offset and size of the memory should be set and the prefix/padding must
241 * be filled with 0 if @params flags contains #GST_MEMORY_FLAG_ZERO_PREFIXED and
242 * #GST_MEMORY_FLAG_ZERO_PADDED respectively.
244 * @user_data is the data that was used when creating @allocator.
246 * Returns: a newly allocated #GstMemory. Free with gst_memory_unref()
248 typedef GstMemory * (*GstAllocatorAllocFunction) (GstAllocator *allocator,
249 gsize size, GstAllocationParams *params,
253 * GstMemoryMapFunction:
255 * @maxsize: size to map
256 * @flags: access mode for the memory
258 * Get the memory of @mem that can be accessed according to the mode specified
259 * in @flags. The function should return a pointer that contains at least
262 * Returns: a pointer to memory of which at least @maxsize bytes can be
263 * accessed according to the access pattern in @flags.
265 typedef gpointer (*GstMemoryMapFunction) (GstMemory *mem, gsize maxsize, GstMapFlags flags);
268 * GstMemoryUnmapFunction:
271 * Return the pointer previously retrieved with gst_memory_map().
273 * Returns: %TRUE on success.
275 typedef void (*GstMemoryUnmapFunction) (GstMemory *mem);
278 * GstMemoryFreeFunction:
281 * Free the memory used by @mem. This function is usually called when the
282 * refcount of the @mem has reached 0.
284 typedef void (*GstMemoryFreeFunction) (GstMemory *mem);
287 * GstMemoryCopyFunction:
290 * @size: a size or -1
292 * Copy @size bytes from @mem starting at @offset and return them wrapped in a
293 * new GstMemory object.
294 * If @size is set to -1, all bytes starting at @offset are copied.
296 * Returns: a new #GstMemory object wrapping a copy of the requested region in
299 typedef GstMemory * (*GstMemoryCopyFunction) (GstMemory *mem, gssize offset, gssize size);
302 * GstMemoryShareFunction:
305 * @size: a size or -1
307 * Share @size bytes from @mem starting at @offset and return them wrapped in a
308 * new GstMemory object. If @size is set to -1, all bytes starting at @offset are
309 * shared. This function does not make a copy of the bytes in @mem.
311 * Returns: a new #GstMemory object sharing the requested region in @mem.
313 typedef GstMemory * (*GstMemoryShareFunction) (GstMemory *mem, gssize offset, gssize size);
316 * GstMemoryIsSpanFunction:
317 * @mem1: a #GstMemory
318 * @mem2: a #GstMemory
319 * @offset: a result offset
321 * Check if @mem1 and @mem2 occupy contiguous memory and return the offset of
322 * @mem1 in the parent buffer in @offset.
324 * Returns: %TRUE if @mem1 and @mem2 are in contiguous memory.
326 typedef gboolean (*GstMemoryIsSpanFunction) (GstMemory *mem1, GstMemory *mem2, gsize *offset);
330 * @mem_type: the memory type this allocator provides
331 * @alloc: the implementation of the GstAllocatorAllocFunction
332 * @mem_map: the implementation of the GstMemoryMapFunction
333 * @mem_unmap: the implementation of the GstMemoryUnmapFunction
334 * @mem_free: the implementation of the GstMemoryFreeFunction
335 * @mem_copy: the implementation of the GstMemoryCopyFunction
336 * @mem_share: the implementation of the GstMemoryShareFunction
337 * @mem_is_span: the implementation of the GstMemoryIsSpanFunction
339 * The #GstMemoryInfo is used to register new memory allocators and contain
340 * the implementations for various memory operations.
342 struct _GstMemoryInfo {
343 const gchar *mem_type;
345 GstAllocatorAllocFunction alloc;
347 GstMemoryMapFunction mem_map;
348 GstMemoryUnmapFunction mem_unmap;
349 GstMemoryFreeFunction mem_free;
351 GstMemoryCopyFunction mem_copy;
352 GstMemoryShareFunction mem_share;
353 GstMemoryIsSpanFunction mem_is_span;
356 gpointer _gst_reserved[GST_PADDING];
362 * An opaque type returned from gst_allocator_new() or gst_allocator_find()
363 * that can be used to allocator memory.
367 GstAllocator * gst_allocator_new (const GstMemoryInfo * info,
368 gpointer user_data, GDestroyNotify notify);
369 const gchar * gst_allocator_get_memory_type (GstAllocator * allocator);
371 GstAllocator * gst_allocator_ref (GstAllocator * allocator);
372 void gst_allocator_unref (GstAllocator * allocator);
374 void gst_allocator_register (const gchar *name, GstAllocator *allocator);
375 GstAllocator * gst_allocator_find (const gchar *name);
377 void gst_allocator_set_default (GstAllocator * allocator);
379 /* allocating memory blocks */
380 void gst_allocation_params_init (GstAllocationParams *params);
381 GstAllocationParams *
382 gst_allocation_params_copy (const GstAllocationParams *params) G_GNUC_MALLOC;
383 void gst_allocation_params_free (GstAllocationParams *params);
385 GstMemory * gst_allocator_alloc (GstAllocator * allocator, gsize size,
386 GstAllocationParams *params);
388 GstMemory * gst_memory_new_wrapped (GstMemoryFlags flags, gpointer data, gsize maxsize,
389 gsize offset, gsize size, gpointer user_data,
390 GDestroyNotify notify);
393 GstMemory * gst_memory_ref (GstMemory *mem);
394 void gst_memory_unref (GstMemory *mem);
396 gboolean gst_memory_is_exclusive (GstMemory *mem);
398 /* getting/setting memory properties */
399 gsize gst_memory_get_sizes (GstMemory *mem, gsize *offset, gsize *maxsize);
400 void gst_memory_resize (GstMemory *mem, gssize offset, gsize size);
402 /* retrieving data */
403 GstMemory * gst_memory_make_mapped (GstMemory *mem, GstMapInfo *info, GstMapFlags flags);
404 gboolean gst_memory_map (GstMemory *mem, GstMapInfo *info, GstMapFlags flags);
405 void gst_memory_unmap (GstMemory *mem, GstMapInfo *info);
407 /* copy and subregions */
408 GstMemory * gst_memory_copy (GstMemory *mem, gssize offset, gssize size);
409 GstMemory * gst_memory_share (GstMemory *mem, gssize offset, gssize size);
412 gboolean gst_memory_is_span (GstMemory *mem1, GstMemory *mem2, gsize *offset);
416 #endif /* __GST_MEMORY_H__ */