Use OpenBSD malloc, and realloc for free in my program

I would like to use the OpenBSD implementation of malloc, realloc and free on the Debian lenny desktop, not glibc.

  • They just replace: will they work on my Linux desktop?

  • What are the files (files) that I need, and which OpenBSD package contains them?

+6
c linux malloc bsd openbsd
source share
4 answers

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

I don't know if there is anything special about the OpenBSD version that would prevent this. However, if it is malloc and some other standard library materials, it is probably more complicated.

+1
source share

Technically, it is well-ported because it uses mmap(2) , but you cannot just copy and paste.

For reference:

Files:

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, the pair determines:

PGSHIFT , which should be log2 of your system page size. And MADV_FREE , a flag that AFAICT is not available on Linux.

Of course, thread cutting code needs to be completely replaced.

+3
source share

Here: .

You may need to use some dependencies.

+1
source share

You can use it as if you were other (1) replacing (2) malloc() subsystems.

In the first example, malloc() usually replaced by:

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

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

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

I recommend that you make the first option, create a static / shared object named bsdmalloc and associate it with it as desired.

You also have the option of simply creating ball malloc routines with your code, like any other module (a rough 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 systematic approach, I recommend switching to the general route of the object.

0
source share

All Articles