有没有办法知道传递给在Linux中__free_hook指针的大小? [英] Is there a way to know size of a pointer passed to a __free_hook in Linux?

查看:317
本文介绍了有没有办法知道传递给在Linux中__free_hook指针的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要跟踪的多少内存目前的大型应用程序分配。

I want to track how much memory is currently allocated by a large application.

我发现我可以安装周围的malloc /免费/ realloc的挂钩,以拦截内存分配调用:

I found that I can install hooks around malloc/free/realloc in order to intercept memory allocation calls:

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

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

So what I want to track is total bytes allocated - total bytes freed.

现在的问题是,自由只需要一个指针,而不是一个大小

Now the problem is that free only takes a pointer and not a size.

在可以建立在我的malloc挂钩,跟踪了多少内存分配给该指针,但引起了不少开销,我自己的地图或HashMap中。

In can create my own map or hashmap in my malloc hook that tracks how much memory was allocated for that pointer but that causes quite a bit of overhead.

有什么办法(哪怕是一个黑客位),以获得在Linux(64位)的PTR的大小自由时被调用(默认G ++的malloc)?

Is there any way (even if it is a bit of a hack) to get on Linux (64 bit) the size of the ptr when free is called (with the default g++ malloc)?

推荐答案

Linux手册malloc_usable_size

linux man malloc_usable_size

malloc_usable_size()返回可用的字节数
  动态分配缓冲器ptr的,这可能比越大
  请求的大小(但被保证是至少一样大,如果
  请求成功)。通常情况下,你应该存储
  请求分配大小,而不是使用此功能。

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.

这篇关于有没有办法知道传递给在Linux中__free_hook指针的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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