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()アロケータは、割り当てたバイト数をグローバル マップに記録します。それでも、この情報を特定のコンテナー インスタンスに簡単に関連付ける方法がわかりません。アロケーター オブジェクトは自分がどのコンテナーに属しているかを認識していないためです。

正直なところ、デバッグ情報を収集するだけなら、非 static を定義してsize_t allocatedaccounting_allocator::allocate()統計をファイルまたは stdout に出力する方がおそらく簡単です。または、開発しているプラ​​ットフォーム用のメモリ プロファイラ ツールの使用を検討してください。

于 2009-10-27T11:07:15.343 に答える
0

「staticsize_tassigned」の宣言をクラス定義に入れます。すべてのテンプレートのインスタンス化には、このテンプレートのすべてのオブジェクト間で共有される個別のカウンターがあります。

于 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 に答える