summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrichdawe <richdawe@1665872d-e22b-0410-9e5d-a57ad4215e6d>2006-09-03 12:48:23 +0000
committerrichdawe <richdawe@1665872d-e22b-0410-9e5d-a57ad4215e6d>2006-09-03 12:48:23 +0000
commite633500627c3263522e6322fe916e6556c4be32f (patch)
tree82c783a8d178600a99446994345ceb64fc183d26
parent1ea198a2cf19a0e84605891a8cc77f7f349d7526 (diff)
downloadportableumem-e633500627c3263522e6322fe916e6556c4be32f.tar.gz
Add --enable-malloc-replacement option, for using libumem as a malloc replacement
git-svn-id: https://labs.omniti.com/portableumem/trunk@24 1665872d-e22b-0410-9e5d-a57ad4215e6d
-rw-r--r--.cvsignore1
-rw-r--r--Makefile.am25
-rw-r--r--configure.ac10
-rw-r--r--init_lib.c41
-rw-r--r--malloc.c6
-rw-r--r--umem.spec.in1
-rw-r--r--umem_test.c2
-rw-r--r--umem_test3.c14
-rwxr-xr-xumem_test423
9 files changed, 111 insertions, 12 deletions
diff --git a/.cvsignore b/.cvsignore
index 544bfaf..0e22da6 100644
--- a/.cvsignore
+++ b/.cvsignore
@@ -18,6 +18,7 @@ stamp-h.in
stamp-h1
umem_test
umem_test2
+umem_test3
Doxyfile
umem.spec
*.tar.gz
diff --git a/Makefile.am b/Makefile.am
index 9086017..dc3ff41 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,13 +1,18 @@
-EXTRA_DIST = COPYRIGHT OPENSOLARIS.LICENSE umem.spec Doxyfile
+EXTRA_DIST = COPYRIGHT OPENSOLARIS.LICENSE umem.spec Doxyfile umem_test4
lib_LTLIBRARIES = libumem.la
-noinst_PROGRAMS = umem_test umem_test2
+noinst_PROGRAMS = umem_test umem_test2 umem_test3
+
+libumem_la_LDFLAGS = -lpthread -ldl
umem_test_SOURCES = umem_test.c
-umem_test_LDADD = -lumem -lpthread -ldl
+umem_test_LDADD = -lumem
umem_test2_SOURCES = umem_test2.c
-umem_test2_LDADD = -lumem -lpthread -ldl
+umem_test2_LDADD = -lumem
+
+umem_test3_SOURCES = umem_test3.c
+umem_test3_LDADD = -lumem
libumem_la_SOURCES = init_lib.c \
umem_agent_support.c \
@@ -32,9 +37,13 @@ libumem_la_SOURCES = init_lib.c \
sys/vmem.h \
sys/vmem_impl_user.h
+if MALLOC_REPLACEMENT
+libumem_la_SOURCES += malloc.c
+endif
+
nobase_include_HEADERS = umem.h sys/vmem.h
-TESTS = umem_test umem_test2
+TESTS = umem_test umem_test2 umem_test3 umem_test4
html-local:
mkdir -p docs
@@ -47,10 +56,6 @@ clean-local:
rpm: dist-bzip2
rpmbuild -ta $(distdir).tar.bz2
-# malloc.c
-
-# XXX: Standalone version?
-# See <http://cvs.opensolaris.org/source/xref/on/usr/src/lib/libumem/Makefile.com>
-
# XXX: Non-i386: SPARC asm. x86_64?
# Convert this to GNU as format: i386_subr_sol.s
+# <http://cvs.opensolaris.org/source/xref/on/usr/src/lib/libumem/>
diff --git a/configure.ac b/configure.ac
index a3574f3..a491850 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1,6 +1,16 @@
AC_INIT([umem], [1.0], [], [umem])
AM_INIT_AUTOMAKE([dist-bzip2])
+AC_ARG_ENABLE([malloc-replacement],
+ AS_HELP_STRING([--enable-malloc-replacement],
+ [Include implementations of malloc/free/etc. in libumem (default is no)]),
+ [case "${enableval}" in
+ yes) malloc_replacement=true ;;
+ no) malloc_replacement=false ;;
+ *) AC_MSG_ERROR(bad value ${enableval} for --enable-malloc-replacement) ;;
+ esac],[malloc_replacement=false])
+AM_CONDITIONAL(MALLOC_REPLACEMENT, test x$malloc_replacement = xtrue)
+
AC_PROG_CC
AM_PROG_AS
AC_PROG_LIBTOOL
diff --git a/init_lib.c b/init_lib.c
index ebb6ee6..0479af4 100644
--- a/init_lib.c
+++ b/init_lib.c
@@ -44,6 +44,9 @@
#include <dlfcn.h>
#endif
+#include <fcntl.h>
+#include <string.h>
+
void
vmem_heap_init(void)
{
@@ -89,7 +92,41 @@ umem_type_init(caddr_t start, size_t len, size_t pgsize)
int
umem_get_max_ncpus(void)
{
-#ifdef _SC_NPROCESSORS_ONLN
+#ifdef linux
+ /*
+ * HACK: sysconf() will invoke malloc() on Linux as part of reading
+ * in /proc/stat. To avoid recursion in the malloc replacement
+ * version of libumem, read /proc/stat into a static buffer.
+ */
+ static char proc_stat[8192];
+ int fd;
+ int ncpus = 1;
+
+ fd = open("/proc/stat", O_RDONLY);
+ if (fd >= 0) {
+ const ssize_t n = read(fd, proc_stat, sizeof(proc_stat) - 1);
+ if (n >= 0) {
+ const char *cur;
+ const char *next;
+
+ proc_stat[n] = '\0';
+ cur = proc_stat;
+ while (*cur && (next = strstr(cur + 3, "cpu"))) {
+ cur = next;
+ }
+
+ if (*cur)
+ ncpus = atoi(cur + 3) + 1;
+ }
+
+ close(fd);
+ }
+
+ return ncpus;
+
+#else /* !linux */
+
+#if _SC_NPROCESSORS_ONLN
return (2 * sysconf(_SC_NPROCESSORS_ONLN));
#elif defined(_SC_NPROCESSORS_CONF)
return (2 * sysconf(_SC_NPROCESSORS_CONF));
@@ -101,4 +138,6 @@ umem_get_max_ncpus(void)
/* XXX: determine CPU count on other platforms */
return (1);
#endif
+
+#endif /* linux */
}
diff --git a/malloc.c b/malloc.c
index 8b69bde..e107858 100644
--- a/malloc.c
+++ b/malloc.c
@@ -414,3 +414,9 @@ realloc(void *buf_arg, size_t newsize)
free(buf_arg);
return (buf);
}
+
+void __attribute__((constructor))
+__malloc_umem_init (void)
+{
+ umem_startup(NULL, 0, 0, NULL, NULL);
+}
diff --git a/umem.spec.in b/umem.spec.in
index 5e3046c..c4f293c 100644
--- a/umem.spec.in
+++ b/umem.spec.in
@@ -16,6 +16,7 @@ BuildRequires: doxygen
BuildRequires: gcc
BuildRequires: binutils
BuildRequires: make
+BuildRequires: mktemp
%description
diff --git a/umem_test.c b/umem_test.c
index 546bf5e..a90e91f 100644
--- a/umem_test.c
+++ b/umem_test.c
@@ -18,6 +18,6 @@ int main(int argc, char *argv[])
umem_free(foo, 32);
- return 0;
+ return EXIT_SUCCESS;
}
diff --git a/umem_test3.c b/umem_test3.c
new file mode 100644
index 0000000..bcbb231
--- /dev/null
+++ b/umem_test3.c
@@ -0,0 +1,14 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+int
+main (void)
+{
+ char *p;
+
+ p = malloc(10);
+ free(p);
+
+ return EXIT_SUCCESS;
+}
diff --git a/umem_test4 b/umem_test4
new file mode 100755
index 0000000..d4ea7ac
--- /dev/null
+++ b/umem_test4
@@ -0,0 +1,23 @@
+#!/bin/sh
+#
+# Check that LD_PRELOAD'ing libumem works. This is how it can be used
+# to replace malloc for any program.
+#
+
+FILE=Makefile.am
+TMPLOC=$(mktemp -d -t umem_test4.XXXXXX)
+trap 'rm -rf $TMPLOC' 1 2 15
+
+LD_PRELOAD=.libs/libumem.so /usr/bin/ldd /bin/ls >$TMPLOC/log 2>&1
+
+# Check we got the expected result
+ret=0
+grep -E '(symbol lookup error|undefined symbol)' $TMPLOC/log >/dev/null 2>&1
+if [ "$?" = "0" ]; then
+ # Matched = failed to load up libumem
+ ret=1
+fi
+
+rm -rf $TMPLOC
+
+exit $ret