Community
cancel
Showing results for 
Search instead for 
Did you mean: 
54 Views

a custom allocator for the tbb::concurrent_unordered_map

I am trying to provide my allocator for the tbb::concurrent_unordered_map to do a custom action on the value, but the destroy method of the allocator receives a pointer of the flist_iterator type which has the protected my_node_ptr variable. So I can not access it.

What is the right way to provide a custom action on the destroy? (besides modifying the tbb source code)

Note: the value is a pointer and I can not wrap it into a struct as value, I need to have my allocator.

0 Kudos
3 Replies
54 Views

Hello, Andrew,

The simplest thing I could suggest is to walk the list before destruction, freeing the pointed-to memory.  I think that is what you'd have to do if it was a std::vector of pointers, or other standard library container.

Do I understand your description right?  You are asking if there is a way to insert pointers you have allocated into a concurrent_unordered_map, and when the map is destroyed, have all the pointers you inserted freed first by the map destructor?

Regards,
Chris

54 Views

Hi Chris,

Yes, when the clear method is called, I need to deallocate all values. And I am trying to avoid a redundant walk since the concurrent_unordered_map goes through all values anyway. I have a lot of stuff in memory so I am trying to make it more efficient.

54 Views

Here is how to provide a custom action on the destroy:

[cpp]template<typename T>
struct myalloc: tbb::tbb_allocator<T> {
   template<typename U>
   struct rebind {
      typedef tbb::tbb_allocator<U>other; // OK
      // typedef myalloc<U> other; // ERROR
   };
   myalloc() throw() {}
   myalloc(const myalloc&) throw() {}
   template<typename U> myalloc(const myalloc<U>&) throw() {}
   void destroy(pointer p) {
      delete (**p).second; // custom action
      p->~value_type();
   }
};[cpp]