在我的程序中使用 OpenBSD 的 malloc、realloc 和 free

发布于 2024-09-02 21:26:20 字数 186 浏览 11 评论 0原文

我想在我的 Debian lenny 桌面上使用 OpenBSD 的 malloc、realloc 和 free 实现,而不是 glibc。

  1. 它们是否只是简单地替换:它们可以在我的 Linux 桌面上运行吗?

  2. 我需要哪些文件以及哪个 OpenBSD 软件包包含它们?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

西瓜 2024-09-09 21:26:20

从技术上讲,它是完全可移植的,因为它使用 mmap(2),但您不能只是复制和粘贴。

供参考:

这些文件是:

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 openbsd.org/cgi-bin/cvsweb/~checkout~/src/lib/libc/include/thread_private.h

再加上一对定义:

PGSHIFT,它必须是您的 log2系统的页面大小。还有 MADV_FREE,AFAICT 在 Linux 中不可用。

当然,线程代码需要完全替换。

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.

妳是的陽光 2024-09-09 21:26:20

Google 有自己的 malloc 替换库
http://code.google.com/p/google-perftools/wiki/ Google 性能工具
以及使用说明。他们说您需要做的就是链接它(在链接标准版本之前)来使用它。

我不知道 OpenBSD 版本是否有什么特殊之处可以防止这种情况发生。如果它是 malloc 和其他一些标准库的东西在一起,那么可能会更困难。

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.

一笑百媚生 2024-09-09 21:26:20

这里: < /a>http:// www.openbsd.org/cgi-bin/cvsweb/src/lib/libc/stdlib/malloc.c

不过,您可能必须引入一些依赖项。

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

You might have to bring in some dependencies though.

柠栀 2024-09-09 21:26:20

您可以像其他一样使用它 (1) 替换 (2) malloc() 子系统。

在第一个示例中,malloc() 通常通过以下方式替换:

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

然后链接到新的 malloc() 库(静态或动态)。

在第二个示例中,LD_PRELOAD 用于拦截对 malloc() / free() 的调用。

我建议您做的是第一个选项,创建一个名为 bsdmalloc 的静态/共享对象,并根据需要链接到它。

您还可以选择使用代码构建 BSD malloc 例程,就像任何其他模块一样(粗略的示例仅包括 stdlib,其中 malloc 是原型):

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

对于更系统范围的方法,我真的建议采用共享对象路线。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文