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
{
/* 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;
}
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 )