Commit d9046263 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

block: add block_TryRealloc()

This works like block_Realloc() but retains the original block in case
of failure.
parent ba7f5acd
...@@ -137,6 +137,7 @@ struct block_t ...@@ -137,6 +137,7 @@ struct block_t
****************************************************************************/ ****************************************************************************/
VLC_API void block_Init( block_t *, void *, size_t ); VLC_API void block_Init( block_t *, void *, size_t );
VLC_API block_t *block_Alloc( size_t ) VLC_USED VLC_MALLOC; VLC_API block_t *block_Alloc( size_t ) VLC_USED VLC_MALLOC;
block_t *block_TryRealloc(block_t *, ssize_t pre, size_t body) VLC_USED;
VLC_API block_t *block_Realloc( block_t *, ssize_t i_pre, size_t i_body ) VLC_USED; VLC_API block_t *block_Realloc( block_t *, ssize_t i_pre, size_t i_body ) VLC_USED;
static inline void block_CopyProperties( block_t *dst, block_t *src ) static inline void block_CopyProperties( block_t *dst, block_t *src )
......
...@@ -145,28 +145,42 @@ block_t *block_Alloc (size_t size) ...@@ -145,28 +145,42 @@ block_t *block_Alloc (size_t size)
return b; return b;
} }
block_t *block_Realloc( block_t *p_block, ssize_t i_prebody, size_t i_body ) block_t *block_TryRealloc (block_t *p_block, ssize_t i_prebody, size_t i_body)
{ {
size_t requested = i_prebody + i_body;
block_Check( p_block ); block_Check( p_block );
/* Corner case: empty block requested */ /* Corner case: empty block requested */
if( i_prebody <= 0 && i_body <= (size_t)(-i_prebody) ) if( i_prebody <= 0 && i_body <= (size_t)(-i_prebody) )
{ i_prebody = i_body = 0;
block_Release( p_block );
return NULL;
}
assert( p_block->p_start <= p_block->p_buffer ); assert( p_block->p_start <= p_block->p_buffer );
assert( p_block->p_start + p_block->i_size assert( p_block->p_start + p_block->i_size
>= p_block->p_buffer + p_block->i_buffer ); >= p_block->p_buffer + p_block->i_buffer );
/* Corner case: the current payload is discarded completely */ /* First, shrink payload */
if( i_prebody <= 0 && p_block->i_buffer <= (size_t)-i_prebody )
p_block->i_buffer = 0; /* discard current payload */ /* Pull payload start */
if( p_block->i_buffer == 0 ) if( i_prebody < 0 )
{ {
if( p_block->i_buffer >= (size_t)-i_prebody )
{
p_block->p_buffer -= i_prebody;
p_block->i_buffer += i_prebody;
}
else /* Discard current payload entirely */
p_block->i_buffer = 0;
i_body += i_prebody;
i_prebody = 0;
}
/* Trim payload end */
if( p_block->i_buffer > i_body )
p_block->i_buffer = i_body;
size_t requested = i_prebody + i_body;
if( p_block->i_buffer == 0 )
{ /* Corner case: nothing to preserve */
if( requested <= p_block->i_size ) if( requested <= p_block->i_size )
{ /* Enough room: recycle buffer */ { /* Enough room: recycle buffer */
size_t extra = p_block->i_size - requested; size_t extra = p_block->i_size - requested;
...@@ -175,54 +189,35 @@ block_t *block_Realloc( block_t *p_block, ssize_t i_prebody, size_t i_body ) ...@@ -175,54 +189,35 @@ block_t *block_Realloc( block_t *p_block, ssize_t i_prebody, size_t i_body )
p_block->i_buffer = requested; p_block->i_buffer = requested;
return p_block; return p_block;
} }
/* Not enough room: allocate a new buffer */ /* Not enough room: allocate a new buffer */
block_t *p_rea = block_Alloc( requested ); block_t *p_rea = block_Alloc( requested );
if( p_rea ) if( p_rea == NULL )
return NULL;
BlockMetaCopy( p_rea, p_block ); BlockMetaCopy( p_rea, p_block );
block_Release( p_block ); block_Release( p_block );
return p_rea; return p_rea;
} }
/* First, shrink payload */
/* Pull payload start */
if( i_prebody < 0 )
{
assert( p_block->i_buffer >= (size_t)-i_prebody );
p_block->p_buffer -= i_prebody;
p_block->i_buffer += i_prebody;
i_body += i_prebody;
i_prebody = 0;
}
/* Trim payload end */
if( p_block->i_buffer > i_body )
p_block->i_buffer = i_body;
uint8_t *p_start = p_block->p_start; uint8_t *p_start = p_block->p_start;
uint8_t *p_end = p_start + p_block->i_size; uint8_t *p_end = p_start + p_block->i_size;
/* Second, reallocate the buffer if we lack space. This is done now to /* Second, reallocate the buffer if we lack space. */
* minimize the payload size for memory copy. */
assert( i_prebody >= 0 ); assert( i_prebody >= 0 );
if( (size_t)(p_block->p_buffer - p_start) < (size_t)i_prebody if( (size_t)(p_block->p_buffer - p_start) < (size_t)i_prebody
|| (size_t)(p_end - p_block->p_buffer) < i_body ) || (size_t)(p_end - p_block->p_buffer) < i_body )
{ {
block_t *p_rea = block_Alloc( requested ); block_t *p_rea = block_Alloc( requested );
if( p_rea )
{
BlockMetaCopy( p_rea, p_block );
p_rea->p_buffer += i_prebody;
p_rea->i_buffer -= i_prebody;
memcpy( p_rea->p_buffer, p_block->p_buffer, p_block->i_buffer );
}
block_Release( p_block );
if( p_rea == NULL ) if( p_rea == NULL )
return NULL; return NULL;
p_block = p_rea;
}
/* NOTE: p_start and p_end are corrupted from this point */ memcpy( p_rea->p_buffer + i_prebody, p_block->p_buffer,
p_block->i_buffer );
BlockMetaCopy( p_rea, p_block );
block_Release( p_block );
return p_rea;
}
/* Third, expand payload */ /* Third, expand payload */
...@@ -241,6 +236,13 @@ block_t *block_Realloc( block_t *p_block, ssize_t i_prebody, size_t i_body ) ...@@ -241,6 +236,13 @@ block_t *block_Realloc( block_t *p_block, ssize_t i_prebody, size_t i_body )
return p_block; return p_block;
} }
block_t *block_Realloc (block_t *block, ssize_t prebody, size_t body)
{
block_t *rea = block_TryRealloc (block, prebody, body);
if (rea == NULL)
block_Release(block);
return rea;
}
static void block_heap_Release (block_t *block) static void block_heap_Release (block_t *block)
{ {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment