如何在glibc中修改malloc行为并测试其是否按预期工作? [英] How to modify the malloc behavior in glibc and test if it works as expected?

查看:131
本文介绍了如何在glibc中修改malloc行为并测试其是否按预期工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用malloc分配内存,但是我不想在malloc中使用动态分配方式.相反,我想使用malloc分配一个大的块/池,以便我可以新建"一个块/池.一些变量/对象进入这个大内存块/池.因此,我想修改glibc中的malloc源代码(例如malloc/malloc.c)以使其适合我的方案.

修改glibc源代码并测试其功能的最便捷,最有效的方法是什么?我想到的一种方法是下载glibc源代码,修改代码,然后进行configure/make/make install,但这将非常麻烦,因为我必须经常测试修改后的malloc版本以确保其正常运行

非常感谢您的帮助!所有的答案和想法都受到赞赏.

解决方案

您可以在您的代码中使用LD_PRELOAD (假设它是动态链接的)以插入您自己的malloc实现.

您想要的内容听起来很像竞技场分配.该库可能已经满足您的需求: https://github.com/thejefflarson/arena

I want to use malloc to allocate memory, but I don't want to use a dynamic allocation way in malloc. Instead, I want to use malloc to allocate a large block/pool so that I can "new" some variables/objects into this large memory block/pool. Therefore, I would like to modify the malloc source code (e.g., malloc/malloc.c) in glibc to make it work for my scenario.

What is the most convenient and efficient way to modify glibc source codes and also test its functionality? One way that comes to my mind is to download the glibc source, modify the code, and then configure/make/make install, but it would be very cumbersome because I have to test my modified version of malloc frequently to make sure it works properly.

Thank you so much for your kind help! All the answers and thoughts are appreciated.

解决方案

You can use LD_PRELOAD with your code (assuming it is dynamically linked) to insert your own malloc implementation.

What you want sounds a lot like Arena Allocation. This library might already do what you need: https://github.com/thejefflarson/arena

这篇关于如何在glibc中修改malloc行为并测试其是否按预期工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆