gstmemory.c 8.15 KB
Newer Older
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
/* 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., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include "gst_private.h"

#include "gstmemory.h"

struct _GstMemoryImpl
{
  GQuark name;

  GstMemoryInfo info;
};

typedef struct
{
  GstMemory mem;

  guint8 *data;
  GFreeFunc free_func;
41
  gpointer free_data;
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
  gsize maxsize;
  gsize offset;
  gsize size;
} GstMemoryDefault;

static const GstMemoryImpl *_default_memory_impl;

static gsize
_default_get_sizes (GstMemory * mem, gsize * maxsize)
{
  GstMemoryDefault *def = (GstMemoryDefault *) mem;

  if (maxsize)
    *maxsize = def->maxsize;

  return def->size;
}

static gpointer
_default_map (GstMemory * mem, gsize * size, gsize * maxsize, GstMapFlags flags)
{
  GstMemoryDefault *def = (GstMemoryDefault *) mem;

  if (size)
    *size = def->size;
  if (maxsize)
    *maxsize = def->maxsize;

  return def->data + def->offset;
}

static gboolean
_default_unmap (GstMemory * mem, gpointer data, gsize size)
{
  GstMemoryDefault *def = (GstMemoryDefault *) mem;

  def->size = size;

  return TRUE;
}

static void
_default_free (GstMemory * mem)
{
  GstMemoryDefault *def = (GstMemoryDefault *) mem;

  if (def->free_func)
89
    def->free_func (def->free_data);
90 91 92 93 94 95 96 97 98 99 100
}

static GstMemory *
_default_copy (GstMemory * mem)
{
  GstMemoryDefault *def = (GstMemoryDefault *) mem;
  GstMemoryDefault *copy;

  copy = g_slice_new (GstMemoryDefault);
  copy->mem.impl = _default_memory_impl;
  copy->data = g_memdup (def->data, def->maxsize);
101
  copy->free_data = copy->data;
102 103 104 105 106 107 108 109
  copy->free_func = g_free;
  copy->maxsize = def->maxsize;
  copy->offset = def->offset;
  copy->size = def->size;

  return (GstMemory *) copy;
}

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
static void
_default_copy_into (GstMemory * mem, gsize offset, gpointer dest, gsize size)
{
  GstMemoryDefault *def = (GstMemoryDefault *) mem;

  g_return_if_fail (size + def->offset + offset > def->maxsize);

  memcpy (dest, def->data + def->offset + offset, size);
}

static void
_default_trim (GstMemory * mem, gsize offset, gsize size)
{
  GstMemoryDefault *def = (GstMemoryDefault *) mem;

  g_return_if_fail (size + def->offset + offset > def->maxsize);

  def->offset += offset;
  def->size = size;
}

static GstMemory *
_default_sub (GstMemory * mem, gsize offset, gsize size)
{
  GstMemoryDefault *def = (GstMemoryDefault *) mem;
  GstMemoryDefault *sub;

  sub = g_slice_new (GstMemoryDefault);
  sub->mem.impl = _default_memory_impl;
  sub->data = def->data;
  sub->free_data = gst_memory_ref (mem);
  sub->free_func = (GFreeFunc) gst_memory_unref;
  sub->maxsize = def->maxsize;
  sub->offset = def->offset + offset;
  sub->size = size;

  return (GstMemory *) sub;
}

static void
_fallback_copy_into (GstMemory * mem, gsize offset, gpointer dest, gsize size)
{
  guint8 *data;
  gsize msize;

  data = gst_memory_map (mem, &msize, NULL, GST_MAP_READ);
  memcpy (dest, data + offset, size);
  gst_memory_unmap (mem, data, msize);
}

static GstMemory *
_fallback_copy (GstMemory * mem)
{
  GstMemory *copy;
  gpointer data, cdata;
  gsize size;

  data = gst_memory_map (mem, &size, NULL, GST_MAP_READ);
  cdata = g_memdup (data, size);
  gst_memory_unmap (mem, data, size);

  copy = gst_memory_new_wrapped (cdata, g_free, size, 0, size);

  return copy;
}

static GstMemory *
_fallback_sub (GstMemory * mem, gsize offset, gsize size)
{
  GstMemoryDefault *def = (GstMemoryDefault *) mem;
  GstMemoryDefault *sub;

  sub = g_slice_new (GstMemoryDefault);
  sub->mem.impl = _default_memory_impl;
  sub->data = def->data;
  sub->free_data = sub->data;
  sub->free_func = NULL;
  sub->maxsize = def->maxsize;
  sub->offset = def->offset + offset;
  sub->size = size;

  return (GstMemory *) sub;
}

static gboolean
_fallback_is_span (GstMemory * mem1, GstMemory * mem2)
{
  return FALSE;
}

static GstMemory *
_fallback_span (GstMemory * mem1, gsize offset, GstMemory * mem2, gsize size)
{
  return FALSE;
}

206 207 208 209 210
const GstMemoryImpl *
gst_memory_register (const gchar * name, const GstMemoryInfo * info)
{
  GstMemoryImpl *impl;

211 212 213
#define INSTALL_FALLBACK(_t) \
  if (impl->info._t == NULL) impl->info._t = _fallback_ ##_t;

214 215
  g_return_val_if_fail (name != NULL, NULL);
  g_return_val_if_fail (info != NULL, NULL);
216 217 218 219 220
  g_return_val_if_fail (info->get_sizes != NULL, NULL);
  g_return_val_if_fail (info->trim != NULL, NULL);
  g_return_val_if_fail (info->map != NULL, NULL);
  g_return_val_if_fail (info->unmap != NULL, NULL);
  g_return_val_if_fail (info->free != NULL, NULL);
221 222 223 224

  impl = g_slice_new (GstMemoryImpl);
  impl->name = g_quark_from_string (name);
  impl->info = *info;
225 226 227 228 229
  INSTALL_FALLBACK (copy);
  INSTALL_FALLBACK (copy_into);
  INSTALL_FALLBACK (sub);
  INSTALL_FALLBACK (is_span);
  INSTALL_FALLBACK (span);
230 231 232 233 234 235 236 237

  GST_DEBUG ("register \"%s\" of size %" G_GSIZE_FORMAT, name);

#if 0
  g_static_rw_lock_writer_lock (&lock);
  g_hash_table_insert (memoryimpl, (gpointer) name, (gpointer) impl);
  g_static_rw_lock_writer_unlock (&lock);
#endif
238
#undef INSTALL_FALLBACK
239 240 241 242 243 244 245 246 247

  return impl;
}

void
_gst_memory_init (void)
{
  static const GstMemoryInfo info = {
    _default_get_sizes,
248
    _default_trim,
249 250 251
    _default_map,
    _default_unmap,
    _default_free,
252 253 254
    _default_copy,
    _default_copy_into,
    _default_sub
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
  };
  _default_memory_impl = gst_memory_register ("GstMemoryDefault", &info);
}

GstMemory *
gst_memory_ref (GstMemory * mem)
{
  g_return_val_if_fail (mem != NULL, NULL);

  g_atomic_int_inc (&mem->refcount);

  return mem;
}

void
gst_memory_unref (GstMemory * mem)
{
  g_return_if_fail (mem != NULL);

  if (g_atomic_int_dec_and_test (&mem->refcount))
    mem->impl->info.free (mem);
}

gsize
gst_memory_get_sizes (GstMemory * mem, gsize * maxsize)
{
  g_return_val_if_fail (mem != NULL, 0);

  return mem->impl->info.get_sizes (mem, maxsize);
}

gpointer
gst_memory_map (GstMemory * mem, gsize * size, gsize * maxsize,
    GstMapFlags flags)
{
  g_return_val_if_fail (mem != NULL, NULL);

  return mem->impl->info.map (mem, size, maxsize, flags);
}

gboolean
gst_memory_unmap (GstMemory * mem, gpointer data, gsize size)
{
  g_return_val_if_fail (mem != NULL, FALSE);

  return mem->impl->info.unmap (mem, data, size);
}

GstMemory *
gst_memory_copy (GstMemory * mem)
{
  g_return_val_if_fail (mem != NULL, NULL);

  return mem->impl->info.copy (mem);
}

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
void
gst_memory_copy_into (GstMemory * mem, gsize offset, gpointer dest, gsize size)
{
  g_return_if_fail (mem != NULL);
  g_return_if_fail (dest != NULL);

  return mem->impl->info.copy_into (mem, offset, dest, size);
}

void
gst_memory_trim (GstMemory * mem, gsize offset, gsize size)
{
  g_return_if_fail (mem != NULL);

  mem->impl->info.trim (mem, offset, size);
}

GstMemory *
gst_memory_sub (GstMemory * mem, gsize offset, gsize size)
{
  g_return_val_if_fail (mem != NULL, NULL);

  return mem->impl->info.sub (mem, offset, size);
}

336 337 338 339 340 341 342 343 344
GstMemory *
gst_memory_new_wrapped (gpointer data, GFreeFunc free_func,
    gsize maxsize, gsize offset, gsize size)
{
  GstMemoryDefault *mem;

  mem = g_slice_new (GstMemoryDefault);
  mem->mem.impl = _default_memory_impl;
  mem->data = data;
345
  mem->free_data = data;
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
  mem->free_func = free_func;
  mem->maxsize = maxsize;
  mem->offset = offset;
  mem->size = size;

  return (GstMemory *) mem;
}

GstMemory *
gst_memory_new_alloc (gsize maxsize, gsize align)
{
  GstMemory *mem;
  gpointer data;
  gsize offset;

  data = g_try_malloc (maxsize + align);
  if (data == NULL)
    return NULL;

  if ((offset = ((guintptr) data & align)))
    offset = align - offset;

  mem = gst_memory_new_wrapped (data, g_free, maxsize + align, offset, maxsize);

  return mem;
}