1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
|
/* GStreamer
* Copyright (C) 2011 Wim Taymans <wim.taymans@gmail.be>
*
* gstmemory.c: memory block handling
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
/**
* SECTION:gstmemory
* @short_description: refcounted wrapper for memory blocks
* @see_also: #GstBuffer
*
* GstMemory is a lightweight refcounted object that wraps a region of memory.
* They are typically used to manage the data of a #GstBuffer.
*
* A GstMemory object has an allocated region of memory of maxsize. The maximum
* size does not change during the lifetime of the memory object. The memory
* also has an offset and size property that specifies the valid range of memory
* in the allocated region.
*
* Memory is usually created by allocators with a gst_allocator_alloc()
* method call. When %NULL is used as the allocator, the default allocator will
* be used.
*
* New allocators can be registered with gst_allocator_register().
* Allocators are identified by name and can be retrieved with
* gst_allocator_find(). gst_allocator_set_default() can be used to change the
* default allocator.
*
* New memory can be created with gst_memory_new_wrapped() that wraps the memory
* allocated elsewhere.
*
* Refcounting of the memory block is performed with gst_memory_ref() and
* gst_memory_unref().
*
* The size of the memory can be retrieved and changed with
* gst_memory_get_sizes() and gst_memory_resize() respectively.
*
* Getting access to the data of the memory is performed with gst_memory_map().
* The call will return a pointer to offset bytes into the region of memory.
* After the memory access is completed, gst_memory_unmap() should be called.
*
* Memory can be copied with gst_memory_copy(), which will return a writable
* copy. gst_memory_share() will create a new memory block that shares the
* memory with an existing memory block at a custom offset and with a custom
* size.
*
* Memory can be efficiently merged when gst_memory_is_span() returns %TRUE.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "gst_private.h"
#include "gstmemory.h"
GType _gst_memory_type = 0;
GST_DEFINE_MINI_OBJECT_TYPE (GstMemory, gst_memory);
static GstMemory *
_gst_memory_copy (GstMemory * mem)
{
GST_CAT_DEBUG (GST_CAT_MEMORY, "copy memory %p", mem);
return gst_memory_copy (mem, 0, -1);
}
static void
_gst_memory_free (GstMemory * mem)
{
GstAllocator *allocator;
GST_CAT_DEBUG (GST_CAT_MEMORY, "free memory %p", mem);
if (mem->parent) {
gst_memory_unlock (mem->parent, GST_LOCK_FLAG_EXCLUSIVE);
gst_memory_unref (mem->parent);
}
allocator = mem->allocator;
gst_allocator_free (allocator, mem);
gst_object_unref (allocator);
}
/**
* gst_memory_init: (skip)
* @mem: a #GstMemory
* @flags: #GstMemoryFlags
* @allocator: the #GstAllocator
* @parent: the parent of @mem
* @maxsize: the total size of the memory
* @align: the alignment of the memory
* @offset: The offset in the memory
* @size: the size of valid data in the memory
* Initializes a newly allocated @mem with the given parameters. This function
* will call gst_mini_object_init() with the default memory parameters.
*/
void
gst_memory_init (GstMemory * mem, GstMemoryFlags flags,
GstAllocator * allocator, GstMemory * parent, gsize maxsize, gsize align,
gsize offset, gsize size)
{
gst_mini_object_init (GST_MINI_OBJECT_CAST (mem),
flags | GST_MINI_OBJECT_FLAG_LOCKABLE, GST_TYPE_MEMORY,
(GstMiniObjectCopyFunction) _gst_memory_copy, NULL,
(GstMiniObjectFreeFunction) _gst_memory_free);
mem->allocator = gst_object_ref (allocator);
if (parent) {
gst_memory_lock (parent, GST_LOCK_FLAG_EXCLUSIVE);
gst_memory_ref (parent);
}
mem->parent = parent;
mem->maxsize = maxsize;
mem->align = align;
mem->offset = offset;
mem->size = size;
GST_CAT_DEBUG (GST_CAT_MEMORY, "new memory %p, maxsize:%" G_GSIZE_FORMAT
" offset:%" G_GSIZE_FORMAT " size:%" G_GSIZE_FORMAT, mem, maxsize,
offset, size);
}
/**
* gst_memory_is_type:
* @mem: a #GstMemory
* @mem_type: a memory type
*
* Check if @mem if allocated with an allocator for @mem_type.
*
* Returns: %TRUE if @mem was allocated from an allocator for @mem_type.
*
* Since: 1.2
*/
gboolean
gst_memory_is_type (GstMemory * mem, const gchar * mem_type)
{
g_return_val_if_fail (mem != NULL, FALSE);
g_return_val_if_fail (mem->allocator != NULL, FALSE);
g_return_val_if_fail (mem_type != NULL, FALSE);
return (g_strcmp0 (mem->allocator->mem_type, mem_type) == 0);
}
/**
* gst_memory_get_sizes:
* @mem: a #GstMemory
* @offset: pointer to offset
* @maxsize: pointer to maxsize
*
* Get the current @size, @offset and @maxsize of @mem.
*
* Returns: the current sizes of @mem
*/
gsize
gst_memory_get_sizes (GstMemory * mem, gsize * offset, gsize * maxsize)
{
g_return_val_if_fail (mem != NULL, 0);
if (offset)
*offset = mem->offset;
if (maxsize)
*maxsize = mem->maxsize;
return mem->size;
}
/**
* gst_memory_resize:
* @mem: a #GstMemory
* @offset: a new offset
* @size: a new size
*
* Resize the memory region. @mem should be writable and offset + size should be
* less than the maxsize of @mem.
*
* #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED will be
* cleared when offset or padding is increased respectively.
*/
void
gst_memory_resize (GstMemory * mem, gssize offset, gsize size)
{
g_return_if_fail (mem != NULL);
g_return_if_fail (offset >= 0 || mem->offset >= -offset);
g_return_if_fail (size + mem->offset + offset <= mem->maxsize);
/* if we increase the prefix, we can't guarantee it is still 0 filled */
if ((offset > 0) && GST_MEMORY_IS_ZERO_PREFIXED (mem))
GST_MEMORY_FLAG_UNSET (mem, GST_MEMORY_FLAG_ZERO_PREFIXED);
/* if we increase the padding, we can't guarantee it is still 0 filled */
if ((offset + size < mem->size) && GST_MEMORY_IS_ZERO_PADDED (mem))
GST_MEMORY_FLAG_UNSET (mem, GST_MEMORY_FLAG_ZERO_PADDED);
mem->offset += offset;
mem->size = size;
}
/**
* gst_memory_make_mapped:
* @mem: (transfer full): a #GstMemory
* @info: (out): pointer for info
* @flags: mapping flags
*
* Create a #GstMemory object that is mapped with @flags. If @mem is mappable
* with @flags, this function returns the mapped @mem directly. Otherwise a
* mapped copy of @mem is returned.
*
* This function takes ownership of old @mem and returns a reference to a new
* #GstMemory.
*
* Returns: (transfer full) (nullable): a #GstMemory object mapped
* with @flags or %NULL when a mapping is not possible.
*/
GstMemory *
gst_memory_make_mapped (GstMemory * mem, GstMapInfo * info, GstMapFlags flags)
{
GstMemory *result;
if (gst_memory_map (mem, info, flags)) {
result = mem;
} else {
result = gst_memory_copy (mem, 0, -1);
gst_memory_unref (mem);
if (result == NULL)
goto cannot_copy;
if (!gst_memory_map (result, info, flags))
goto cannot_map;
}
return result;
/* ERRORS */
cannot_copy:
{
GST_CAT_DEBUG (GST_CAT_MEMORY, "cannot copy memory %p", mem);
return NULL;
}
cannot_map:
{
GST_CAT_DEBUG (GST_CAT_MEMORY, "cannot map memory %p with flags %d", mem,
flags);
gst_memory_unref (result);
return NULL;
}
}
/**
* gst_memory_map:
* @mem: a #GstMemory
* @info: (out): pointer for info
* @flags: mapping flags
*
* Fill @info with the pointer and sizes of the memory in @mem that can be
* accessed according to @flags.
*
* This function can return %FALSE for various reasons:
* - the memory backed by @mem is not accessible with the given @flags.
* - the memory was already mapped with a different mapping.
*
* @info and its contents remain valid for as long as @mem is valid and
* until gst_memory_unmap() is called.
*
* For each gst_memory_map() call, a corresponding gst_memory_unmap() call
* should be done.
*
* Returns: %TRUE if the map operation was successful.
*/
gboolean
gst_memory_map (GstMemory * mem, GstMapInfo * info, GstMapFlags flags)
{
g_return_val_if_fail (mem != NULL, FALSE);
g_return_val_if_fail (info != NULL, FALSE);
if (!gst_memory_lock (mem, (GstLockFlags) flags))
goto lock_failed;
info->data = mem->allocator->mem_map (mem, mem->maxsize, flags);
if (G_UNLIKELY (info->data == NULL))
goto error;
info->memory = mem;
info->flags = flags;
info->size = mem->size;
info->maxsize = mem->maxsize - mem->offset;
info->data = info->data + mem->offset;
return TRUE;
/* ERRORS */
lock_failed:
{
GST_CAT_DEBUG (GST_CAT_MEMORY, "mem %p: lock %d failed", mem, flags);
memset (info, 0, sizeof (GstMapInfo));
return FALSE;
}
error:
{
/* something went wrong, restore the orginal state again */
GST_CAT_ERROR (GST_CAT_MEMORY, "mem %p: subclass map failed", mem);
gst_memory_unlock (mem, (GstLockFlags) flags);
memset (info, 0, sizeof (GstMapInfo));
return FALSE;
}
}
/**
* gst_memory_unmap:
* @mem: a #GstMemory
* @info: a #GstMapInfo
*
* Release the memory obtained with gst_memory_map()
*/
void
gst_memory_unmap (GstMemory * mem, GstMapInfo * info)
{
g_return_if_fail (mem != NULL);
g_return_if_fail (info != NULL);
g_return_if_fail (info->memory == mem);
mem->allocator->mem_unmap (mem);
gst_memory_unlock (mem, (GstLockFlags) info->flags);
}
/**
* gst_memory_copy:
* @mem: a #GstMemory
* @offset: an offset to copy
* @size: size to copy or -1 to copy all bytes from offset
*
* Return a copy of @size bytes from @mem starting from @offset. This copy is
* guaranteed to be writable. @size can be set to -1 to return a copy all bytes
* from @offset.
*
* Returns: a new #GstMemory.
*/
GstMemory *
gst_memory_copy (GstMemory * mem, gssize offset, gssize size)
{
GstMemory *copy;
g_return_val_if_fail (mem != NULL, NULL);
copy = mem->allocator->mem_copy (mem, offset, size);
return copy;
}
/**
* gst_memory_share:
* @mem: a #GstMemory
* @offset: an offset to share
* @size: size to share or -1 to share bytes from offset
*
* Return a shared copy of @size bytes from @mem starting from @offset. No
* memory copy is performed and the memory region is simply shared. The result
* is guaranteed to be not-writable. @size can be set to -1 to return a share
* all bytes from @offset.
*
* Returns: a new #GstMemory.
*/
GstMemory *
gst_memory_share (GstMemory * mem, gssize offset, gssize size)
{
GstMemory *shared;
g_return_val_if_fail (mem != NULL, NULL);
g_return_val_if_fail (!GST_MEMORY_FLAG_IS_SET (mem, GST_MEMORY_FLAG_NO_SHARE),
NULL);
shared = mem->allocator->mem_share (mem, offset, size);
return shared;
}
/**
* gst_memory_is_span:
* @mem1: a #GstMemory
* @mem2: a #GstMemory
* @offset: a pointer to a result offset
*
* Check if @mem1 and mem2 share the memory with a common parent memory object
* and that the memory is contiguous.
*
* If this is the case, the memory of @mem1 and @mem2 can be merged
* efficiently by performing gst_memory_share() on the parent object from
* the returned @offset.
*
* Returns: %TRUE if the memory is contiguous and of a common parent.
*/
gboolean
gst_memory_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
{
g_return_val_if_fail (mem1 != NULL, FALSE);
g_return_val_if_fail (mem2 != NULL, FALSE);
/* need to have the same allocators */
if (mem1->allocator != mem2->allocator)
return FALSE;
/* need to have the same parent */
if (mem1->parent == NULL || mem1->parent != mem2->parent)
return FALSE;
/* and memory is contiguous */
if (!mem1->allocator->mem_is_span (mem1, mem2, offset))
return FALSE;
return TRUE;
}
void
_priv_gst_memory_initialize (void)
{
_gst_memory_type = gst_memory_get_type ();
}
|