From 7b744cf4d25b2c80cd28efd3e48fd08862b87beb Mon Sep 17 00:00:00 2001 From: Tim Janik Date: Tue, 24 Jan 2006 16:56:17 +0000 Subject: [PATCH] only use posix_memalign() if it's known to work, revert to memalign() Tue Jan 24 17:49:36 2006 Tim Janik * glib/gslice.c: only use posix_memalign() if it's known to work, revert to memalign() otherwise. * configure.in: check for broken posix_memalign() implementations to fix #328254. --- ChangeLog | 8 ++++++++ ChangeLog.pre-2-10 | 8 ++++++++ ChangeLog.pre-2-12 | 8 ++++++++ configure.in | 41 +++++++++++++++++++++++++++++++++++++++++ glib/gslice.c | 20 +++++++++++++------- 5 files changed, 78 insertions(+), 7 deletions(-) diff --git a/ChangeLog b/ChangeLog index c6c5cff..19b88ef 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +Tue Jan 24 17:49:36 2006 Tim Janik + + * glib/gslice.c: only use posix_memalign() if it's known to work, + revert to memalign() otherwise. + + * configure.in: check for broken posix_memalign() implementations + to fix #328254. + 2006-01-24 Matthias Clasen * tests/unicode-encoding.c: Use UTF-16LE as target encoding diff --git a/ChangeLog.pre-2-10 b/ChangeLog.pre-2-10 index c6c5cff..19b88ef 100644 --- a/ChangeLog.pre-2-10 +++ b/ChangeLog.pre-2-10 @@ -1,3 +1,11 @@ +Tue Jan 24 17:49:36 2006 Tim Janik + + * glib/gslice.c: only use posix_memalign() if it's known to work, + revert to memalign() otherwise. + + * configure.in: check for broken posix_memalign() implementations + to fix #328254. + 2006-01-24 Matthias Clasen * tests/unicode-encoding.c: Use UTF-16LE as target encoding diff --git a/ChangeLog.pre-2-12 b/ChangeLog.pre-2-12 index c6c5cff..19b88ef 100644 --- a/ChangeLog.pre-2-12 +++ b/ChangeLog.pre-2-12 @@ -1,3 +1,11 @@ +Tue Jan 24 17:49:36 2006 Tim Janik + + * glib/gslice.c: only use posix_memalign() if it's known to work, + revert to memalign() otherwise. + + * configure.in: check for broken posix_memalign() implementations + to fix #328254. + 2006-01-24 Matthias Clasen * tests/unicode-encoding.c: Use UTF-16LE as target encoding diff --git a/configure.in b/configure.in index f25b277..1207242 100644 --- a/configure.in +++ b/configure.in @@ -1021,6 +1021,47 @@ AC_MSG_RESULT($have_codeset) dnl **************************************** +dnl *** posix_memalign *** +dnl **************************************** +AC_MSG_CHECKING(for a compliant posix_memalign() implementation) +AC_CACHE_VAL(glib_cv_compliant_posix_memalign,[ + glib_cv_compliant_posix_memalign=0 + if test "$ac_cv_func_posix_memalign" = "yes" ; then + AC_TRY_RUN([ + #define _XOPEN_SOURCE 600 + #include + #include + static void test_memalign (size_t boundary, size_t size) { + void *mem = 0; + if (posix_memalign (&mem, boundary, size) != 0 || !mem) + exit (1); + } + int main() { + test_memalign ( 128, 128 - 2 * sizeof (void*)); + test_memalign ( 256, 256 - 2 * sizeof (void*)); + test_memalign ( 512, 512 - 2 * sizeof (void*)); + test_memalign ( 1024, 1024 - 2 * sizeof (void*)); + test_memalign ( 2048, 2048 - 2 * sizeof (void*)); + test_memalign ( 4096, 4096 - 2 * sizeof (void*)); + test_memalign ( 8192, 8192 - 2 * sizeof (void*)); + test_memalign (16384, 16384 - 2 * sizeof (void*)); + test_memalign (32768, 32768 - 2 * sizeof (void*)); + exit (0); /* success */ + } + ], + [glib_cv_compliant_posix_memalign=1], [], [:]) + : + fi + ]) +if test "$glib_cv_compliant_posix_memalign" = "1"; then + AC_MSG_RESULT(yes) + AC_DEFINE(POSIX_MEMALIGN_WITH_COMPLIANT_ALLOCS, 1, [define if posix_memalign() can allocate any size]) +else + AC_MSG_RESULT(no) +fi + + +dnl **************************************** dnl *** strlcpy/strlcat *** dnl **************************************** # Check for strlcpy diff --git a/glib/gslice.c b/glib/gslice.c index c9a4d02..ae18056 100644 --- a/glib/gslice.c +++ b/glib/gslice.c @@ -38,6 +38,11 @@ #include #endif +#if defined HAVE_POSIX_MEMALIGN && defined POSIX_MEMALIGN_WITH_COMPLIANT_ALLOCS +# define HAVE_COMLIANT_POSIX_MEMALIGN 1 +#endif + + /* the GSlice allocator is split up into 4 layers, roughly modelled after the slab * allocator and magazine extensions as outlined in: * + [Bonwick94] Jeff Bonwick, The slab allocator: An object-caching kernel @@ -292,7 +297,7 @@ g_slice_init_nomessage (void) mem_assert ((sys_page_size & (sys_page_size - 1)) == 0); slice_config_init (&allocator->config); allocator->min_page_size = sys_page_size; -#if HAVE_POSIX_MEMALIGN || HAVE_MEMALIGN +#if HAVE_COMLIANT_POSIX_MEMALIGN || HAVE_MEMALIGN /* allow allocation of pages up to 8KB (with 8KB alignment). * this is useful because many medium to large sized structures * fit less than 8 times (see [4]) into 4KB pages. @@ -1022,13 +1027,14 @@ slab_allocator_free_chunk (gsize chunk_size, #endif /* from config.h: - * define HAVE_POSIX_MEMALIGN 1 // if free(posix_memalign(3)) works, - * define HAVE_MEMALIGN 1 // if free(memalign(3)) works, - * define HAVE_VALLOC 1 // if free(valloc(3)) works, or + * define HAVE_POSIX_MEMALIGN 1 // if free(posix_memalign(3)) works, + * define HAVE_COMLIANT_POSIX_MEMALIGN 1 // if free(posix_memalign(3)) works for sizes != 2^n, + * define HAVE_MEMALIGN 1 // if free(memalign(3)) works, + * define HAVE_VALLOC 1 // if free(valloc(3)) works, or * if none is provided, we implement malloc(3)-based alloc-only page alignment */ -#if !(HAVE_POSIX_MEMALIGN || HAVE_MEMALIGN || HAVE_VALLOC) +#if !(HAVE_COMLIANT_POSIX_MEMALIGN || HAVE_MEMALIGN || HAVE_VALLOC) static GTrashStack *compat_valloc_trash = NULL; #endif @@ -1038,7 +1044,7 @@ allocator_memalign (gsize alignment, { gpointer aligned_memory = NULL; gint err = ENOMEM; -#if HAVE_POSIX_MEMALIGN +#if HAVE_COMLIANT_POSIX_MEMALIGN err = posix_memalign (&aligned_memory, alignment, memsize); #elif HAVE_MEMALIGN errno = 0; @@ -1078,7 +1084,7 @@ static void allocator_memfree (gsize memsize, gpointer mem) { -#if HAVE_POSIX_MEMALIGN || HAVE_MEMALIGN || HAVE_VALLOC +#if HAVE_COMLIANT_POSIX_MEMALIGN || HAVE_MEMALIGN || HAVE_VALLOC free (mem); #else mem_assert (memsize <= sys_page_size); -- 2.7.4