views:

212

answers:

4

I would like to use OpenBSD's implementation of malloc, realloc and free on my Debian lenny desktop rather than glibc's.

  1. Are they simply drop in replacements: will they work on my Linux desktop ?

  2. Which are the file(s) that I need and which OpenBSD package contains them ?

A: 

Google has their own malloc replacement library at http://code.google.com/p/google-perftools/wiki/GooglePerformanceTools with instructions for using it. They say all you need to do is link it in (before the standard version is linked in) to use it.

I do not know if there is anything special about the OpenBSD version that would prevent this. If it is malloc and some other standard library stuff together it is likely more difficult, though.

nategoose
It IS special - take a look: http://www.openbsd.org/papers/eurobsdcon2009/otto-malloc.pdf
Nikolai N Fetissov
That is very interesting, but that's not the kind of special I was referring to. I was referring to dependencies or inclusion of versions of things that clashed with Linux's glibc. If OpenBSD's malloc needed to get it's hash function from OpenBSD's libc but that wasn't in Linux's glibc there would be problems. If something in glibc were to somehow rely on malloc being implemented in a certain way (which would be dumb) there would be problems. If building OpenBSD's malloc library without all of other libc stuff such as stdio is not easy, there could be problems. Thanks for the link.
nategoose
BTW, I know that most of the potential problems I mentioned probably don't exist. They were just possibilities.
nategoose
+1  A: 

Here: http://www.openbsd.org/cgi-bin/cvsweb/src/lib/libc/stdlib/malloc.c.

You might have to bring in some dependencies though.

Nikolai N Fetissov
+2  A: 

Technically it is perfectly portable as it uses mmap(2), but you can't just copy&paste.

For reference:

The files are:

http://www.openbsd.org/cgi-bin/cvsweb/src/lib/libc/stdlib/malloc.c

http://www.openbsd.org/cgi-bin/cvsweb/src/lib/libc/crypt/arc4random.c

,

http://www.openbsd.org/cgi-bin/cvsweb/~checkout~/src/lib/libc/include/thread_private.h

Plus a pair of defines:

PGSHIFT which must be the log2 of your system's page size. And MADV_FREE, a flag which AFAICT is not available in Linux.

The threading code needs complete replacement, of course.

jbcreix
A: 

You could use it like you would other (1) replacement (2) malloc() subsystems.

In the first example, malloc() is generally replaced via:

#define malloc(n) GC_malloc(n)
#define calloc(m,n) GC_malloc((m)*(n))
...
#define free(n) GC_free(n)

You then link against the new malloc() library (statically or dynamically).

In the second example, LD_PRELOAD is used to intercept calls to malloc() / free().

What I recommend you do is the first option, create a static / shared object called bsdmalloc and link against it as desired.

You also have the option of just building the BSD malloc routines with your code, just like you would any other module (crude example including only stdlib where malloc is prototyped) :

#include <stdlib.h>

#define malloc(n) BSD_malloc(n)

void *BSD_malloc(int n)
{
        return NULL;
}


int main(void)
{
   char *ret;

   ret = (char *) malloc(1024);

   return ret == NULL ? 1 : 0;
}

For a more system wide approach, I really recommend going the shared object route.

Tim Post