473,395 Members | 1,680 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

writing allocator for STL maps

12
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
Mar 16 '08 #1
7 11407
Banfa
9,065 Expert Mod 8TB
Erm, I don't think this should be necessary, by default the STL containers allocate their memory from the program heap.
Mar 17 '08 #2
mavrik
12
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
Mar 17 '08 #3
weaknessforcats
9,208 Expert Mod 8TB
The allocator is a defaulted template argument. You should be able to create your map by overriding this default.
Mar 17 '08 #4
mavrik
12
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
Mar 17 '08 #5
weaknessforcats
9,208 Expert Mod 8TB
The map template looks like:
template <
class Key,
class Type,
class Traits = less<Key>,
class Allocator=allocator<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.
Mar 18 '08 #6
mavrik
12
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<pair<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
Mar 20 '08 #7
weaknessforcats
9,208 Expert Mod 8TB
It should work this way:
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     map<int , long , less<int> , myAllocator<pair<int, long> > > myMap;
  4.     vector<int, myAllocator<int> > myVector;
  5. }    
  6.  
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:
Expand|Select|Wrap|Line Numbers
  1. template <class T>
  2.   class myAllocator
  3.   {
  4.     public:
  5.         typedef size_t            size_type;
  6.         typedef ptrdiff_t         difference_type;
  7.         typedef T*                pointer;
  8.         typedef const T*          const_pointer;
  9.         typedef T&                reference;
  10.         typedef const T&          const_reference;
  11.         typedef T                 value_type;      
  12.         template <class U>    
  13.         struct rebind
  14.         {
  15.           typedef myAllocator<U> other;
  16.         };
  17.  
  18.    myAllocator () throw();
  19.    myAllocator (const myAllocator&) throw ();
  20.     template <class U> 
  21.     myAllocator(const myAllocator<U>&) throw();
  22.    template <class U> 
  23.    myAllocator& operator=(const myAllocator<U>&) throw();
  24.     ~myAllocator () throw();
  25.     pointer address(reference) const;
  26.     const_pointer address(const_reference) const;
  27.     pointer allocate (size_type, typename myAllocator<T>::const_pointer = 0);
  28.     void deallocate(pointer p, size_type n); 
  29.     size_type max_size() const throw();
  30.     void construct(pointer, const T&);
  31.     void destroy(pointer);
  32.  
  33.   };
  34.  
  35.   // globals
  36.   template <class T, class U>
  37.   bool operator==(const myAllocator<T>&, 
  38.                   const myAllocator<U>&) throw();
  39.  
  40.   template <class T, class U>
  41.   bool operator!=(const myAllocator<T>&, 
  42.                   const myAllocator<U>&) throw();
  43.  int main()
  44. {
  45.     map<int , long , less<int> , myAllocator<pair<int, long> > > myMap;
  46.     vector<int, myAllocator<int> > myVector;
  47. }    
  48.  
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.
Mar 20 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Scott Brady Drummonds | last post by:
Hi, everyone, A coworker and I have been pondering a memory allocation problem that we're having with a very large process. Our joint research has led us to the conclusion that we may have to...
3
by: Bernhard Kick | last post by:
Hi all, I saw this code in the book "Accelerated C++" (chapt 11, iirc): template <class T> class Vec { ... std::allocator<T> alloc; // object to handle memory allocation // ??would static...
13
by: John Harrison | last post by:
If you specify an allocator in an STL container is it a requirement that the allocator allocates object of the right type, or can you assume that the container will rebind the allocator to the...
7
by: Grahamo | last post by:
Hi, can anybody tell me where I can get the boiler plate code for std::allocator. I need to have my version of new and delete called and want to get reference code. My compilers headers are all...
3
by: Protoman | last post by:
Why won't the compiler let me write this: template <class T> class Allocator { public: Allocator(){} T* allocate(){static long long i=0; T* j=new(pool) T; i++; return &*j;} private:
6
by: mavrik | last post by:
Hi All, I've created STL allocator which creates memory pool in the beginning, and all maps and vectors will take memory from that memory pool. But...I am observing that application has become...
6
by: Juha Nieminen | last post by:
I tested the speed of a simple program like this: //------------------------------------------------------------ #include <list> #include <boost/pool/pool_alloc.hpp> int main() { typedef...
16
by: PeterAPIIT | last post by:
Hello all C++ expert programmer, i have wrote partial general allocator for my container. After reading standard C++ library and code guru article, i have several questions. 1. Why...
12
by: David Given | last post by:
I have a situation where I need to be able to allocate chunks on unmapped virtual memory. Because the memory I'm allocating isn't mapped, I can't use a normal memory allocator, as most of them...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.