【发布时间】:2014-08-28 18:44:24
【问题描述】:
在文件 gperftools-2.2.1/src/gperftools/malloc_extension.h 中,内容如下:
// Extra extensions exported by some malloc implementations. These
// extensions are accessed through a virtual base class so an
// application can link against a malloc that does not implement these
// extensions, and it will get default versions that do nothing.
//
// NOTE FOR C USERS: If you wish to use this functionality from within
// a C program, see malloc_extension_c.h.
我的问题是如何通过虚拟基类访问这些扩展?
通常要从动态库加载一个类,我需要编写一个基类,它允许我通过多态性获取所需类的实例及其函数,如here 所述。
但是,API 中必须有一些可用的类工厂函数,但在任何 tcmalloc 文件中都没有此类函数。此外,我还需要使用 dlopen() 加载 tcmalloc 库,根据安装说明不建议这样做:
...通过 dlopen 加载 malloc 替换库是 在任何情况下都自找麻烦:有些数据将分配给一个 malloc,有些则分配给另一个。
因此,显然不能通过上述典型方式访问扩展。我可以使用 malloc_extensions_c.h 中声明的 C 版本,但想知道是否有更好的解决方案。
【问题讨论】:
标签: c++ c linux malloc tcmalloc