[vlc-devel] commit: block_Alloc: use posix_memalign() ( Rémi Denis-Courmont )
git version control
git at videolan.org
Sun Sep 27 16:10:15 CEST 2009
vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Sun Sep 27 16:56:14 2009 +0300| [86997e006e2e08c1aaaf4b68408f7dbe5a4a21ed] | committer: Rémi Denis-Courmont
block_Alloc: use posix_memalign()
> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=86997e006e2e08c1aaaf4b68408f7dbe5a4a21ed
---
src/misc/block.c | 45 ++++++++++++++++++++++++++++++---------------
1 files changed, 30 insertions(+), 15 deletions(-)
diff --git a/src/misc/block.c b/src/misc/block.c
index 72e366f..bc93c9a 100644
--- a/src/misc/block.c
+++ b/src/misc/block.c
@@ -89,10 +89,10 @@ static void BlockMetaCopy( block_t *restrict out, const block_t *in )
out->i_nb_samples = in->i_nb_samples;
}
-/* Memory alignment */
+/* Memory alignment (must be a multiple of sizeof(void*) and a power of two) */
#define BLOCK_ALIGN 16
-/* Initial size of reserved header and footer */
-#define BLOCK_PADDING_SIZE 32
+/* Initial reserved header and footer size (must be multiple of alignment) */
+#define BLOCK_PADDING 32
/* Maximum size of reserved footer before we release with realloc() */
#define BLOCK_WASTE_SIZE 2048
@@ -100,24 +100,39 @@ 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: use memalign
- * 16 -> align on 16
- * 2 * BLOCK_PADDING_SIZE -> pre + post padding
+ * 2 * BLOCK_PADDING -> pre + post padding
*/
- const size_t i_alloc = i_size + 2 * BLOCK_PADDING_SIZE + BLOCK_ALIGN;
- block_sys_t *p_sys = malloc( sizeof( *p_sys ) + i_alloc );
+ block_sys_t *p_sys;
+ uint8_t *buf;
+#define ALIGN(x) (((x) + BLOCK_ALIGN - 1) & ~(BLOCK_ALIGN - 1))
+#ifdef HAVE_POSIX_MEMALIGN
+ const size_t i_alloc = ALIGN(sizeof(*p_sys)) + (2 * BLOCK_PADDING)
+ + ALIGN(i_size);
+ void *ptr;
+
+ if( posix_memalign( &ptr, BLOCK_ALIGN, i_alloc ) )
+ return NULL;
+
+ p_sys = ptr;
+ buf = p_sys->p_allocated_buffer + (-sizeof(*p_sys) & (BLOCK_ALIGN - 1));
+
+#else
+ const size_t i_alloc = sizeof(*p_sys) + BLOCK_ALIGN + (2 * BLOCK_PADDING)
+ + ALIGN(i_size);
+ p_sys = malloc( i_alloc );
if( p_sys == NULL )
return NULL;
- /* Fill opaque data */
- p_sys->i_allocated_buffer = i_alloc;
+ buf = (void *)ALIGN((uintptr_t)p_sys->p_allocated_buffer);
+
+#endif
+ buf += BLOCK_PADDING;
- block_Init( &p_sys->self, p_sys->p_allocated_buffer + BLOCK_PADDING_SIZE
- + BLOCK_ALIGN
- - ((uintptr_t)p_sys->p_allocated_buffer % BLOCK_ALIGN),
- i_size );
+ block_Init( &p_sys->self, buf, i_size );
p_sys->self.pf_release = BlockRelease;
+ /* Fill opaque data */
+ p_sys->i_allocated_buffer = i_alloc - sizeof(*p_sys);
return &p_sys->self;
}
@@ -216,7 +231,7 @@ block_t *block_Realloc( block_t *p_block, ssize_t i_prebody, size_t i_body )
if( p_end - (p_block->p_buffer + i_body) > BLOCK_WASTE_SIZE )
{
const ptrdiff_t i_prebody = p_block->p_buffer - p_start;
- const size_t i_new = requested + 1 * BLOCK_PADDING_SIZE;
+ const size_t i_new = requested + 1 * BLOCK_PADDING;
block_sys_t *p_new = realloc( p_sys, sizeof (*p_sys) + i_new );
if( p_new != NULL )
More information about the vlc-devel
mailing list