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 
101 	public static GType getType()
102 	{
103 		return gst_memory_get_type();
104 	}
105 
106 	/**
107 	 * Allocate a new memory block that wraps the given @data.
108 	 *
109 	 * The prefix/padding must be filled with 0 if @flags contains
110 	 * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED respectively.
111 	 *
112 	 * Params:
113 	 *     flags = #GstMemoryFlags
114 	 *     data = data to
115 	 *         wrap
116 	 *     maxsize = allocated size of @data
117 	 *     offset = offset in @data
118 	 *     size = size of valid data
119 	 *     userData = user_data
120 	 *     notify = called with @user_data when the memory is freed
121 	 *
122 	 * Return: a new #GstMemory.
123 	 *
124 	 * Throws: ConstructionException GTK+ fails to create the object.
125 	 */
126 	public this(GstMemoryFlags flags, ubyte[] data, size_t maxsize, size_t offset, void* userData, GDestroyNotify notify)
127 	{
128 		auto p = gst_memory_new_wrapped(flags, data.ptr, maxsize, offset, cast(size_t)data.length, userData, notify);
129 		
130 		if(p is null)
131 		{
132 			throw new ConstructionException("null returned by new_wrapped");
133 		}
134 		
135 		this(cast(GstMemory*) p);
136 	}
137 
138 	/**
139 	 * Return a copy of @size bytes from @mem starting from @offset. This copy is
140 	 * guaranteed to be writable. @size can be set to -1 to return a copy all bytes
141 	 * from @offset.
142 	 *
143 	 * Params:
144 	 *     offset = an offset to copy
145 	 *     size = size to copy or -1 to copy all bytes from offset
146 	 *
147 	 * Return: a new #GstMemory.
148 	 */
149 	public Memory copy(ptrdiff_t offset, ptrdiff_t size)
150 	{
151 		auto p = gst_memory_copy(gstMemory, offset, size);
152 		
153 		if(p is null)
154 		{
155 			return null;
156 		}
157 		
158 		return ObjectG.getDObject!(Memory)(cast(GstMemory*) p);
159 	}
160 
161 	/**
162 	 * Get the current @size, @offset and @maxsize of @mem.
163 	 *
164 	 * Params:
165 	 *     offset = pointer to offset
166 	 *     maxsize = pointer to maxsize
167 	 *
168 	 * Return: the current sizes of @mem
169 	 */
170 	public size_t getSizes(size_t* offset, size_t* maxsize)
171 	{
172 		return gst_memory_get_sizes(gstMemory, offset, maxsize);
173 	}
174 
175 	/**
176 	 * Initializes a newly allocated @mem with the given parameters. This function
177 	 * will call gst_mini_object_init() with the default memory parameters.
178 	 *
179 	 * Params:
180 	 *     flags = #GstMemoryFlags
181 	 *     allocator = the #GstAllocator
182 	 *     parent = the parent of @mem
183 	 *     maxsize = the total size of the memory
184 	 *     alig = the alignment of the memory
185 	 *     offset = The offset in the memory
186 	 *     size = the size of valid data in the memory
187 	 */
188 	public void init(GstMemoryFlags flags, Allocator allocator, Memory parent, size_t maxsize, size_t alig, size_t offset, size_t size)
189 	{
190 		gst_memory_init(gstMemory, flags, (allocator is null) ? null : allocator.getAllocatorStruct(), (parent is null) ? null : parent.getMemoryStruct(), maxsize, alig, offset, size);
191 	}
192 
193 	/**
194 	 * Check if @mem1 and mem2 share the memory with a common parent memory object
195 	 * and that the memory is contiguous.
196 	 *
197 	 * If this is the case, the memory of @mem1 and @mem2 can be merged
198 	 * efficiently by performing gst_memory_share() on the parent object from
199 	 * the returned @offset.
200 	 *
201 	 * Params:
202 	 *     mem2 = a #GstMemory
203 	 *     offset = a pointer to a result offset
204 	 *
205 	 * Return: %TRUE if the memory is contiguous and of a common parent.
206 	 */
207 	public bool isSpan(Memory mem2, size_t* offset)
208 	{
209 		return gst_memory_is_span(gstMemory, (mem2 is null) ? null : mem2.getMemoryStruct(), offset) != 0;
210 	}
211 
212 	/**
213 	 * Check if @mem if allocated with an allocator for @mem_type.
214 	 *
215 	 * Params:
216 	 *     memType = a memory type
217 	 *
218 	 * Return: %TRUE if @mem was allocated from an allocator for @mem_type.
219 	 *
220 	 * Since: 1.2
221 	 */
222 	public bool isType(string memType)
223 	{
224 		return gst_memory_is_type(gstMemory, Str.toStringz(memType)) != 0;
225 	}
226 
227 	/**
228 	 * Create a #GstMemory object that is mapped with @flags. If @mem is mappable
229 	 * with @flags, this function returns the mapped @mem directly. Otherwise a
230 	 * mapped copy of @mem is returned.
231 	 *
232 	 * This function takes ownership of old @mem and returns a reference to a new
233 	 * #GstMemory.
234 	 *
235 	 * Params:
236 	 *     info = pointer for info
237 	 *     flags = mapping flags
238 	 *
239 	 * Return: a #GstMemory object mapped
240 	 *     with @flags or %NULL when a mapping is not possible.
241 	 */
242 	public Memory makeMapped(out GstMapInfo info, GstMapFlags flags)
243 	{
244 		auto p = gst_memory_make_mapped(gstMemory, &info, flags);
245 		
246 		if(p is null)
247 		{
248 			return null;
249 		}
250 		
251 		return ObjectG.getDObject!(Memory)(cast(GstMemory*) p);
252 	}
253 
254 	/**
255 	 * Fill @info with the pointer and sizes of the memory in @mem that can be
256 	 * accessed according to @flags.
257 	 *
258 	 * This function can return %FALSE for various reasons:
259 	 * - the memory backed by @mem is not accessible with the given @flags.
260 	 * - the memory was already mapped with a different mapping.
261 	 *
262 	 * @info and its contents remain valid for as long as @mem is valid and
263 	 * until gst_memory_unmap() is called.
264 	 *
265 	 * For each gst_memory_map() call, a corresponding gst_memory_unmap() call
266 	 * should be done.
267 	 *
268 	 * Params:
269 	 *     info = pointer for info
270 	 *     flags = mapping flags
271 	 *
272 	 * Return: %TRUE if the map operation was successful.
273 	 */
274 	public bool map(out GstMapInfo info, GstMapFlags flags)
275 	{
276 		return gst_memory_map(gstMemory, &info, flags) != 0;
277 	}
278 
279 	/**
280 	 * Resize the memory region. @mem should be writable and offset + size should be
281 	 * less than the maxsize of @mem.
282 	 *
283 	 * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED will be
284 	 * cleared when offset or padding is increased respectively.
285 	 *
286 	 * Params:
287 	 *     offset = a new offset
288 	 *     size = a new size
289 	 */
290 	public void resize(ptrdiff_t offset, size_t size)
291 	{
292 		gst_memory_resize(gstMemory, offset, size);
293 	}
294 
295 	/**
296 	 * Return a shared copy of @size bytes from @mem starting from @offset. No
297 	 * memory copy is performed and the memory region is simply shared. The result
298 	 * is guaranteed to be not-writable. @size can be set to -1 to return a share
299 	 * all bytes from @offset.
300 	 *
301 	 * Params:
302 	 *     offset = an offset to share
303 	 *     size = size to share or -1 to share bytes from offset
304 	 *
305 	 * Return: a new #GstMemory.
306 	 */
307 	public Memory share(ptrdiff_t offset, ptrdiff_t size)
308 	{
309 		auto p = gst_memory_share(gstMemory, offset, size);
310 		
311 		if(p is null)
312 		{
313 			return null;
314 		}
315 		
316 		return ObjectG.getDObject!(Memory)(cast(GstMemory*) p);
317 	}
318 
319 	/**
320 	 * Release the memory obtained with gst_memory_map()
321 	 *
322 	 * Params:
323 	 *     info = a #GstMapInfo
324 	 */
325 	public void unmap(GstMapInfo* info)
326 	{
327 		gst_memory_unmap(gstMemory, info);
328 	}
329 }