writing allocator for STL maps

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mavrik
    New Member
    • Jan 2007
    • 12

    writing allocator for STL maps

    Hi,
    I've written allocator for STL vectors, which take all memory from my heap.
    But I don't have any idea to implement the same for Maps.

    As maps take Key and Values. So should I use the same allocator for both data types, or the allocator for map should ahve different signature???
    -Thanks
    -Mavrik
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Erm, I don't think this should be necessary, by default the STL containers allocate their memory from the program heap.

    Comment

    • mavrik
      New Member
      • Jan 2007
      • 12

      #3
      I know that default allocators take memory from heap.
      But my requirement is to create a pool of memory and all allocations of memory should be done from that memory pool.
      I've created memory pool for all type of data types, and I am using that memory for all allocations of vector, as I've overriden new and delete in allocator of vector.
      Now I want to do the same for maps.
      So I am not sure how to do that??
      Any Idea??
      -Thanks

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        The allocator is a defaulted template argument. You should be able to create your map by overriding this default.

        Comment

        • mavrik
          New Member
          • Jan 2007
          • 12

          #5
          Maps take key and value as pair type.
          So how should I allocate memory for both types(kwy and value ) from my allocator which takes just one typename as template.

          -Thanks

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            The map template looks like:
            Originally posted by MSDN
            template <
            class Key,
            class Type,
            class Traits = less<Key>,
            class Allocator=alloc ator<pair <const Key, Type> >
            >
            class map
            There are actually four template parameters. The first two are for your pair. The third is the ordering function pointer (which you cna specify if you don't like the default) and the fourth is your allocator.

            Comment

            • mavrik
              New Member
              • Jan 2007
              • 12

              #7
              Thank u all for ur replies...
              I still have one more query..

              I am using my allocator for vectors as follows.
              vector<int myAllocator<int >> v1;

              now to use the same allocaor for maps, I should do as follows...
              map<int , long , less<int> , myAllocator<pai r<int,long> > >,
              as pair is a tyoe now..
              Is that right???
              If yes... then allocate() function of my allocator does not recognise pair as a data type. Rather it considers it as map.
              And allocate memory for sizeof(map), not actually sizeof(int) + sizeof(long);
              If I am clear in communicating my problem, can you suggest something??
              It has become a real problem for me now...
              Thanks

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                It should work this way:
                [code=cpp]
                int main()
                {
                map<int , long , less<int> , myAllocator<pai r<int, long> > > myMap;
                vector<int, myAllocator<int > > myVector;
                }
                [/code]

                Your allocator is to allocate memory for the type you need. In the case of the map you need memory for a pair. In the case of the vector, you need memory for an int.

                What I did was take the standard allocator and modify it to myAllocator sso the above code would compile. Here is what I did:
                [code=cpp]
                template <class T>
                class myAllocator
                {
                public:
                typedef size_t size_type;
                typedef ptrdiff_t difference_type ;
                typedef T* pointer;
                typedef const T* const_pointer;
                typedef T& reference;
                typedef const T& const_reference ;
                typedef T value_type;
                template <class U>
                struct rebind
                {
                typedef myAllocator<U> other;
                };

                myAllocator () throw();
                myAllocator (const myAllocator&) throw ();
                template <class U>
                myAllocator(con st myAllocator<U>& ) throw();
                template <class U>
                myAllocator& operator=(const myAllocator<U>& ) throw();
                ~myAllocator () throw();
                pointer address(referen ce) const;
                const_pointer address(const_r eference) const;
                pointer allocate (size_type, typename myAllocator<T>: :const_pointer = 0);
                void deallocate(poin ter p, size_type n);
                size_type max_size() const throw();
                void construct(point er, const T&);
                void destroy(pointer );

                };

                // globals
                template <class T, class U>
                bool operator==(cons t myAllocator<T>& ,
                const myAllocator<U>& ) throw();

                template <class T, class U>
                bool operator!=(cons t myAllocator<T>& ,
                const myAllocator<U>& ) throw();
                int main()
                {
                map<int , long , less<int> , myAllocator<pai r<int, long> > > myMap;
                vector<int, myAllocator<int > > myVector;
                }
                [/code]

                This compile fine but dies in the link since the methods aren't written.

                Allocators have rules and you can get a peek at them by getting a book Effective STL by Scott Meyers and reading items 10 and 11.

                One essential on the allocate method is to be sure the arugment is for the number of objects requiring memory and not the number of bytes.

                Comment

                Working...