[vlc-devel] commit: Realloc block if a large extent of the footer becomes unused ( Rémi Denis-Courmont )

git version control git at videolan.org
Mon Mar 24 21:30:32 CET 2008


vlc | branch: master | Rémi Denis-Courmont <rem at videolan.org> | Mon Mar 24 22:30:18 2008 +0200| [daa7c08f86f23fc4cd301f2c48e8540dc3a8f117]

Realloc block if a large extent of the footer becomes unused

Should fix #1536

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=daa7c08f86f23fc4cd301f2c48e8540dc3a8f117
---

 src/misc/block.c |   32 +++++++++++++++++++++++++++++---
 1 files changed, 29 insertions(+), 3 deletions(-)

diff --git a/src/misc/block.c b/src/misc/block.c
index e2c0023..d113a1c 100644
--- a/src/misc/block.c
+++ b/src/misc/block.c
@@ -69,16 +69,22 @@ static void BlockRelease( block_t *p_block )
     free( p_block );
 }
 
+/* Memory alignment */
+#define BLOCK_ALIGN        16
+/* Initial size of reserved header and footer */
 #define BLOCK_PADDING_SIZE 32
+/* Maximum size of reserved footer before we release with realloc() */
+#define BLOCK_WASTE_SIZE   2048
 
 block_t *block_Alloc( size_t i_size )
 {
     /* We do only one malloc
-     * TODO bench if doing 2 malloc but keeping a pool of buffer is better
+     * TODO: bench if doing 2 malloc but keeping a pool of buffer is better
+     * TODO: use memalign
      * 16 -> align on 16
      * 2 * BLOCK_PADDING_SIZE -> pre + post padding
      */
-    const size_t i_alloc = i_size + 2 * BLOCK_PADDING_SIZE + 16;
+    const size_t i_alloc = i_size + 2 * BLOCK_PADDING_SIZE + BLOCK_ALIGN;
     block_sys_t *p_sys = malloc( sizeof( *p_sys ) + i_alloc );
 
     if( p_sys == NULL )
@@ -88,7 +94,9 @@ block_t *block_Alloc( size_t i_size )
     p_sys->i_allocated_buffer = i_alloc;
 
     block_Init( &p_sys->self, p_sys->p_allocated_buffer + BLOCK_PADDING_SIZE
-                + 16 - ((uintptr_t)p_sys->p_allocated_buffer % 16 ), i_size );
+                + BLOCK_ALIGN
+                - ((uintptr_t)p_sys->p_allocated_buffer % BLOCK_ALIGN),
+                i_size );
     p_sys->self.pf_release    = BlockRelease;
 
     return &p_sys->self;
@@ -161,6 +169,24 @@ 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)
+    {
+        const size_t news = p_block->i_buffer + 2 * BLOCK_PADDING_SIZE + 16;
+        block_sys_t *newb = realloc (p_sys, sizeof (*p_sys) + news);
+
+        if (newb != NULL)
+        {
+            p_sys = newb;
+            p_sys->i_allocated_buffer = news;
+            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);
+        }
+    }
+
     return p_block;
 }
 




More information about the vlc-devel mailing list