buffer: allow configurable memory alignment. Fixes #596832

The alignment guaranteed by malloc is not always sufficient. E.g. vector
instructions or hardware subsystems want specifically aligned buffers. The
attached patch will use posix_memalign if available to allocate buffers.
The desired alignment can be set when running configure using the new
--with-buffer-alignment option.
This commit is contained in:
Stefan Kost 2010-03-04 10:44:52 +02:00
parent 679deb7581
commit a184419ec5
2 changed files with 108 additions and 0 deletions

View file

@ -400,6 +400,10 @@ dnl check for mmap()
AC_FUNC_MMAP
AM_CONDITIONAL(HAVE_MMAP, test "x$ac_cv_func_mmap_fixed_mapped" = "xyes")
dnl check for posix_memalign(), getpagesize()
AC_CHECK_FUNCS([posix_memalign])
AC_CHECK_FUNCS([getpagesize])
dnl Check for POSIX timers
AC_CHECK_FUNCS(clock_gettime, [], [
AC_CHECK_LIB(rt, clock_gettime, [
@ -556,6 +560,24 @@ dnl bit of a misnomer, but keep the conditional named like this so we don't
dnl have to change too much elsewhere
AM_CONDITIONAL(HAVE_CHECK, test "x$BUILD_CHECK" = "xyes")
dnl configure the desired buffer alignment
AC_ARG_WITH([buffer-alignment],
AS_HELP_STRING([--with-buffer-alignment],[8,N,malloc,pagesize (default is 32)]),
[
if test "x$withval" = "xyes"
then
AC_DEFINE(BUFFER_ALIGNMENT, 32, [Buffer alignment to use])
else
case "${withval}" in
malloc) AC_DEFINE(BUFFER_ALIGNMENT_MALLOC, 1, [Buffer alignment by malloc default]) ;;
pagesize) AC_DEFINE(BUFFER_ALIGNMENT_PAGESIZE, 1, [Buffer alignment by pagesize]) ;;
*) AC_DEFINE_UNQUOTED(BUFFER_ALIGNMENT, ${withval}, [Buffer alignment to use]) ;;
esac
fi
], [
AC_DEFINE(BUFFER_ALIGNMENT_MALLOC, 1, [Buffer alignment by malloc default])
]
)
dnl *** set variables based on configure arguments

View file

@ -115,6 +115,13 @@
*/
#include "gst_private.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include "gstbuffer.h"
#include "gstinfo.h"
#include "gstutils.h"
@ -126,6 +133,31 @@ static GstBuffer *_gst_buffer_copy (GstBuffer * buffer);
static GType _gst_buffer_type = 0;
/* buffer alignment in bytes
* an alignment of 8 would be the same as malloc() guarantees
*/
#ifdef HAVE_POSIX_MEMALIGN
#if defined(BUFFER_ALIGNMENT_MALLOC)
static size_t _gst_buffer_data_alignment = 8;
#elif defined(BUFFER_ALIGNMENT_PAGESIZE)
static size_t _gst_buffer_data_alignment = 0;
#elif defined(BUFFER_ALIGNMENT)
static size_t _gst_buffer_data_alignment = BUFFER_ALIGNMENT;
#else
#error "No buffer alignment configured"
#endif
static inline gboolean
aligned_malloc (gpointer * memptr, guint size)
{
gint res;
res = posix_memalign (memptr, _gst_buffer_data_alignment, size);
return (res == 0);
}
#endif /* HAVE_POSIX_MEMALIGN */
void
_gst_buffer_initialize (void)
{
@ -133,6 +165,11 @@ _gst_buffer_initialize (void)
* done from multiple threads;
* see http://bugzilla.gnome.org/show_bug.cgi?id=304551 */
g_type_class_ref (gst_buffer_get_type ());
#ifdef HAVE_GETPAGESIZE
#ifdef BUFFER_ALIGNMENT_PAGESIZE
_gst_buffer_data_alignment = getpagesize ();
#endif
#endif
}
#define _do_init \
@ -239,7 +276,26 @@ _gst_buffer_copy (GstBuffer * buffer)
copy = gst_buffer_new ();
/* we simply copy everything from our parent */
#ifdef HAVE_POSIX_MEMALIGN
{
gpointer memptr = NULL;
if (G_LIKELY (buffer->size)) {
if (G_UNLIKELY (!aligned_malloc (&memptr, buffer->size))) {
/* terminate on error like g_memdup() would */
g_error ("%s: failed to allocate %" G_GSIZE_FORMAT " bytes",
G_STRLOC, buffer->size);
} else {
memcpy (memptr, buffer->data, buffer->size);
}
}
copy->data = (guint8 *) memptr;
GST_BUFFER_FREE_FUNC (copy) = free;
}
#else
copy->data = g_memdup (buffer->data, buffer->size);
#endif
/* make sure it gets freed (even if the parent is subclassed, we return a
normal buffer) */
copy->malloc_data = copy->data;
@ -306,7 +362,23 @@ gst_buffer_new_and_alloc (guint size)
newbuf = gst_buffer_new ();
#ifdef HAVE_POSIX_MEMALIGN
{
gpointer memptr = NULL;
if (G_LIKELY (size)) {
if (G_UNLIKELY (!aligned_malloc (&memptr, size))) {
/* terminate on error like g_memdup() would */
g_error ("%s: failed to allocate %" G_GSIZE_FORMAT " bytes",
G_STRLOC, size);
}
}
newbuf->malloc_data = (guint8 *) memptr;
GST_BUFFER_FREE_FUNC (newbuf) = free;
}
#else
newbuf->malloc_data = g_malloc (size);
#endif
GST_BUFFER_DATA (newbuf) = newbuf->malloc_data;
GST_BUFFER_SIZE (newbuf) = size;
@ -336,13 +408,24 @@ gst_buffer_try_new_and_alloc (guint size)
{
GstBuffer *newbuf;
guint8 *malloc_data;
#ifdef HAVE_POSIX_MEMALIGN
gpointer memptr = NULL;
if (G_LIKELY (size)) {
if (G_UNLIKELY (!aligned_malloc (&memptr, size))) {
GST_CAT_WARNING (GST_CAT_BUFFER, "failed to allocate %d bytes", size);
return NULL;
}
}
malloc_data = (guint8 *) memptr;
#else
malloc_data = g_try_malloc (size);
if (G_UNLIKELY (malloc_data == NULL && size != 0)) {
GST_CAT_WARNING (GST_CAT_BUFFER, "failed to allocate %d bytes", size);
return NULL;
}
#endif
/* FIXME: there's no g_type_try_create_instance() in GObject yet, so this
* will still abort if a new GstBuffer structure can't be allocated */
@ -351,6 +434,9 @@ gst_buffer_try_new_and_alloc (guint size)
GST_BUFFER_MALLOCDATA (newbuf) = malloc_data;
GST_BUFFER_DATA (newbuf) = malloc_data;
GST_BUFFER_SIZE (newbuf) = size;
#ifdef HAVE_POSIX_MEMALIGN
GST_BUFFER_FREE_FUNC (newbuf) = free;
#endif
GST_CAT_LOG (GST_CAT_BUFFER, "new %p of size %d", newbuf, size);