2002-01-05 01:25:28 +00:00
|
|
|
#include "gstmempool.h"
|
2004-03-15 19:27:17 +00:00
|
|
|
#include <string.h> /* memset */
|
2002-01-05 01:25:28 +00:00
|
|
|
|
|
|
|
#ifdef __SMP__
|
|
|
|
#define POOL_LOCK "lock ; "
|
|
|
|
#else
|
|
|
|
#define POOL_LOCK ""
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define GST_MEM_POOL_AREA(pool) (((GstMemPoolElement*)(pool))->area)
|
|
|
|
#define GST_MEM_POOL_DATA(pool) ((gpointer)(((GstMemPoolElement*)(pool)) + 1))
|
|
|
|
#define GST_MEM_POOL_LINK(mem) ((GstMemPoolElement*)((guint8*)(mem) - sizeof (GstMemPoolElement)))
|
|
|
|
|
2002-10-25 03:19:14 +00:00
|
|
|
#ifdef HAVE_CPU_I386
|
2002-01-05 01:25:28 +00:00
|
|
|
#define USE_ASM
|
2002-09-23 06:03:17 +00:00
|
|
|
#endif
|
2002-01-05 01:25:28 +00:00
|
|
|
|
|
|
|
/*******************************************************
|
|
|
|
* area size
|
|
|
|
* +-----------------------------------------+
|
|
|
|
* pool size
|
|
|
|
* +------------+
|
|
|
|
*
|
|
|
|
* !next!data... !next!data.... !next!data...
|
|
|
|
* ! ^ ! ^ !
|
|
|
|
* +-------------+ +------------+ +---> NULL
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static gboolean
|
2004-03-13 15:27:01 +00:00
|
|
|
populate (GstMemPool * mem_pool)
|
2002-01-05 01:25:28 +00:00
|
|
|
{
|
|
|
|
guint8 *area;
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
if (mem_pool->cleanup)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
area = (guint8 *) g_malloc (mem_pool->area_size);
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
for (i = 0; i < mem_pool->area_size; i += mem_pool->pool_size) {
|
2002-01-05 01:25:28 +00:00
|
|
|
guint8 *areap = area + i;
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
GST_MEM_POOL_AREA (areap) = (GstMemPoolElement *) area;
|
2002-01-05 01:25:28 +00:00
|
|
|
|
|
|
|
if (mem_pool->alloc_func) {
|
|
|
|
mem_pool->alloc_func (mem_pool, GST_MEM_POOL_DATA (areap));
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_mem_pool_free (mem_pool, GST_MEM_POOL_DATA (areap));
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
GstMemPool *
|
|
|
|
gst_mem_pool_new (gchar * name, gint atom_size, gulong area_size, gint type,
|
|
|
|
GstMemPoolAllocFunc alloc_func, GstMemPoolFreeFunc free_func)
|
2002-01-05 01:25:28 +00:00
|
|
|
{
|
|
|
|
GstMemPool *mem_pool;
|
|
|
|
|
|
|
|
g_return_val_if_fail (atom_size > 0, NULL);
|
|
|
|
g_return_val_if_fail (area_size >= atom_size, NULL);
|
|
|
|
|
|
|
|
mem_pool = g_malloc (sizeof (GstMemPool));
|
|
|
|
|
|
|
|
mem_pool->pool_size = atom_size + sizeof (GstMemPoolElement);
|
2004-03-13 15:27:01 +00:00
|
|
|
area_size = (area_size / atom_size) * mem_pool->pool_size;
|
2002-01-05 01:25:28 +00:00
|
|
|
|
|
|
|
mem_pool->name = g_strdup (name);
|
|
|
|
mem_pool->free = NULL;
|
|
|
|
mem_pool->cnt = 0;
|
|
|
|
mem_pool->atom_size = atom_size;
|
|
|
|
mem_pool->area_size = area_size;
|
|
|
|
mem_pool->cleanup = FALSE;
|
|
|
|
mem_pool->alloc_func = alloc_func;
|
|
|
|
mem_pool->free_func = free_func;
|
|
|
|
mem_pool->chunk_lock = g_mutex_new ();
|
|
|
|
|
|
|
|
populate (mem_pool);
|
|
|
|
|
|
|
|
return mem_pool;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
free_area (gpointer key, gpointer value, gpointer user_data)
|
|
|
|
{
|
|
|
|
g_print ("free %p\n", key);
|
|
|
|
g_free (key);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_mem_pool_destroy (GstMemPool * mem_pool)
|
2002-01-05 01:25:28 +00:00
|
|
|
{
|
|
|
|
GHashTable *elements = g_hash_table_new (NULL, NULL);
|
|
|
|
gpointer data;
|
|
|
|
|
|
|
|
mem_pool->cleanup = TRUE;
|
|
|
|
|
|
|
|
data = gst_mem_pool_alloc (mem_pool);
|
|
|
|
while (data) {
|
|
|
|
GstMemPoolElement *elem = GST_MEM_POOL_LINK (data);
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
g_hash_table_insert (elements, GST_MEM_POOL_AREA (elem), NULL);
|
|
|
|
|
2002-01-05 01:25:28 +00:00
|
|
|
data = gst_mem_pool_alloc (mem_pool);
|
2004-03-13 15:27:01 +00:00
|
|
|
}
|
2002-01-05 01:25:28 +00:00
|
|
|
g_hash_table_foreach_remove (elements, free_area, NULL);
|
|
|
|
|
|
|
|
g_hash_table_destroy (elements);
|
|
|
|
g_free (mem_pool->name);
|
|
|
|
g_free (mem_pool);
|
|
|
|
}
|
|
|
|
|
|
|
|
gpointer
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_mem_pool_alloc (GstMemPool * mem_pool)
|
2002-01-05 01:25:28 +00:00
|
|
|
{
|
2003-12-12 15:00:39 +00:00
|
|
|
volatile GstMemPoolElement *pool = NULL;
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2002-01-05 01:25:28 +00:00
|
|
|
g_return_val_if_fail (mem_pool != NULL, NULL);
|
|
|
|
|
|
|
|
again:
|
2002-10-25 19:38:15 +00:00
|
|
|
#if defined(USE_ASM) && defined(HAVE_CPU_I386)
|
2004-03-13 15:27:01 +00:00
|
|
|
__asm__ __volatile__ (" testl %%eax, %%eax \n\t" " jz 20f \n" "10: \t" " movl (%%eax), %%ebx \n\t" " movl %%edx, %%ecx \n\t" " incl %%ecx \n\t" POOL_LOCK "cmpxchg8b %1 \n\t" " jz 20f \n\t" " testl %%eax, %%eax \n\t" " jnz 10b \n" "20:\t":"=a" (pool)
|
|
|
|
: "m" (*mem_pool), "a" (mem_pool->free), "d" (mem_pool->cnt)
|
|
|
|
: "ecx", "ebx");
|
2002-01-05 01:25:28 +00:00
|
|
|
#else
|
|
|
|
g_mutex_lock (mem_pool->chunk_lock);
|
|
|
|
if (mem_pool->free) {
|
|
|
|
pool = mem_pool->free;
|
|
|
|
mem_pool->free = pool->link;
|
|
|
|
}
|
|
|
|
g_mutex_unlock (mem_pool->chunk_lock);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!pool) {
|
2002-03-19 04:10:13 +00:00
|
|
|
/*g_print ("extending\n"); */
|
2002-01-05 01:25:28 +00:00
|
|
|
if (populate (mem_pool))
|
|
|
|
goto again;
|
2004-03-13 15:27:01 +00:00
|
|
|
else
|
2002-01-05 01:25:28 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return GST_MEM_POOL_DATA (pool);
|
|
|
|
}
|
|
|
|
|
|
|
|
gpointer
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_mem_pool_alloc0 (GstMemPool * mem_pool)
|
2002-01-05 01:25:28 +00:00
|
|
|
{
|
|
|
|
gpointer mem = gst_mem_pool_alloc (mem_pool);
|
|
|
|
|
|
|
|
if (mem)
|
|
|
|
memset (mem, 0, mem_pool->atom_size);
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2002-01-05 01:25:28 +00:00
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_mem_pool_free (GstMemPool * mem_pool, gpointer mem)
|
2002-01-05 01:25:28 +00:00
|
|
|
{
|
|
|
|
GstMemPoolElement *pool;
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2002-01-05 01:25:28 +00:00
|
|
|
g_return_if_fail (mem_pool != NULL);
|
|
|
|
g_return_if_fail (mem != NULL);
|
|
|
|
|
|
|
|
pool = GST_MEM_POOL_LINK (mem);
|
|
|
|
|
2002-10-25 19:38:15 +00:00
|
|
|
#if defined(USE_ASM) && defined(HAVE_CPU_I386)
|
2004-03-13 15:27:01 +00:00
|
|
|
__asm__ __volatile__ ("1: \t"
|
|
|
|
" movl %2, (%1) \n"
|
|
|
|
POOL_LOCK "cmpxchg %1, %0 \n\t"
|
2004-03-15 19:27:17 +00:00
|
|
|
" jnz 1b \n\t"::
|
|
|
|
"m" (*mem_pool), "r" (pool), "a" (mem_pool->free));
|
2002-01-05 01:25:28 +00:00
|
|
|
#else
|
|
|
|
g_mutex_lock (mem_pool->chunk_lock);
|
2003-12-12 15:00:39 +00:00
|
|
|
pool->link = (GstMemPoolElement *) mem_pool->free;
|
2002-01-05 01:25:28 +00:00
|
|
|
mem_pool->free = pool;
|
|
|
|
g_mutex_unlock (mem_pool->chunk_lock);
|
|
|
|
#endif
|
|
|
|
}
|