2004-03-15 19:27:17 +00:00
|
|
|
#include <string.h> /* memset */
|
|
|
|
#include <stdlib.h> /* memset */
|
2003-02-10 20:08:59 +00:00
|
|
|
#include "gstmemchunk.h"
|
2002-01-05 01:25:28 +00:00
|
|
|
|
|
|
|
#ifdef __SMP__
|
|
|
|
#define CHUNK_LOCK "lock ; "
|
|
|
|
#else
|
|
|
|
#define CHUNK_LOCK ""
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#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)))
|
|
|
|
|
|
|
|
/*******************************************************
|
|
|
|
* area size
|
|
|
|
* +-----------------------------------------+
|
|
|
|
* chunk size
|
|
|
|
* +------------+
|
|
|
|
*
|
|
|
|
* !next!data... !next!data.... !next!data...
|
|
|
|
* ! ^ ! ^ !
|
|
|
|
* +-------------+ +------------+ +---> NULL
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static gboolean
|
2004-03-13 15:27:01 +00:00
|
|
|
populate (GstMemChunk * mem_chunk)
|
2002-01-05 01:25:28 +00:00
|
|
|
{
|
|
|
|
guint8 *area;
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
if (mem_chunk->cleanup)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
area = (guint8 *) g_malloc (mem_chunk->area_size);
|
|
|
|
g_print ("alloc %p\n", area);
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
for (i = 0; i < mem_chunk->area_size; i += mem_chunk->chunk_size) {
|
|
|
|
GST_MEM_CHUNK_AREA (area + i) = (GstMemChunkElement *) area;
|
2002-01-05 01:25:28 +00:00
|
|
|
gst_mem_chunk_free (mem_chunk, GST_MEM_CHUNK_DATA (area + i));
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
GstMemChunk *
|
|
|
|
gst_mem_chunk_new (gchar * name, gint atom_size, gulong area_size, gint type)
|
2002-01-05 01:25:28 +00:00
|
|
|
{
|
|
|
|
GstMemChunk *mem_chunk;
|
|
|
|
|
|
|
|
g_return_val_if_fail (atom_size > 0, NULL);
|
|
|
|
g_return_val_if_fail (area_size >= atom_size, NULL);
|
|
|
|
|
|
|
|
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-01-05 01:25:28 +00:00
|
|
|
|
|
|
|
mem_chunk->name = g_strdup (name);
|
|
|
|
mem_chunk->free = NULL;
|
|
|
|
mem_chunk->cnt = 0;
|
|
|
|
mem_chunk->atom_size = atom_size;
|
|
|
|
mem_chunk->area_size = area_size;
|
|
|
|
mem_chunk->cleanup = FALSE;
|
|
|
|
|
|
|
|
populate (mem_chunk);
|
|
|
|
|
|
|
|
return mem_chunk;
|
|
|
|
}
|
|
|
|
|
|
|
|
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_chunk_destroy (GstMemChunk * mem_chunk)
|
2002-01-05 01:25:28 +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-01-05 01:25:28 +00:00
|
|
|
data = gst_mem_chunk_alloc (mem_chunk);
|
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_chunk->name);
|
|
|
|
g_free (mem_chunk);
|
|
|
|
}
|
|
|
|
|
|
|
|
gpointer
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_mem_chunk_alloc (GstMemChunk * mem_chunk)
|
2002-01-05 01:25:28 +00:00
|
|
|
{
|
|
|
|
GstMemChunkElement *chunk = NULL;
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2002-01-05 01:25:28 +00:00
|
|
|
g_return_val_if_fail (mem_chunk != NULL, NULL);
|
|
|
|
|
|
|
|
again:
|
2002-11-17 00:12:59 +00:00
|
|
|
#ifdef HAVE_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" CHUNK_LOCK "cmpxchg8b %1 \n\t" " jz 20f \n\t" " testl %%eax, %%eax \n\t" " jnz 10b \n" "20:\t":"=a" (chunk)
|
|
|
|
: "m" (*mem_chunk), "a" (mem_chunk->free), "d" (mem_chunk->cnt)
|
|
|
|
: "ecx", "ebx");
|
2002-11-17 00:12:59 +00:00
|
|
|
#else
|
2004-03-13 15:27:01 +00:00
|
|
|
fprintf (stderr, "This only compiles correctly on i386. Sorry\n");
|
|
|
|
abort ();
|
2002-11-17 00:12:59 +00:00
|
|
|
#endif
|
2002-01-05 01:25:28 +00:00
|
|
|
|
|
|
|
if (!chunk) {
|
2002-03-19 04:10:13 +00:00
|
|
|
/*g_print ("extending\n"); */
|
2002-01-05 01:25:28 +00:00
|
|
|
if (populate (mem_chunk))
|
|
|
|
goto again;
|
2004-03-13 15:27:01 +00:00
|
|
|
else
|
2002-01-05 01:25:28 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return GST_MEM_CHUNK_DATA (chunk);
|
|
|
|
}
|
|
|
|
|
|
|
|
gpointer
|
2004-03-13 15:27:01 +00:00
|
|
|
gst_mem_chunk_alloc0 (GstMemChunk * mem_chunk)
|
2002-01-05 01:25:28 +00:00
|
|
|
{
|
|
|
|
gpointer mem = gst_mem_chunk_alloc (mem_chunk);
|
|
|
|
|
|
|
|
if (mem)
|
|
|
|
memset (mem, 0, mem_chunk->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_chunk_free (GstMemChunk * mem_chunk, gpointer mem)
|
2002-01-05 01:25:28 +00:00
|
|
|
{
|
|
|
|
GstMemChunkElement *chunk;
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2002-01-05 01:25:28 +00:00
|
|
|
g_return_if_fail (mem_chunk != NULL);
|
|
|
|
g_return_if_fail (mem != NULL);
|
|
|
|
|
|
|
|
chunk = GST_MEM_CHUNK_LINK (mem);
|
|
|
|
|
2002-11-17 00:12:59 +00:00
|
|
|
#ifdef HAVE_I386
|
2004-03-13 15:27:01 +00:00
|
|
|
__asm__ __volatile__ ("1: \t"
|
|
|
|
" movl %2, (%1) \n"
|
|
|
|
CHUNK_LOCK "cmpxchg %1, %0 \n\t"
|
2004-03-15 19:27:17 +00:00
|
|
|
" jnz 1b \n\t"::
|
|
|
|
"m" (*mem_chunk), "r" (chunk), "a" (mem_chunk->free));
|
2002-11-17 00:12:59 +00:00
|
|
|
#else
|
2004-03-13 15:27:01 +00:00
|
|
|
fprintf (stderr, "This only compiles correctly on i386. Sorry\n");
|
|
|
|
abort ();
|
2002-11-17 00:12:59 +00:00
|
|
|
#endif
|
2002-01-05 01:25:28 +00:00
|
|
|
}
|