summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorivmai <ivmai>2011-05-30 16:52:54 +0000
committerIvan Maidanski <ivmai@mail.ru>2011-07-25 16:03:26 +0400
commit2657b13b2a090a13aeb692d2df39c2034989be3e (patch)
tree7fe2ab25fd136ca5a920f0e9b369512f33d2c004
parent93d66c013aa74b66e620f22d0feb2c5e96794138 (diff)
downloadlibatomic_ops-2657b13b2a090a13aeb692d2df39c2034989be3e.tar.gz
2011-05-30 Ivan Maidanski <ivmai@mail.ru>
* configure.ac (PIC): Add workaround for GCC v3.4.6 which does not define the corresponding macro. * configure.ac: Don't check for GCC twice. * configure: Regenerate. * tests/test_malloc.c (DEFAULT_NTHREADS): New macro. * tests/test_malloc.c (run_one_test): Refine printed message (in case of HAVE_MMAP). * tests/test_malloc.c (main): Use DEFAULT_NTHREADS. * tests/test_stack.c (main): Cast AO_stack_pop() returned pointer. * src/atomic_ops_malloc.c (USE_MMAP_ANON, GC_MMAP_FLAGS, OPT_MAP_ANON): New macro. * src/atomic_ops_malloc.c (get_mmaped): Pass -1 (instead of 0) as file descriptor to mmap() if MAP_ANONYMOUS (same as for MAP_ANON). * src/atomic_ops_malloc.c (get_mmaped): Use USE_MMAP_ANON, GC_MMAP_FLAGS and OPT_MAP_ANON macros; reformat code; check open() returned value.
-rw-r--r--ChangeLog19
-rwxr-xr-xconfigure16
-rw-r--r--configure.ac16
-rw-r--r--src/atomic_ops_malloc.c59
-rw-r--r--tests/test_malloc.c26
-rw-r--r--tests/test_stack.c2
6 files changed, 98 insertions, 40 deletions
diff --git a/ChangeLog b/ChangeLog
index c864db2..e876913 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,22 @@
+2011-05-30 Ivan Maidanski <ivmai@mail.ru>
+
+ * configure.ac (PIC): Add workaround for GCC v3.4.6 which does not
+ define the corresponding macro.
+ * configure.ac: Don't check for GCC twice.
+ * configure: Regenerate.
+ * tests/test_malloc.c (DEFAULT_NTHREADS): New macro.
+ * tests/test_malloc.c (run_one_test): Refine printed message (in
+ case of HAVE_MMAP).
+ * tests/test_malloc.c (main): Use DEFAULT_NTHREADS.
+ * tests/test_stack.c (main): Cast AO_stack_pop() returned pointer.
+ * src/atomic_ops_malloc.c (USE_MMAP_ANON, GC_MMAP_FLAGS,
+ OPT_MAP_ANON): New macro.
+ * src/atomic_ops_malloc.c (get_mmaped): Pass -1 (instead of 0) as
+ file descriptor to mmap() if MAP_ANONYMOUS (same as for MAP_ANON).
+ * src/atomic_ops_malloc.c (get_mmaped): Use USE_MMAP_ANON,
+ GC_MMAP_FLAGS and OPT_MAP_ANON macros; reformat code; check open()
+ returned value.
+
2011-05-11 Ivan Maidanski <ivmai@mail.ru>
* tests/test_malloc.c: Expand all tabs to spaces; remove trailing
diff --git a/configure b/configure
index 91069fd..56e2d35 100755
--- a/configure
+++ b/configure
@@ -4730,24 +4730,26 @@ if test "$GCC" = yes; then
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: -fPIC" >&5
$as_echo "-fPIC" >&6; }
PICFLAG=-fPIC
+ case "$host" in
+ *-*-solaris*)
+ # Workaround: at least GCC 3.4.6 does not define this macro.
+ CFLAGS="$CFLAGS -D__PIC__=1"
+ ;;
+ esac
else
case "$host" in
*-*-hpux*)
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: \"+Z\"" >&5
$as_echo "\"+Z\"" >&6; }
PICFLAG="+Z"
- if test "$GCC" != yes; then
- CFLAGS="$CFLAGS +O2 -mt"
- fi
+ CFLAGS="$CFLAGS +O2 -mt"
;;
*-*-solaris*)
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: -Kpic" >&5
$as_echo "-Kpic" >&6; }
PICFLAG=-Kpic
- if test "$GCC" != yes; then
- CFLAGS="$CFLAGS -O"
- need_asm=true
- fi
+ CFLAGS="$CFLAGS -O"
+ need_asm=true
;;
*-*-linux*)
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: -fPIC" >&5
diff --git a/configure.ac b/configure.ac
index 547e315..e913b96 100644
--- a/configure.ac
+++ b/configure.ac
@@ -25,22 +25,24 @@ AC_MSG_CHECKING(Determining PIC compiler flag)
if test "$GCC" = yes; then
AC_MSG_RESULT(-fPIC)
PICFLAG=-fPIC
+ case "$host" in
+ *-*-solaris*)
+ # Workaround: at least GCC 3.4.6 does not define this macro.
+ CFLAGS="$CFLAGS -D__PIC__=1"
+ ;;
+ esac
else
case "$host" in
*-*-hpux*)
AC_MSG_RESULT("+Z")
PICFLAG="+Z"
- if test "$GCC" != yes; then
- CFLAGS="$CFLAGS +O2 -mt"
- fi
+ CFLAGS="$CFLAGS +O2 -mt"
;;
*-*-solaris*)
AC_MSG_RESULT(-Kpic)
PICFLAG=-Kpic
- if test "$GCC" != yes; then
- CFLAGS="$CFLAGS -O"
- need_asm=true
- fi
+ CFLAGS="$CFLAGS -O"
+ need_asm=true
;;
*-*-linux*)
AC_MSG_RESULT(-fPIC)
diff --git a/src/atomic_ops_malloc.c b/src/atomic_ops_malloc.c
index 4c08269..3af585e 100644
--- a/src/atomic_ops_malloc.c
+++ b/src/atomic_ops_malloc.c
@@ -67,6 +67,28 @@ static volatile char *initial_heap_lim = AO_initial_heap + AO_INITIAL_HEAP_SIZE;
#include <fcntl.h>
#include <sys/mman.h>
+#if defined(MAP_ANONYMOUS) || defined(MAP_ANON)
+# define USE_MMAP_ANON
+#endif
+
+#ifdef USE_MMAP_FIXED
+# define GC_MMAP_FLAGS MAP_FIXED | MAP_PRIVATE
+ /* Seems to yield better performance on Solaris 2, but can */
+ /* be unreliable if something is already mapped at the address. */
+#else
+# define GC_MMAP_FLAGS MAP_PRIVATE
+#endif
+
+#ifdef USE_MMAP_ANON
+# ifdef MAP_ANONYMOUS
+# define OPT_MAP_ANON MAP_ANONYMOUS
+# else
+# define OPT_MAP_ANON MAP_ANON
+# endif
+#else
+# define OPT_MAP_ANON 0
+#endif
+
static volatile AO_t mmap_enabled = 0;
void
@@ -78,24 +100,28 @@ AO_malloc_enable_mmap(void)
static char *get_mmaped(size_t sz)
{
char * result;
+# ifdef USE_MMAP_ANON
+# define zero_fd -1
+# else
+ int zero_fd;
+# endif
assert(!(sz & (CHUNK_SIZE - 1)));
- if (!mmap_enabled) return 0;
-# if defined(MAP_ANONYMOUS)
- result = mmap(0, sz, PROT_READ | PROT_WRITE,
- MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
-# elif defined(MAP_ANON)
- result = mmap(0, sz, PROT_READ | PROT_WRITE,
- MAP_PRIVATE | MAP_ANON, -1, 0);
-# else
- {
- int zero_fd = open("/dev/zero", O_RDONLY);
- result = mmap(0, sz, PROT_READ | PROT_WRITE,
- MAP_PRIVATE, zero_fd, 0);
- close(zero_fd);
- }
+ if (!mmap_enabled)
+ return 0;
+
+# ifndef USE_MMAP_ANON
+ zero_fd = open("/dev/zero", O_RDONLY);
+ if (zero_fd == -1)
+ return 0;
+# endif
+ result = mmap(0, sz, PROT_READ | PROT_WRITE,
+ GC_MMAP_FLAGS | OPT_MAP_ANON, zero_fd, 0/* offset */);
+# ifndef USE_MMAP_ANON
+ close(zero_fd);
# endif
- if (result == MAP_FAILED) result = 0;
+ if (result == MAP_FAILED)
+ result = 0;
return result;
}
@@ -281,8 +307,7 @@ AO_free(void *p)
log_sz = *(AO_t *)base;
# ifdef AO_TRACE_MALLOC
fprintf(stderr, "%x: AO_free(%p sz:%lu)\n", (int)pthread_self(), p,
- (unsigned long)
- (log_sz > LOG_MAX_SIZE? log_sz : (1 << log_sz)));
+ (unsigned long)(log_sz > LOG_MAX_SIZE? log_sz : (1 << log_sz)));
# endif
if (log_sz > LOG_MAX_SIZE)
AO_free_large(p);
diff --git a/tests/test_malloc.c b/tests/test_malloc.c
index 4eb50a2..11e13ce 100644
--- a/tests/test_malloc.c
+++ b/tests/test_malloc.c
@@ -25,12 +25,23 @@
#ifndef MAX_NTHREADS
# define MAX_NTHREADS 100
#endif
+
+#ifndef DEFAULT_NTHREADS
+# ifdef HAVE_MMAP
+# define DEFAULT_NTHREADS 10
+# else
+# define DEFAULT_NTHREADS 3
+# endif
+#endif
+
#ifndef N_REVERSALS
# define N_REVERSALS 1000 /* must be even */
#endif
+
#ifndef LIST_LENGTH
# define LIST_LENGTH 1000
#endif
+
#ifndef LARGE_OBJ_SIZE
# define LARGE_OBJ_SIZE 200000
#endif
@@ -130,9 +141,12 @@ void * run_one_test(void * arg) {
char *q;
if (0 == p) {
- fprintf(stderr, "AO_malloc(%d) failed: This is normal without mmap\n",
- LARGE_OBJ_SIZE);
- AO_free(p);
+# ifdef HAVE_MMAP
+ fprintf(stderr, "AO_malloc(%d) failed\n", LARGE_OBJ_SIZE);
+# else
+ fprintf(stderr, "AO_malloc(%d) failed: This is normal without mmap\n",
+ LARGE_OBJ_SIZE);
+# endif
} else {
p[0] = p[LARGE_OBJ_SIZE/2] = p[LARGE_OBJ_SIZE-1] = 'a';
q = AO_malloc(LARGE_OBJ_SIZE);
@@ -168,11 +182,7 @@ int main(int argc, char **argv) {
int exper_n;
if (1 == argc) {
-# if !defined(HAVE_MMAP)
- nthreads = 3;
-# else
- nthreads = 10;
-# endif
+ nthreads = DEFAULT_NTHREADS;
} else if (2 == argc) {
nthreads = atoi(argv[1]);
if (nthreads < 1 || nthreads > MAX_NTHREADS) {
diff --git a/tests/test_stack.c b/tests/test_stack.c
index 3955dc3..177ceee 100644
--- a/tests/test_stack.c
+++ b/tests/test_stack.c
@@ -213,7 +213,7 @@ int main(int argc, char **argv)
print_list();
# endif
check_list(list_length);
- while ((le = AO_stack_pop(&the_list)) != 0)
+ while ((le = (list_element *)AO_stack_pop(&the_list)) != 0)
free(le);
}
# ifndef NO_TIMES