5

我正在使用自定义分配器来解决多个容器中的内存使用情况。目前我使用静态变量来说明内存使用情况。我怎样才能在多个容器中分离这个帐户,而不必重写分配器以使用不同的静态变量?


static size_t allocated = 0;


   template <class T>
   class accounting_allocator {
     public:
       // type definitions
       typedef T        value_type;
       typedef T*       pointer;
       typedef const T* const_pointer;
       typedef T&       reference;
       typedef const T& const_reference;
       typedef std::size_t    size_type;
       typedef std::ptrdiff_t difference_type;
       //static size_t allocated;

       // rebind allocator to type U
       template <class U>
       struct rebind {
           typedef accounting_allocator<U> other;
       };

       // return address of values
       pointer address (reference value) const {
           return &value;
       }
       const_pointer address (const_reference value) const {
           return &value;
       }

       /* constructors and destructor
        * - nothing to do because the allocator has no state
        */
       accounting_allocator() throw() {
       }
       accounting_allocator(const accounting_allocator&) throw() {
       }
       template <class U>
         accounting_allocator (const accounting_allocator<U>&) throw() {
       }
       ~accounting_allocator() throw() {
       }

       // return maximum number of elements that can be allocated
       size_type max_size () const throw() {
        //  std::cout << "max_size()" << std::endl;
           return std::numeric_limits<std::size_t>::max() / sizeof(T);
       }

       // allocate but don't initialize num elements of type T
       pointer allocate (size_type num, const void* = 0) {
           // print message and allocate memory with global new
           //std::cerr << "allocate " << num << " element(s)" << " of size " << sizeof(T) << std::endl;
           pointer ret = (pointer)(::operator new(num*sizeof(T)));
           //std::cerr << " allocated at: " << (void*)ret << std::endl;
           allocated += num * sizeof(T);
            //std::cerr << "allocated: " << allocated/(1024*1024) << " MB" << endl;
           return ret;
       }

       // initialize elements of allocated storage p with value value
       void construct (pointer p, const T& value) {
           // initialize memory with placement new
           new((void*)p)T(value);
      }

       // destroy elements of initialized storage p
       void destroy (pointer p) {
           // destroy objects by calling their destructor
           p->~T();
       }

       // deallocate storage p of deleted elements
       void deallocate (pointer p, size_type num) {
           // print message and deallocate memory with global delete
#if 0
           std::cerr << "deallocate " << num << " element(s)"
                     << " of size " << sizeof(T)
                     << " at: " << (void*)p << std::endl;
#endif
           ::operator delete((void*)p);
           allocated -= num * sizeof(T);
       }
   };
  template<>
    class accounting_allocator<void>
    {
    public:
      typedef size_t      size_type;
      typedef ptrdiff_t   difference_type;
      typedef void*       pointer;
      typedef const void* const_pointer;
      typedef void        value_type;

      template<typename _Tp1>
        struct rebind
        { typedef allocator<_Tp1> other; };
    };


   // return that all specializations of this allocator are interchangeable
   template <class T1, class T2>
   bool operator== (const accounting_allocator<T1>&,
                    const accounting_allocator<T2>&) throw() {
       return true;
   }
   template <class T1, class T2>
   bool operator!= (const accounting_allocator<T1>&,
                    const accounting_allocator<T2>&) throw() {
       return false;
   }
4

3 回答 3

6

如果您的意思是每个容器类型都需要一个单独的计数器,您可以简单地将容器类型包含为模板参数并取消注释static size_t allocated,使其成为静态成员变量。这样,将为每种类型的容器生成一个单独的计数器变量。

如果您说要为容器的每个实例使用单独的计数器,则需要创建size_t allocated一个非静态成员变量。问题是,您还需要某种钩子,以便您可以从每个容器外部访问分配计数器。STL 分配器设计使其很难做到这一点。一些 STL 容器有一个构造函数,可以让你传递一个分配器的实例,但并不是所有的容器都支持这个。在支持这一点的容器上,您可以在分配器类中包含对某个全局映射的引用,然后将分配器的实例传递给每个容器的构造函数。然后,当你打电话accounting_allocator::allocate(),分配器将记录它在全局映射中分配的字节数。不过,我看不出如何轻松地将这些信息与特定容器实例相关联,因为分配器对象不知道它属于哪个容器。

老实说,如果您只是收集调试信息,那么定义一个非静态可能更容易size_t allocated,并且accounting_allocator::allocate()只需将统计信息输出到文件或标准输出。或者,考虑为您开发的平台使用内存分析器工具。

于 2009-10-27T11:07:15.343 回答
0

将“静态 size_t 已分配”的声明放入类定义中。每个模板实例化都会有一个单独的计数器在该模板的所有对象之间共享。

于 2009-10-27T09:51:50.647 回答
0

查看我的代码示例:

// uintptr_t represents an object address
// as a numerical value.
// you could use unsigned long insead if 
// sizeof(long) == sizeof(void*) on your system.
struct AllocCounter {
    static size_t *Register(uintptr_t uContainer)
    {
        // insert container address into map, and
        // return an associated allocation counter.
    }
    static bool Unregister(uintptr_t uContainer)
    {
        // remove container address and the 
        // associated allocation counter from the map
    }
    static void DebugCounter(void)
    {
        // statistic of all container objects.
    }
protected:
    static hash_map<uintptr_t, size_t> m_aCounter;
};

此外,您可以通过增强上述 AllocCounter 将容器或对象类名称等与分配计数器相关联。

还有一个容器示例:

class Container 
{
public:
    Container(void) 
    {
        m_pAllocCounter = AllocCounter::Register((uintptr_t)this);
        ....
    }
    ~Container()
    {
        AllocCounter::Unregister((uintptr_t)this);
    }
    pointer ObjectAllocate(void)
    {
        pointer obj;
        *m_pAllocCounter += sizeof *obj;
        obj = new CObject;
        return obj;
    }
    void ObjectDealloc(pointer pObj)
    {
        *m_pAllocCounter -= sizeof *pObj;
        delete pObj;
    }
    ....

private:
    size_t *m_pAllocCounter;
    ....
};
于 2009-10-27T12:02:47.083 回答