1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gstreamer.Memory;
26 
27 private import glib.ConstructionException;
28 private import glib.Str;
29 private import gobject.ObjectG;
30 private import gstreamer.Allocator;
31 private import gstreamerc.gstreamer;
32 public  import gstreamerc.gstreamertypes;
33 
34 
35 /**
36  * GstMemory is a lightweight refcounted object that wraps a region of memory.
37  * They are typically used to manage the data of a #GstBuffer.
38  * 
39  * A GstMemory object has an allocated region of memory of maxsize. The maximum
40  * size does not change during the lifetime of the memory object. The memory
41  * also has an offset and size property that specifies the valid range of memory
42  * in the allocated region.
43  * 
44  * Memory is usually created by allocators with a gst_allocator_alloc()
45  * method call. When %NULL is used as the allocator, the default allocator will
46  * be used.
47  * 
48  * New allocators can be registered with gst_allocator_register().
49  * Allocators are identified by name and can be retrieved with
50  * gst_allocator_find(). gst_allocator_set_default() can be used to change the
51  * default allocator.
52  * 
53  * New memory can be created with gst_memory_new_wrapped() that wraps the memory
54  * allocated elsewhere.
55  * 
56  * Refcounting of the memory block is performed with gst_memory_ref() and
57  * gst_memory_unref().
58  * 
59  * The size of the memory can be retrieved and changed with
60  * gst_memory_get_sizes() and gst_memory_resize() respectively.
61  * 
62  * Getting access to the data of the memory is performed with gst_memory_map().
63  * The call will return a pointer to offset bytes into the region of memory.
64  * After the memory access is completed, gst_memory_unmap() should be called.
65  * 
66  * Memory can be copied with gst_memory_copy(), which will return a writable
67  * copy. gst_memory_share() will create a new memory block that shares the
68  * memory with an existing memory block at a custom offset and with a custom
69  * size.
70  * 
71  * Memory can be efficiently merged when gst_memory_is_span() returns %TRUE.
72  */
73 public class Memory
74 {
75 	/** the main Gtk struct */
76 	protected GstMemory* gstMemory;
77 
78 	/** Get the main Gtk struct */
79 	public GstMemory* getMemoryStruct()
80 	{
81 		return gstMemory;
82 	}
83 
84 	/** the main Gtk struct as a void* */
85 	protected void* getStruct()
86 	{
87 		return cast(void*)gstMemory;
88 	}
89 
90 	/**
91 	 * Sets our main struct and passes it to the parent class.
92 	 */
93 	public this (GstMemory* gstMemory)
94 	{
95 		this.gstMemory = gstMemory;
96 	}
97 
98 
99 	/** */
100 	public static GType getType()
101 	{
102 		return gst_memory_get_type();
103 	}
104 
105 	/**
106 	 * Allocate a new memory block that wraps the given @data.
107 	 *
108 	 * The prefix/padding must be filled with 0 if @flags contains
109 	 * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED respectively.
110 	 *
111 	 * Params:
112 	 *     flags = #GstMemoryFlags
113 	 *     data = data to
114 	 *         wrap
115 	 *     maxsize = allocated size of @data
116 	 *     offset = offset in @data
117 	 *     size = size of valid data
118 	 *     userData = user_data
119 	 *     notify = called with @user_data when the memory is freed
120 	 *
121 	 * Return: a new #GstMemory.
122 	 *
123 	 * Throws: ConstructionException GTK+ fails to create the object.
124 	 */
125 	public this(GstMemoryFlags flags, ubyte[] data, size_t maxsize, size_t offset, void* userData, GDestroyNotify notify)
126 	{
127 		auto p = gst_memory_new_wrapped(flags, data.ptr, maxsize, offset, cast(size_t)data.length, userData, notify);
128 		
129 		if(p is null)
130 		{
131 			throw new ConstructionException("null returned by new_wrapped");
132 		}
133 		
134 		this(cast(GstMemory*) p);
135 	}
136 
137 	/**
138 	 * Return a copy of @size bytes from @mem starting from @offset. This copy is
139 	 * guaranteed to be writable. @size can be set to -1 to return a copy
140 	 * from @offset to the end of the memory region.
141 	 *
142 	 * Params:
143 	 *     offset = offset to copy from
144 	 *     size = size to copy, or -1 to copy to the end of the memory region
145 	 *
146 	 * Return: a new #GstMemory.
147 	 */
148 	public Memory copy(ptrdiff_t offset, ptrdiff_t size)
149 	{
150 		auto p = gst_memory_copy(gstMemory, offset, size);
151 		
152 		if(p is null)
153 		{
154 			return null;
155 		}
156 		
157 		return ObjectG.getDObject!(Memory)(cast(GstMemory*) p);
158 	}
159 
160 	/**
161 	 * Get the current @size, @offset and @maxsize of @mem.
162 	 *
163 	 * Params:
164 	 *     offset = pointer to offset
165 	 *     maxsize = pointer to maxsize
166 	 *
167 	 * Return: the current sizes of @mem
168 	 */
169 	public size_t getSizes(size_t* offset, size_t* maxsize)
170 	{
171 		return gst_memory_get_sizes(gstMemory, offset, maxsize);
172 	}
173 
174 	/**
175 	 * Initializes a newly allocated @mem with the given parameters. This function
176 	 * will call gst_mini_object_init() with the default memory parameters.
177 	 *
178 	 * Params:
179 	 *     flags = #GstMemoryFlags
180 	 *     allocator = the #GstAllocator
181 	 *     parent = the parent of @mem
182 	 *     maxsize = the total size of the memory
183 	 *     alig = the alignment of the memory
184 	 *     offset = The offset in the memory
185 	 *     size = the size of valid data in the memory
186 	 */
187 	public void init(GstMemoryFlags flags, Allocator allocator, Memory parent, size_t maxsize, size_t alig, size_t offset, size_t size)
188 	{
189 		gst_memory_init(gstMemory, flags, (allocator is null) ? null : allocator.getAllocatorStruct(), (parent is null) ? null : parent.getMemoryStruct(), maxsize, alig, offset, size);
190 	}
191 
192 	/**
193 	 * Check if @mem1 and mem2 share the memory with a common parent memory object
194 	 * and that the memory is contiguous.
195 	 *
196 	 * If this is the case, the memory of @mem1 and @mem2 can be merged
197 	 * efficiently by performing gst_memory_share() on the parent object from
198 	 * the returned @offset.
199 	 *
200 	 * Params:
201 	 *     mem2 = a #GstMemory
202 	 *     offset = a pointer to a result offset
203 	 *
204 	 * Return: %TRUE if the memory is contiguous and of a common parent.
205 	 */
206 	public bool isSpan(Memory mem2, size_t* offset)
207 	{
208 		return gst_memory_is_span(gstMemory, (mem2 is null) ? null : mem2.getMemoryStruct(), offset) != 0;
209 	}
210 
211 	/**
212 	 * Check if @mem if allocated with an allocator for @mem_type.
213 	 *
214 	 * Params:
215 	 *     memType = a memory type
216 	 *
217 	 * Return: %TRUE if @mem was allocated from an allocator for @mem_type.
218 	 *
219 	 * Since: 1.2
220 	 */
221 	public bool isType(string memType)
222 	{
223 		return gst_memory_is_type(gstMemory, Str.toStringz(memType)) != 0;
224 	}
225 
226 	/**
227 	 * Create a #GstMemory object that is mapped with @flags. If @mem is mappable
228 	 * with @flags, this function returns the mapped @mem directly. Otherwise a
229 	 * mapped copy of @mem is returned.
230 	 *
231 	 * This function takes ownership of old @mem and returns a reference to a new
232 	 * #GstMemory.
233 	 *
234 	 * Params:
235 	 *     info = pointer for info
236 	 *     flags = mapping flags
237 	 *
238 	 * Return: a #GstMemory object mapped
239 	 *     with @flags or %NULL when a mapping is not possible.
240 	 */
241 	public Memory makeMapped(out GstMapInfo info, GstMapFlags flags)
242 	{
243 		auto p = gst_memory_make_mapped(gstMemory, &info, flags);
244 		
245 		if(p is null)
246 		{
247 			return null;
248 		}
249 		
250 		return ObjectG.getDObject!(Memory)(cast(GstMemory*) p);
251 	}
252 
253 	/**
254 	 * Fill @info with the pointer and sizes of the memory in @mem that can be
255 	 * accessed according to @flags.
256 	 *
257 	 * This function can return %FALSE for various reasons:
258 	 * - the memory backed by @mem is not accessible with the given @flags.
259 	 * - the memory was already mapped with a different mapping.
260 	 *
261 	 * @info and its contents remain valid for as long as @mem is valid and
262 	 * until gst_memory_unmap() is called.
263 	 *
264 	 * For each gst_memory_map() call, a corresponding gst_memory_unmap() call
265 	 * should be done.
266 	 *
267 	 * Params:
268 	 *     info = pointer for info
269 	 *     flags = mapping flags
270 	 *
271 	 * Return: %TRUE if the map operation was successful.
272 	 */
273 	public bool map(out GstMapInfo info, GstMapFlags flags)
274 	{
275 		return gst_memory_map(gstMemory, &info, flags) != 0;
276 	}
277 
278 	/**
279 	 * Resize the memory region. @mem should be writable and offset + size should be
280 	 * less than the maxsize of @mem.
281 	 *
282 	 * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED will be
283 	 * cleared when offset or padding is increased respectively.
284 	 *
285 	 * Params:
286 	 *     offset = a new offset
287 	 *     size = a new size
288 	 */
289 	public void resize(ptrdiff_t offset, size_t size)
290 	{
291 		gst_memory_resize(gstMemory, offset, size);
292 	}
293 
294 	/**
295 	 * Return a shared copy of @size bytes from @mem starting from @offset. No
296 	 * memory copy is performed and the memory region is simply shared. The result
297 	 * is guaranteed to be non-writable. @size can be set to -1 to return a shared
298 	 * copy from @offset to the end of the memory region.
299 	 *
300 	 * Params:
301 	 *     offset = offset to share from
302 	 *     size = size to share, or -1 to share to the end of the memory region
303 	 *
304 	 * Return: a new #GstMemory.
305 	 */
306 	public Memory share(ptrdiff_t offset, ptrdiff_t size)
307 	{
308 		auto p = gst_memory_share(gstMemory, offset, size);
309 		
310 		if(p is null)
311 		{
312 			return null;
313 		}
314 		
315 		return ObjectG.getDObject!(Memory)(cast(GstMemory*) p);
316 	}
317 
318 	/**
319 	 * Release the memory obtained with gst_memory_map()
320 	 *
321 	 * Params:
322 	 *     info = a #GstMapInfo
323 	 */
324 	public void unmap(GstMapInfo* info)
325 	{
326 		gst_memory_unmap(gstMemory, info);
327 	}
328 }