Index: /trunk/Makefile.am =================================================================== --- /trunk/Makefile.am (revision 21) +++ /trunk/Makefile.am (revision 24) @@ -1,12 +1,17 @@ -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 \ @@ -33,7 +38,11 @@ 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: @@ -48,9 +57,5 @@ rpmbuild -ta $(distdir).tar.bz2 -# malloc.c - -# XXX: Standalone version? -# See - # XXX: Non-i386: SPARC asm. x86_64? # Convert this to GNU as format: i386_subr_sol.s +# Index: /trunk/umem_test.c =================================================================== --- /trunk/umem_test.c (revision 22) +++ /trunk/umem_test.c (revision 24) @@ -19,5 +19,5 @@ umem_free(foo, 32); - return 0; + return EXIT_SUCCESS; } Index: /trunk/umem_test4 =================================================================== --- /trunk/umem_test4 (revision 24) +++ /trunk/umem_test4 (revision 24) @@ -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 Index: /trunk/malloc.c =================================================================== --- /trunk/malloc.c (revision 2) +++ /trunk/malloc.c (revision 24) @@ -415,2 +415,8 @@ return (buf); } + +void __attribute__((constructor)) +__malloc_umem_init (void) +{ + umem_startup(NULL, 0, 0, NULL, NULL); +} Index: /trunk/umem.spec.in =================================================================== --- /trunk/umem.spec.in (revision 23) +++ /trunk/umem.spec.in (revision 24) @@ -17,4 +17,5 @@ BuildRequires: binutils BuildRequires: make +BuildRequires: mktemp Index: /trunk/configure.ac =================================================================== --- /trunk/configure.ac (revision 16) +++ /trunk/configure.ac (revision 24) @@ -1,4 +1,14 @@ 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 Index: /trunk/umem_test3.c =================================================================== --- /trunk/umem_test3.c (revision 24) +++ /trunk/umem_test3.c (revision 24) @@ -0,0 +1,14 @@ +#include +#include +#include + +int +main (void) +{ + char *p; + + p = malloc(10); + free(p); + + return EXIT_SUCCESS; +} Index: /trunk/init_lib.c =================================================================== --- /trunk/init_lib.c (revision 2) +++ /trunk/init_lib.c (revision 24) @@ -45,4 +45,7 @@ #endif +#include +#include + void vmem_heap_init(void) @@ -90,5 +93,39 @@ 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) @@ -102,3 +139,5 @@ return (1); #endif + +#endif /* linux */ } Index: /trunk/.cvsignore =================================================================== --- /trunk/.cvsignore (revision 21) +++ /trunk/.cvsignore (revision 24) @@ -19,4 +19,5 @@ umem_test umem_test2 +umem_test3 Doxyfile umem.spec