38
uint8_t *p_allocated_buffer;
39
int i_allocated_buffer;
42
size_t i_allocated_buffer;
43
uint8_t p_allocated_buffer[0];
47
static void BlockNoRelease( block_t *b )
49
fprintf( stderr, "block %p has no release callback! This is a bug!\n", b );
54
void block_Init( block_t *restrict b, void *buf, size_t size )
56
/* Fill all fields to their default */
57
b->p_next = b->p_prev = NULL;
59
b->i_pts = b->i_dts = b->i_length = 0;
64
b->pf_release = BlockNoRelease;
68
static void BlockRelease( block_t *p_block )
73
/* Memory alignment */
74
#define BLOCK_ALIGN 16
75
/* Initial size of reserved header and footer */
42
76
#define BLOCK_PADDING_SIZE 32
43
static void BlockRelease( block_t * );
77
/* Maximum size of reserved footer before we release with realloc() */
78
#define BLOCK_WASTE_SIZE 2048
45
block_t *__block_New( vlc_object_t *p_obj, int i_size )
80
block_t *block_Alloc( size_t i_size )
47
82
/* We do only one malloc
48
* TODO bench if doing 2 malloc but keeping a pool of buffer is better
83
* TODO: bench if doing 2 malloc but keeping a pool of buffer is better
49
85
* 16 -> align on 16
50
86
* 2 * BLOCK_PADDING_SIZE -> pre + post padding
53
const int i_alloc = i_size + 2 * BLOCK_PADDING_SIZE + 16;
55
malloc( sizeof( block_t ) + sizeof( block_sys_t ) + i_alloc );
88
const size_t i_alloc = i_size + 2 * BLOCK_PADDING_SIZE + BLOCK_ALIGN;
89
block_sys_t *p_sys = malloc( sizeof( *p_sys ) + i_alloc );
57
if( p_block == NULL ) return NULL;
59
94
/* Fill opaque data */
60
p_sys = (block_sys_t*)( (uint8_t*)p_block + sizeof( block_t ) );
61
95
p_sys->i_allocated_buffer = i_alloc;
62
p_sys->p_allocated_buffer = (uint8_t*)p_block + sizeof( block_t ) +
63
sizeof( block_sys_t );
66
p_block->p_next = NULL;
67
p_block->p_prev = NULL;
71
p_block->i_length = 0;
73
p_block->i_buffer = i_size;
75
&p_sys->p_allocated_buffer[BLOCK_PADDING_SIZE +
76
16 - ((uintptr_t)p_sys->p_allocated_buffer % 16 )];
77
p_block->pf_release = BlockRelease;
79
/* Is ok, as no comunication between p_vlc */
80
p_block->p_manager = VLC_OBJECT( p_obj->p_vlc );
81
p_block->p_sys = p_sys;
97
block_Init( &p_sys->self, p_sys->p_allocated_buffer + BLOCK_PADDING_SIZE
99
- ((uintptr_t)p_sys->p_allocated_buffer % BLOCK_ALIGN),
101
p_sys->self.pf_release = BlockRelease;
86
block_t *block_Realloc( block_t *p_block, int i_prebody, int i_body )
106
block_t *block_Realloc( block_t *p_block, ssize_t i_prebody, size_t i_body )
108
block_sys_t *p_sys = (block_sys_t *)p_block;
109
ssize_t i_buffer_size = i_prebody + i_body;
111
if( i_buffer_size <= 0 )
113
block_Release( p_block );
90
117
if( p_block->pf_release != BlockRelease )
92
119
/* Special case when pf_release if overloaded
93
* TODO if used one day, them implement it in a smarter way */
120
* TODO if used one day, then implement it in a smarter way */
94
121
block_t *p_dup = block_Duplicate( p_block );
95
122
block_Release( p_block );
100
i_buffer_size = i_prebody + i_body;
102
if( i_body < 0 || i_buffer_size <= 0 )
104
block_Release( p_block );
108
if( p_block->p_buffer - i_prebody > p_block->p_sys->p_allocated_buffer &&
109
p_block->p_buffer - i_prebody < p_block->p_sys->p_allocated_buffer +
110
p_block->p_sys->i_allocated_buffer )
129
/* Adjust reserved header if there is enough room */
130
if( p_block->p_buffer - i_prebody > p_sys->p_allocated_buffer &&
131
p_block->p_buffer - i_prebody < p_sys->p_allocated_buffer +
132
p_sys->i_allocated_buffer )
112
134
p_block->p_buffer -= i_prebody;
113
135
p_block->i_buffer += i_prebody;
116
if( p_block->p_buffer + i_body < p_block->p_sys->p_allocated_buffer +
117
p_block->p_sys->i_allocated_buffer )
139
/* Adjust payload size if there is enough room */
140
if( p_block->p_buffer + i_body < p_sys->p_allocated_buffer +
141
p_sys->i_allocated_buffer )
119
143
p_block->i_buffer = i_buffer_size;
147
/* Not enough room, reallocate the buffer */
123
148
if( i_body > 0 || i_prebody > 0 )
125
block_t *p_rea = block_New( p_block->p_manager, i_buffer_size );
127
p_rea->i_dts = p_block->i_dts;
128
p_rea->i_pts = p_block->i_pts;
129
p_rea->i_flags = p_block->i_flags;
130
p_rea->i_length = p_block->i_length;
131
p_rea->i_rate = p_block->i_rate;
132
p_rea->i_samples = p_block->i_samples;
134
memcpy( p_rea->p_buffer + i_prebody, p_block->p_buffer,
135
__MIN( p_block->i_buffer, p_rea->i_buffer - i_prebody ) );
150
/* FIXME: this is really dumb, we should use realloc() */
151
block_t *p_rea = block_New( NULL, i_buffer_size );
155
p_rea->i_dts = p_block->i_dts;
156
p_rea->i_pts = p_block->i_pts;
157
p_rea->i_flags = p_block->i_flags;
158
p_rea->i_length = p_block->i_length;
159
p_rea->i_rate = p_block->i_rate;
160
p_rea->i_samples = p_block->i_samples;
162
memcpy( p_rea->p_buffer + i_prebody, p_block->p_buffer,
163
__MIN( p_block->i_buffer, p_rea->i_buffer - i_prebody ) );
137
166
block_Release( p_block );
171
/* We have a very large reserved footer now? Release some of it. */
172
if ((p_sys->p_allocated_buffer + p_sys->i_allocated_buffer) -
173
(p_block->p_buffer + p_block->i_buffer) > BLOCK_WASTE_SIZE)
175
const size_t news = p_block->i_buffer + 2 * BLOCK_PADDING_SIZE + 16;
176
block_sys_t *newb = realloc (p_sys, sizeof (*p_sys) + news);
181
p_sys->i_allocated_buffer = news;
182
p_block = &p_sys->self;
183
p_block->p_buffer = p_sys->p_allocated_buffer + BLOCK_PADDING_SIZE
185
- ((uintptr_t)p_sys->p_allocated_buffer % BLOCK_ALIGN);
145
static void BlockRelease( block_t *p_block )
192
# include <sys/mman.h>
194
typedef struct block_mmap_t
201
static void block_mmap_Release (block_t *block)
203
block_mmap_t *p_sys = (block_mmap_t *)block;
205
munmap (p_sys->base_addr, p_sys->length);
210
* Creates a block from a virtual address memory mapping (mmap).
211
* This is provided by LibVLC so that mmap blocks can safely be deallocated
212
* even after the allocating plugin has been unloaded from memory.
214
* @param addr base address of the mapping (as returned by mmap)
215
* @param length length (bytes) of the mapping (as passed to mmap)
216
* @return NULL if addr is MAP_FAILED, or an error occurred (in the later
217
* case, munmap(addr, length) is invoked before returning).
219
block_t *block_mmap_Alloc (void *addr, size_t length)
221
if (addr == MAP_FAILED)
224
block_mmap_t *block = malloc (sizeof (*block));
227
munmap (addr, length);
231
block_Init (&block->self, (uint8_t *)addr, length);
232
block->self.pf_release = block_mmap_Release;
233
block->base_addr = addr;
234
block->length = length;
238
block_t *block_mmap_Alloc (void *addr, size_t length)
240
(void)addr; (void)length; return NULL;
247
ssize_t pread (int fd, void *buf, size_t count, off_t offset)
249
HANDLE handle = (HANDLE)(intptr_t)_get_osfhandle (fd);
250
if (handle == INVALID_HANDLE_VALUE)
253
OVERLAPPED olap = { .Offset = offset, .OffsetHigh = (offset >> 32), };
255
/* This braindead API will override the file pointer even if we specify
256
* an explicit read offset... So do not expect this to mix well with
257
* regular read() calls. */
258
if (ReadFile (handle, buf, count, &written, &olap))
265
* Loads a file into a block of memory. If possible a private file mapping is
266
* created. Otherwise, the file is read normally. On 32-bits platforms, this
267
* function will not work for very large files, due to memory space
270
* @param fd file descriptor to load from
271
* @return a new block with the file content at p_buffer, and file length at
272
* i_buffer (release it with block_Release()), or NULL upon error (see errno).
274
block_t *block_File (int fd)
279
/* First, get the file size */
283
/* st_size is meaningful for regular files, shared memory and typed memory.
284
* It's also meaning for symlinks, but that's not possible with fstat().
285
* In other cases, it's undefined, and we should really not go further. */
287
# define S_TYPEISSHM( buf ) (0)
289
if (S_ISDIR (st.st_mode))
294
if (!S_ISREG (st.st_mode) && !S_TYPEISSHM (&st))
300
/* Prevent an integer overflow in mmap() and malloc() */
301
if (st.st_size >= SIZE_MAX)
306
length = (size_t)st.st_size;
313
addr = mmap (NULL, length, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
314
if (addr != MAP_FAILED)
315
return block_mmap_Alloc (addr, length);
319
/* If mmap() is not implemented by the OS _or_ the filesystem... */
320
block_t *block = block_Alloc (length);
324
for (size_t i = 0; i < length;)
326
ssize_t len = pread (fd, block->p_buffer + i, length - i, i);
329
block_Release (block);
151
337
/*****************************************************************************
152
338
* block_fifo_t management
153
339
*****************************************************************************/
154
block_fifo_t *__block_FifoNew( vlc_object_t *p_obj )
156
block_fifo_t *p_fifo;
158
p_fifo = malloc( sizeof( block_fifo_t ) );
159
vlc_mutex_init( p_obj, &p_fifo->lock );
160
vlc_cond_init( p_obj, &p_fifo->wait );
161
p_fifo->i_depth = p_fifo->i_size = 0;
342
vlc_mutex_t lock; /* fifo data lock */
343
vlc_cond_t wait; /* fifo data conditional variable */
352
block_fifo_t *block_FifoNew( void )
354
block_fifo_t *p_fifo = malloc( sizeof( block_fifo_t ) );
358
vlc_mutex_init( &p_fifo->lock );
359
vlc_cond_init( NULL, &p_fifo->wait );
162
360
p_fifo->p_first = NULL;
163
361
p_fifo->pp_last = &p_fifo->p_first;
362
p_fifo->i_depth = p_fifo->i_size = 0;
363
p_fifo->b_force_wake = false;