2002-07-08 19:12:38 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
|
2005-03-07 18:27:42 +00:00
|
|
|
* <2005> Wim Taymans <wim@fluendo.com>
|
|
|
|
*
|
|
|
|
* gstmemchunk.c: implementation of lockfree allocation of fixed
|
|
|
|
* size memory chunks.
|
2002-07-08 19:12:38 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2005-09-02 16:17:23 +00:00
|
|
|
/**
|
2005-09-06 22:03:01 +00:00
|
|
|
* SECTION:gstmemchunk
|
2005-09-02 16:17:23 +00:00
|
|
|
* @short_description: Atomic chunk allocator
|
|
|
|
* @see_also: #GstBuffer, #GstEvent, #GstData
|
|
|
|
*
|
|
|
|
* GstMemChunk is an atomic chunk allocator. It uses atomic operations to
|
|
|
|
* allocate fixed size memory regions and is therefore thread safe without the
|
|
|
|
* overhead of mutexes or other heavyweight locking mechanisms.
|
|
|
|
*
|
|
|
|
* The GstMemChunk is used to allocate critical resources for #GstBuffer and
|
|
|
|
* #GstEvent.
|
|
|
|
*/
|
2004-04-05 21:42:07 +00:00
|
|
|
#include "gst_private.h"
|
2002-07-08 19:12:38 +00:00
|
|
|
|
2004-03-15 19:27:17 +00:00
|
|
|
#include <string.h> /* memset */
|
2002-07-08 19:12:38 +00:00
|
|
|
|
2002-12-29 19:19:45 +00:00
|
|
|
#include "gstmemchunk.h"
|
|
|
|
#include "gsttrashstack.h"
|
2004-04-05 21:42:07 +00:00
|
|
|
#ifdef HAVE_VALGRIND
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <valgrind/valgrind.h>
|
|
|
|
#endif
|
|
|
|
|
2002-07-08 19:12:38 +00:00
|
|
|
#define GST_MEM_CHUNK_AREA(chunk) (((GstMemChunkElement*)(chunk))->area)
|
|
|
|
#define GST_MEM_CHUNK_DATA(chunk) ((gpointer)(((GstMemChunkElement*)(chunk)) + 1))
|
|
|
|
#define GST_MEM_CHUNK_LINK(mem) ((GstMemChunkElement*)((guint8*)(mem) - sizeof (GstMemChunkElement)))
|
|
|
|
|
2002-12-29 19:19:45 +00:00
|
|
|
typedef struct _GstMemChunkElement GstMemChunkElement;
|
|
|
|
|
|
|
|
struct _GstMemChunkElement
|
|
|
|
{
|
2004-03-15 19:27:17 +00:00
|
|
|
GstTrashStackElement elem; /* make sure we can safely push it on the trashstack */
|
|
|
|
gpointer area; /* pointer to data areas */
|
2002-12-29 19:19:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct _GstMemChunk
|
|
|
|
{
|
2004-03-13 15:27:01 +00:00
|
|
|
GstTrashStack stack;
|
2002-12-29 19:19:45 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
gchar *name;
|
|
|
|
gulong area_size;
|
|
|
|
gulong chunk_size;
|
|
|
|
gulong atom_size;
|
|
|
|
gboolean cleanup;
|
2002-12-29 19:19:45 +00:00
|
|
|
};
|
|
|
|
|
2002-07-08 19:12:38 +00:00
|
|
|
/*******************************************************
|
|
|
|
* area size
|
2002-12-29 19:19:45 +00:00
|
|
|
* +-------------------------------------------------------+
|
2002-07-08 19:12:38 +00:00
|
|
|
* chunk size
|
2002-12-29 19:19:45 +00:00
|
|
|
* +-----------------+
|
2002-07-08 19:12:38 +00:00
|
|
|
*
|
2002-12-29 19:19:45 +00:00
|
|
|
* !next!area|data... !next!area!data.... !next!area!data...
|
|
|
|
* ! ^ ! ^ !
|
|
|
|
* +------------------+ +-----------------+ +--------> NULL
|
2002-07-08 19:12:38 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
static gboolean
|
2004-03-13 15:27:01 +00:00
|
|
|
populate (GstMemChunk * mem_chunk)
|
2002-07-08 19:12:38 +00:00
|
|
|
{
|
|
|
|
guint8 *area;
|
2004-02-07 15:51:39 +00:00
|
|
|
gint i;
|
2002-07-08 19:12:38 +00:00
|
|
|
|
|
|
|
if (mem_chunk->cleanup)
|
|
|
|
return FALSE;
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2004-04-05 21:42:07 +00:00
|
|
|
/* FIXME: if we don't do this here and use g_malloc, valgrind crashes */
|
|
|
|
#if HAVE_VALGRIND
|
|
|
|
if (__gst_in_valgrind ()) {
|
|
|
|
/* copied from valgrind example */
|
|
|
|
area =
|
|
|
|
(guint8 *) mmap (0, mem_chunk->area_size,
|
|
|
|
PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANON, -1, 0);
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
area = g_malloc0 (mem_chunk->area_size);
|
|
|
|
}
|
2002-07-08 19:12:38 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
for (i = 0; i < mem_chunk->area_size; i += mem_chunk->chunk_size) {
|
2002-12-29 19:19:45 +00:00
|
|
|
GST_MEM_CHUNK_AREA (area + i) = area;
|
|
|
|
gst_trash_stack_push (&mem_chunk->stack, area + i);
|
2002-07-08 19:12:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2002-12-14 16:20:41 +00:00
|
|
|
/**
|
|
|
|
* gst_mem_chunk_new:
|
|
|
|
* @name: the name of the chunk
|
|
|
|
* @atom_size: the size of the allocated atoms
|
|
|
|
* @area_size: the initial size of the memory area
|
|
|
|
* @type: the allocation strategy to use
|
|
|
|
*
|
|
|
|
* Creates a new memchunk that will allocate atom_sized memchunks.
|
|
|
|
* The initial area is set to area_size and will grow automatically
|
|
|
|
* when it is too small (with a small overhead when that happens)
|
|
|
|
*
|
|
|
|
* Returns: a new #GstMemChunk
|
2005-03-07 18:27:42 +00:00
|
|
|
*
|
|
|
|
* MT safe.
|
2002-12-14 16:20:41 +00:00
|
|
|
*/
|
2004-03-13 15:27:01 +00:00
|
|
|
GstMemChunk *
|
|
|
|
gst_mem_chunk_new (gchar * name, gint atom_size, gulong area_size, gint type)
|
2002-07-08 19:12:38 +00:00
|
|
|
{
|
|
|
|
GstMemChunk *mem_chunk;
|
|
|
|
|
|
|
|
g_return_val_if_fail (atom_size > 0, NULL);
|
2004-02-07 15:51:39 +00:00
|
|
|
g_return_val_if_fail (area_size >= atom_size, NULL);
|
2002-07-08 19:12:38 +00:00
|
|
|
|
|
|
|
mem_chunk = g_malloc (sizeof (GstMemChunk));
|
|
|
|
|
|
|
|
mem_chunk->chunk_size = atom_size + sizeof (GstMemChunkElement);
|
2004-03-13 15:27:01 +00:00
|
|
|
area_size = (area_size / atom_size) * mem_chunk->chunk_size;
|
2002-07-08 19:12:38 +00:00
|
|
|
|
|
|
|
mem_chunk->name = g_strdup (name);
|
|
|
|
mem_chunk->atom_size = atom_size;
|
|
|
|
mem_chunk->area_size = area_size;
|
|
|
|
mem_chunk->cleanup = FALSE;
|
2002-12-29 19:19:45 +00:00
|
|
|
gst_trash_stack_init (&mem_chunk->stack);
|
2002-07-08 19:12:38 +00:00
|
|
|
|
|
|
|
populate (mem_chunk);
|
|
|
|
|
|
|
|
return mem_chunk;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
free_area (gpointer key, gpointer value, gpointer user_data)
|
|
|
|
{
|
2004-04-05 21:42:07 +00:00
|
|
|
#if HAVE_VALGRIND
|
|
|
|
GstMemChunk *chunk = (GstMemChunk *) user_data;
|
|
|
|
|
|
|
|
if (__gst_in_valgrind ()) {
|
|
|
|
/* copied from valgrind example */
|
|
|
|
munmap (key, chunk->area_size);
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
g_free (key);
|
|
|
|
}
|
2002-07-08 19:12:38 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2002-12-14 16:20:41 +00:00
|
|
|
/**
|
|
|
|
* gst_mem_chunk_destroy:
|
|
|
|
* @mem_chunk: the GstMemChunk to destroy
|
|
|
|
*
|
2005-03-07 18:27:42 +00:00
|
|
|
* Free the memory allocated by the memchunk. This function
|
|
|
|
* is not Threadsafe as it does not wait for all outstanding
|
|
|
|
* allocations to be freed.
|
2002-12-14 16:20:41 +00:00
|
|
|
*/
|
2002-07-08 19:12:38 +00:00
|
|
|
void
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_mem_chunk_destroy (GstMemChunk * mem_chunk)
|
2002-07-08 19:12:38 +00:00
|
|
|
{
|
|
|
|
GHashTable *elements = g_hash_table_new (NULL, NULL);
|
|
|
|
gpointer data;
|
|
|
|
|
|
|
|
mem_chunk->cleanup = TRUE;
|
|
|
|
|
|
|
|
data = gst_mem_chunk_alloc (mem_chunk);
|
|
|
|
while (data) {
|
|
|
|
GstMemChunkElement *elem = GST_MEM_CHUNK_LINK (data);
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
g_hash_table_insert (elements, GST_MEM_CHUNK_AREA (elem), NULL);
|
|
|
|
|
2002-07-08 19:12:38 +00:00
|
|
|
data = gst_mem_chunk_alloc (mem_chunk);
|
2004-03-13 15:27:01 +00:00
|
|
|
}
|
2004-04-05 21:42:07 +00:00
|
|
|
g_hash_table_foreach_remove (elements, free_area, mem_chunk);
|
2002-07-08 19:12:38 +00:00
|
|
|
|
|
|
|
g_hash_table_destroy (elements);
|
|
|
|
g_free (mem_chunk->name);
|
|
|
|
g_free (mem_chunk);
|
|
|
|
}
|
|
|
|
|
2002-12-14 16:20:41 +00:00
|
|
|
/**
|
|
|
|
* gst_mem_chunk_alloc:
|
|
|
|
* @mem_chunk: the mem chunk to use
|
|
|
|
*
|
|
|
|
* Allocate a new memory region from the chunk. The size
|
|
|
|
* of the allocated memory was specified when the memchunk
|
|
|
|
* was created.
|
|
|
|
*
|
|
|
|
* Returns: a pointer to the allocated memory region.
|
2005-03-07 18:27:42 +00:00
|
|
|
*
|
|
|
|
* MT safe.
|
2002-12-14 16:20:41 +00:00
|
|
|
*/
|
2002-07-08 19:12:38 +00:00
|
|
|
gpointer
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_mem_chunk_alloc (GstMemChunk * mem_chunk)
|
2002-07-08 19:12:38 +00:00
|
|
|
{
|
2002-12-29 19:19:45 +00:00
|
|
|
GstMemChunkElement *chunk;
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2002-07-08 19:12:38 +00:00
|
|
|
g_return_val_if_fail (mem_chunk != NULL, NULL);
|
|
|
|
|
|
|
|
again:
|
2002-12-29 19:19:45 +00:00
|
|
|
chunk = gst_trash_stack_pop (&mem_chunk->stack);
|
|
|
|
/* chunk is empty, try to refill */
|
2005-03-07 18:27:42 +00:00
|
|
|
if (G_UNLIKELY (!chunk)) {
|
|
|
|
if (G_LIKELY (populate (mem_chunk))) {
|
2002-07-08 19:12:38 +00:00
|
|
|
goto again;
|
2005-03-07 18:27:42 +00:00
|
|
|
} else {
|
|
|
|
/* this happens when we are in cleanup mode and we
|
|
|
|
* allocate all remaining chunks for cleanup */
|
2002-07-08 19:12:38 +00:00
|
|
|
return NULL;
|
2005-03-07 18:27:42 +00:00
|
|
|
}
|
2002-07-08 19:12:38 +00:00
|
|
|
}
|
2004-04-05 21:42:07 +00:00
|
|
|
#ifdef HAVE_VALGRIND
|
2005-03-07 18:27:42 +00:00
|
|
|
if (G_UNLIKELY (__gst_in_valgrind ())) {
|
|
|
|
VALGRIND_MALLOCLIKE_BLOCK (GST_MEM_CHUNK_DATA (chunk), mem_chunk->atom_size,
|
|
|
|
0, 0);
|
|
|
|
}
|
2004-04-05 21:42:07 +00:00
|
|
|
#endif
|
2002-07-08 19:12:38 +00:00
|
|
|
return GST_MEM_CHUNK_DATA (chunk);
|
|
|
|
}
|
|
|
|
|
2002-12-14 16:20:41 +00:00
|
|
|
/**
|
|
|
|
* gst_mem_chunk_alloc0:
|
|
|
|
* @mem_chunk: the mem chunk to use
|
|
|
|
*
|
|
|
|
* Allocate a new memory region from the chunk. The size
|
|
|
|
* of the allocated memory was specified when the memchunk
|
|
|
|
* was created. The memory will be set to all zeroes.
|
|
|
|
*
|
|
|
|
* Returns: a pointer to the allocated memory region.
|
2005-03-07 18:27:42 +00:00
|
|
|
*
|
|
|
|
* MT safe.
|
2002-12-14 16:20:41 +00:00
|
|
|
*/
|
2002-07-08 19:12:38 +00:00
|
|
|
gpointer
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_mem_chunk_alloc0 (GstMemChunk * mem_chunk)
|
2002-07-08 19:12:38 +00:00
|
|
|
{
|
|
|
|
gpointer mem = gst_mem_chunk_alloc (mem_chunk);
|
|
|
|
|
2005-03-07 18:27:42 +00:00
|
|
|
if (G_LIKELY (mem))
|
2002-07-08 19:12:38 +00:00
|
|
|
memset (mem, 0, mem_chunk->atom_size);
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2002-07-08 19:12:38 +00:00
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
|
2002-12-14 16:20:41 +00:00
|
|
|
/**
|
|
|
|
* gst_mem_chunk_free:
|
|
|
|
* @mem_chunk: the mem chunk to use
|
|
|
|
* @mem: the memory region to hand back to the chunk
|
|
|
|
*
|
|
|
|
* Free the memeory region allocated from the chunk.
|
2005-03-07 18:27:42 +00:00
|
|
|
*
|
|
|
|
* MT safe.
|
2002-12-14 16:20:41 +00:00
|
|
|
*/
|
2002-07-08 19:12:38 +00:00
|
|
|
void
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_mem_chunk_free (GstMemChunk * mem_chunk, gpointer mem)
|
2002-07-08 19:12:38 +00:00
|
|
|
{
|
|
|
|
GstMemChunkElement *chunk;
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2002-07-08 19:12:38 +00:00
|
|
|
g_return_if_fail (mem_chunk != NULL);
|
|
|
|
g_return_if_fail (mem != NULL);
|
|
|
|
|
|
|
|
chunk = GST_MEM_CHUNK_LINK (mem);
|
|
|
|
|
2004-04-05 21:42:07 +00:00
|
|
|
#ifdef HAVE_VALGRIND
|
2005-03-07 18:27:42 +00:00
|
|
|
if (G_UNLIKELY (__gst_in_valgrind ())) {
|
|
|
|
VALGRIND_FREELIKE_BLOCK (mem, 0);
|
|
|
|
}
|
2004-04-05 21:42:07 +00:00
|
|
|
#endif
|
2002-12-29 19:19:45 +00:00
|
|
|
gst_trash_stack_push (&mem_chunk->stack, chunk);
|
2002-07-08 19:12:38 +00:00
|
|
|
}
|