]> git.sesse.net Git - vlc/blobdiff - src/misc/block.c
block_heap_Alloc(): create a block from an existing heap allocation
[vlc] / src / misc / block.c
index 09832722c0f0f5d17024347b974bdeb9a208da14..b2cf2692e022d70b17e202262b4ff0494a9dabfe 100644 (file)
@@ -57,9 +57,11 @@ static void BlockNoRelease( block_t *b )
 void block_Init( block_t *restrict b, void *buf, size_t size )
 {
     /* Fill all fields to their default */
-    b->p_next = b->p_prev = NULL;
+    b->p_next = NULL;
     b->i_flags = 0;
-    b->i_pts = b->i_dts = b->i_length = 0;
+    b->i_pts =
+    b->i_dts = VLC_TS_INVALID;
+    b->i_length = 0;
     b->i_rate = 0;
     b->p_buffer = buf;
     b->i_buffer = size;
@@ -172,26 +174,69 @@ block_t *block_Realloc( block_t *p_block, ssize_t i_prebody, size_t i_body )
         return p_rea;
     }
 
-    /* We have a very large reserved footer now? Release some of it. */
-    if ((p_sys->p_allocated_buffer + p_sys->i_allocated_buffer) -
-        (p_block->p_buffer + p_block->i_buffer) > BLOCK_WASTE_SIZE)
+    /* We have a very large reserved footer now? Release some of it.
+     * XXX it may not keep the algniment of p_buffer */
+    if( (p_sys->p_allocated_buffer + p_sys->i_allocated_buffer) -
+        (p_block->p_buffer + p_block->i_buffer) > BLOCK_WASTE_SIZE )
     {
-        const size_t news = p_block->i_buffer + 2 * BLOCK_PADDING_SIZE + 16;
-        block_sys_t *newb = realloc (p_sys, sizeof (*p_sys) + news);
+        const ptrdiff_t i_prebody = p_block->p_buffer - p_sys->p_allocated_buffer;
+        const size_t i_new = i_prebody + p_block->i_buffer + 1 * BLOCK_PADDING_SIZE;
+        block_sys_t *p_new = realloc( p_sys, sizeof (*p_sys) + i_new );
 
-        if (newb != NULL)
+        if( p_new != NULL )
         {
-            p_sys = newb;
-            p_sys->i_allocated_buffer = news;
+            p_sys = p_new;
+            p_sys->i_allocated_buffer = i_new;
             p_block = &p_sys->self;
-            p_block->p_buffer = p_sys->p_allocated_buffer + BLOCK_PADDING_SIZE
-                + BLOCK_ALIGN
-                - ((uintptr_t)p_sys->p_allocated_buffer % BLOCK_ALIGN);
+            p_block->p_buffer = &p_sys->p_allocated_buffer[i_prebody];
         }
     }
     return p_block;
 }
 
+
+typedef struct
+{
+    block_t  self;
+    void    *mem;
+} block_heap_t;
+
+static void block_heap_Release (block_t *self)
+{
+    block_heap_t *block = (block_heap_t *)self;
+
+    free (block->mem);
+    free (block);
+}
+
+/**
+ * Creates a block from a heap allocation.
+ * This is provided by LibVLC so that manually heap-allocated blocks can safely
+ * be deallocated even after the origin plugin has been unloaded from memory.
+ *
+ * When block_Release() is called, VLC will free() the specified pointer.
+ *
+ * @param ptr base address of the heap allocation (will be free()'d)
+ * @param addr base address of the useful buffer data
+ * @param length bytes length of the useful buffer datan
+ * @return NULL in case of error (ptr free()'d in that case), or a valid
+ * block_t pointer.
+ */
+block_t *block_heap_Alloc (void *ptr, void *addr, size_t length)
+{
+    block_heap_t *block = malloc (sizeof (*block));
+    if (block == NULL)
+    {
+        free (addr);
+        return NULL;
+    }
+
+    block_Init (&block->self, (uint8_t *)addr, length);
+    block->self.pf_release = block_heap_Release;
+    block->mem = ptr;
+    return &block->self;
+}
+
 #ifdef HAVE_MMAP
 # include <sys/mman.h>
 
@@ -431,6 +476,8 @@ void block_FifoEmpty( block_fifo_t *p_fifo )
  */
 void block_FifoPace (block_fifo_t *fifo, size_t max_depth, size_t max_size)
 {
+    vlc_testcancel ();
+
     vlc_mutex_lock (&fifo->lock);
     while ((fifo->i_depth > max_depth) || (fifo->i_size > max_size))
     {
@@ -483,6 +530,8 @@ block_t *block_FifoGet( block_fifo_t *p_fifo )
 {
     block_t *b;
 
+    vlc_testcancel( );
+
     vlc_mutex_lock( &p_fifo->lock );
     mutex_cleanup_push( &p_fifo->lock );
 
@@ -523,10 +572,12 @@ block_t *block_FifoShow( block_fifo_t *p_fifo )
 {
     block_t *b;
 
+    vlc_testcancel( );
+
     vlc_mutex_lock( &p_fifo->lock );
     mutex_cleanup_push( &p_fifo->lock );
 
-    if( p_fifo->p_first == NULL )
+    while( p_fifo->p_first == NULL )
         vlc_cond_wait( &p_fifo->wait, &p_fifo->lock );
 
     b = p_fifo->p_first;