c - 有没有办法知道在 Linux 中传递给 __free_hook 的指针的大小?

标签 c linux malloc hook free

我想跟踪一个大型应用程序当前分配了多少内存。

我发现我可以在 malloc/free/realloc 周围安装钩子(Hook)以拦截内存分配调用:

http://man7.org/linux/man-pages/man3/malloc_hook.3.html

所以我想跟踪的是分配的总字节数 - 释放的总字节数。

现在的问题是 free 只需要一个指针而不是一个大小。

可以在我的 malloc Hook 中创建我自己的映射或 HashMap ,跟踪为该指针分配了多少内存,但这会导致相当多的开销。

在调用 free 时(使用默认的 g++ malloc)在 Linux(64 位)上有什么方法(即使有点 hack)获得 ptr 的大小?

最佳答案

linux man malloc_usable_size

malloc_usable_size() returns the number of bytes available in the dynamically allocated buffer ptr, which may be greater than the requested size (but is guaranteed to be at least as large, if the request was successful). Typically, you should store the requested allocation size rather than use this function.

关于c - 有没有办法知道在 Linux 中传递给 __free_hook 的指针的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15368093/

相关文章:

c 的自定义 malloc 函数

c - 为什么我不能做 strcpy?

c - C 左移运算中的模行为

python - 如何删除文件和文件夹但保留目录结构并留下一个空文件而不是文件本身?

linux - 对于任何 Linux Distro 的打包系统,找到可同时安装的包的最大数量

c - 尝试进行内存分配时出现访问冲突错误

c++ - Eclipse:无法运行程序 "make"(在目录 "[path is given]"中)

c - 为什么不能在 main 启动之前调用 gsl_vector_alloc ?

linux - 了解 linux 内核和补丁发布

C malloc 字符串