Add --enable-malloc-replacement option, for using libumem as a malloc replacement

This commit is contained in:
Richard Dawe 2006-09-03 12:48:23 +00:00
parent e7140b1a1a
commit 29bf7f3e03
9 changed files with 111 additions and 12 deletions

View file

@ -18,6 +18,7 @@ stamp-h.in
stamp-h1
umem_test
umem_test2
umem_test3
Doxyfile
umem.spec
*.tar.gz

View file

@ -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/>

View file

@ -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

View file

@ -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 */
}

View file

@ -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);
}

View file

@ -16,6 +16,7 @@ BuildRequires: doxygen
BuildRequires: gcc
BuildRequires: binutils
BuildRequires: make
BuildRequires: mktemp
%description

View file

@ -18,6 +18,6 @@ int main(int argc, char *argv[])
umem_free(foo, 32);
return 0;
return EXIT_SUCCESS;
}

14
umem_test3.c Normal file
View file

@ -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;
}

23
umem_test4 Executable file
View file

@ -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