如何跟踪由STL库分配的内存 [英] How to track memory assign by STL library

查看:146
本文介绍了如何跟踪由STL库分配的内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想跟踪所有STL容器(如map,list,vector等)分配的所有内存(由std lib分配的大小)。我只想跟踪STL容器,而不是常规对象创建。基本上要覆盖新的和删除std lib。

I want to track all the memory(size allocated by std lib) allocated by all STL containers like map,list,vector etc. I just want to track STL container not regular object creation. Basically want to override new and delete of std lib.

示例

class demo  {
public:
  int i;
  std::list<int> mylist;
}

int main() {
demo dd = new demo(); // -> Don't want to track this. Just want to track
                      //    mylist(size of my list) 
}

我发现std有它自己的分配器选项。例如列表是分配器

I found out that std has it's own allocator option. For example list has it is allocator

template < class T, class Alloc = allocator<T> > class list;

如果我没有定义任何内容,默认分配器是什么。我有一千个列表,然后没有分配器,我不想手动更改每一个。

What is the default allocator if I don't defined anything. I have thousand of list and none of then has allocator and I don't want to change each one of them manually. So, what I was thinking if there is way where I can replace default allocator with mine.

如何做到这一点?

推荐答案

如果你的主要优先级是短期低实现成本(相对于长期可维护性),解决方法之一是包装你的 命名空间中包含 std 命名空间的 outer :: std 中可以重新定义向量列表 ,等等与您的自定义分配器。通过这样做,对 std 命名空间的所有引用将解析为 outer :: std
所需的唯一工作(除了实现您的自定义分配器)是将所有代码包装到外部命名空间,并提供 outer :: std 。这部分可能很容易成为一种痛苦。

If your main priority is short term low implementation cost (as opposed to longer term maintainability), one way to get around it is to wrap your demo class into an outer namespace that contains a std namespace. Inside outer::std you can redefine vector, list, etc. with your custom allocator. By doing so, all references to the std namespace will resolve to outer::std. The only work needed (in addition to implementing your custom allocator) is to wrap all your code into the outer namespace and to provide all the required definitions in outer::std. That part might easily become a pain. Future contributors might suffer.

像这样:

#include <iostream>
#include <vector>
namespace outside
{
    template<typename T>
    struct MyAllocator
    {
    typedef typename std::allocator<T>::value_type value_type;
    typedef typename std::allocator<T>::pointer pointer;
    typedef typename std::allocator<T>::const_pointer const_pointer;
    typedef typename std::allocator<T>::reference reference;
    typedef typename std::allocator<T>::const_reference const_reference;
    typedef typename std::allocator<T>::size_type size_type;
    pointer allocate (size_type n, typename std::allocator<void>::const_pointer hint=0)
    {
        std::cerr << "allocate..." << std::endl;
        return std::allocator<T>{}.allocate(n, hint);
    }
    void deallocate (pointer p, size_type n)
    {
        std::cerr << "deallocate..." << std::endl;
        return std::allocator<T>{}.deallocate(p, n);
    }
    template <class Type> struct rebind
    {
        typedef MyAllocator<Type> other;
    };
    MyAllocator()
    {
    }
    MyAllocator(const MyAllocator<T>& other )
    {
    }
    template< class U >
    MyAllocator( const MyAllocator<U>& other )
    {
    }
};
} // namespace outside

namespace outer
{
namespace std
{
template<class T>
using vector = ::std::vector<T, outside::MyAllocator<T>>;
} // namespace std

class demo
{
public:
    int i;
    std::vector<int> myVector;
};
} // namespace outer

int main()
{
    using outer::demo;
    std::cerr << "creating new demo" << std::endl;
    demo *dd = new demo();
    std::cerr << "resizing dd->myList" << std::endl;
    dd->myVector.resize(10,3);
    delete dd;
}

这篇关于如何跟踪由STL库分配的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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