How to modify the malloc behavior in glibc and test if it works as expected?

青春壹個敷衍的年華 提交于 2020-07-23 06:23:34

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/62824113/how-to-modify-the-malloc-behavior-in-glibc-and-test-if-it-works-as-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!