在我的程序中使用 OpenBSD 的 malloc、realloc 和 free
我想在我的 Debian lenny 桌面上使用 OpenBSD 的 malloc、realloc 和 free 实现,而不是 glibc。
它们是否只是简单地替换:它们可以在我的 Linux 桌面上运行吗?
我需要哪些文件以及哪个 OpenBSD 软件包包含它们?
I would like to use OpenBSD's implementation of malloc, realloc and free on my Debian lenny desktop rather than glibc's.
Are they simply drop in replacements: will they work on my Linux desktop ?
Which are the file(s) that I need and which OpenBSD package contains them ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从技术上讲,它是完全可移植的,因为它使用
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. AndMADV_FREE
, a flag which AFAICT is not available in Linux.The threading code needs complete replacement, of course.
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.
这里:
< /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.
您可以像其他一样使用它 (1) 替换 (2)
malloc()
子系统。在第一个示例中,
malloc()
通常通过以下方式替换:然后链接到新的 malloc() 库(静态或动态)。
在第二个示例中,
LD_PRELOAD
用于拦截对malloc()
/free()
的调用。我建议您做的是第一个选项,创建一个名为
bsdmalloc
的静态/共享对象,并根据需要链接到它。您还可以选择使用代码构建 BSD malloc 例程,就像任何其他模块一样(粗略的示例仅包括 stdlib,其中 malloc 是原型):
对于更系统范围的方法,我真的建议采用共享对象路线。
You could use it like you would other (1) replacement (2)
malloc()
subsystems.In the first example,
malloc()
is generally replaced via:You then link against the new malloc() library (statically or dynamically).
In the second example,
LD_PRELOAD
is used to intercept calls tomalloc()
/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) :
For a more system wide approach, I really recommend going the shared object route.