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;
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>
*/
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))
{
{
block_t *b;
+ vlc_testcancel( );
+
vlc_mutex_lock( &p_fifo->lock );
mutex_cleanup_push( &p_fifo->lock );
{
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;