summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xconfigure38
-rw-r--r--gc.c21
-rw-r--r--lib.c25
3 files changed, 80 insertions, 4 deletions
diff --git a/configure b/configure
index ef489af5..34be9b59 100755
--- a/configure
+++ b/configure
@@ -2675,6 +2675,44 @@ if [ -z "$have_alloca" ] ; then
exit 1
fi
+printf "Checking for malloc-with-alignment function ... "
+
+while true ; do
+ cat > conftest.c <<!
+#include <malloc.h>
+
+int main(void)
+{
+ void *bytes = memalign(16, 42);
+ return 0;
+}
+!
+
+ if ! conftest ; then
+ printf "memalign\n"
+ printf "#define HAVE_MEMALIGN 1\n" $try_header >> config.h
+ break;
+ fi
+
+ cat > conftest.c <<!
+#include <stdlib.h>
+
+int main(void)
+{
+ void *bytes;
+ int res = posix_memalign(&bytes, 16, 42);
+ return 0;
+}
+!
+ if conftest ; then
+ printf "posix_memalign\n"
+ printf "#define HAVE_POSIX_MEMALIGN 1\n" $try_header >> config.h
+ break;
+ fi
+
+ printf "none\n"
+done
+
printf "Checking for termios ... "
cat > conftest.c <<!
diff --git a/gc.c b/gc.c
index 1b3e68cd..93004338 100644
--- a/gc.c
+++ b/gc.c
@@ -57,9 +57,15 @@
#define STACK_TOP_EXTRA_WORDS 0
#endif
+#if HAVE_MEMALIGN || HAVE_POSIX_MEMALIGN
+#define OBJ_ALIGN (sizeof (obj_t))
+#else
+#define OBJ_ALIGN 8
+#endif
+
typedef struct heap {
- struct heap *next;
obj_t block[HEAP_SIZE];
+ struct heap *next;
} heap_t;
typedef struct mach_context {
@@ -427,13 +433,22 @@ static int in_heap(val ptr)
if (!is_ptr(ptr))
return 0;
+ if (coerce(uint_ptr_t, ptr) % OBJ_ALIGN != 0)
+ return 0;
+
if (ptr < heap_min_bound || ptr >= heap_max_bound)
return 0;
for (heap = heap_list; heap != 0; heap = heap->next) {
- if (ptr >= heap->block && ptr < heap->block + HEAP_SIZE)
- if ((coerce(char *, ptr) - coerce(char *, heap->block)) % sizeof (obj_t) == 0)
+ if (ptr >= heap->block && ptr < heap->block + HEAP_SIZE) {
+#if HAVE_MEMALIGN || HAVE_POSIX_MEMALIGN
+ return 1;
+#else
+ if ((coerce(char *, ptr) -
+ coerce(char *, heap->block)) % sizeof (obj_t) == 0)
return 1;
+#endif
+ }
}
return 0;
diff --git a/lib.c b/lib.c
index debd353f..f4f47b76 100644
--- a/lib.c
+++ b/lib.c
@@ -45,6 +45,9 @@
#define NOMINMAX
#include <windows.h>
#endif
+#if HAVE_MEMALIGN
+#include <malloc.h>
+#endif
#include "lib.h"
#include "gc.h"
#include "arith.h"
@@ -2849,9 +2852,29 @@ mem_t *chk_malloc(size_t size)
return ptr;
}
+#if HAVE_POSIX_MEMALIGN
+
+static void *memalign(size_t align, size_t size)
+{
+ void *ptr;
+ if (posix_memalign(&ptr, align, size) == 0)
+ return ptr;
+ return 0;
+}
+
+#elif !HAVE_MEMALIGN
+
+static void *memalign(size_t align, size_t size)
+{
+ (void) align;
+ return malloc(size);
+}
+
+#endif
+
mem_t *chk_malloc_gc_more(size_t size)
{
- mem_t *ptr = convert(mem_t *, malloc(size));
+ mem_t *ptr = convert(mem_t *, memalign(sizeof (obj_t), size));
assert (!async_sig_enabled);
if (size && ptr == 0)
oom();