Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Writing a std::...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 1 Topic 9496 of 9593
Post > Topic >>

Writing a std::allocator for a map<>

by markus2004x@[EMAIL PROTECTED] Apr 13, 2008 at 12:48 PM

I am trying to write an allocator compatible with std::map.  I am
using stlport 5.1 and VS 2003.

I get an obscure compile error message:

stlpd_std::priv::_STLP_alloc_proxy<_Value,_Tp,_MaybeReboundAlloc>::_STLP_alloc_proxy(const
_MaybeReboundAlloc &,_Value)' : cannot convert parameter 1 from 'const
stlpd_std::priv::_Rb_tree_base<_Tp,_Alloc>::allocator_type' to 'const
stlpd_std::priv::_Rb_tree_base<_Tp,_Alloc>::_M_node_allocator_type &,
with ....

By the way, if I use Dimkumware, my allocator works just fine.

Does anybody have any suggestion? Thanks a lot

Below I attach my allocator code:

                // ******** Class description
**********************************************
	// Non-thread-safe standard allocator, which allocates big chunks of
memory
	// and than allocates small objects from this large chunk of memory.
	// Only 1 object at a type can be allocated
	//

	template<class T>
	class AllocatorPool1NTS
	{
	public :
		//    typedefs
		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;

	//private:

		class pool1
		{
		public:
			//! constructor
			pool1(unsigned objectsPerBlock = 128u ) :
objectsPerBlock_(objectsPerBlock) {}

			//! destructor
			~pool1()
			{
				for_each( memBlocks_.begin(), memBlocks_.end(), &::free );
			}

			//! set n objects per block
			void setObjectsPerBlock( unsigned n ) { objectsPerBlock_ = n; }

			//! read n objects per block
			unsigned getObjectsPerBlock() const { return objectsPerBlock_; }

			//! Return memory address of next available object
			pointer allocate1()
			{
				if ( availableBlocks_.empty() ) {
					// std::cout << "Creating block" << std::endl;
					pointer newBlock = (pointer) malloc( sizeof( value_type ) *
objectsPerBlock_ );
					MY_CHECK( newBlock != 0, "Could not allocate memory" );
					memBlocks_.push_back( newBlock );
					pointer end = newBlock + objectsPerBlock_;
					do {
						availableBlocks_.push( newBlock );
					} while( ++newBlock != end );
				}

				// std::cout << "Get from pool of " << availableBlocks_.size() <<
std::endl;
				pointer p = availableBlocks_.top();
				availableBlocks_.pop();
				return p;
			}

			//! Reinsert object in the pool
			void deallocate1( pointer p )
			{
				//std::cout << "Returning to pool of " << availableBlocks_.size()
<< std::endl;
				availableBlocks_.push( p );
			}

		private:
			unsigned objectsPerBlock_;
			std::stack< pointer, std::vector<pointer> > availableBlocks_;
			std::vector< pointer > memBlocks_;
		};


	public :


		inline explicit AllocatorPool1NTS() : pool_( new pool1 ) {}

		inline explicit AllocatorPool1NTS( const AllocatorPool1NTS& a ) :
pool_( a.pool_ ) {}

		inline AllocatorPool1NTS& operator=( const AllocatorPool1NTS& a )
{ pool_ = a.pool_; }

		//template<typename U>
		//inline explicit AllocatorPool1NTS(const AllocatorPool1NTS<U> &
a ) : objectsPerBlock_( a.objectsPerBlock_ ) {}

		template<class U>
		struct rebind {
			typedef AllocatorPool1NTS<U> other;
		};

		inline ~AllocatorPool1NTS() {}

		inline void setObjectsPreBlock( unsigned n ) { pool_-
>setObjectsPerBlock( n ); }

		//    address
		inline pointer address(reference r) { return &r; }
		inline const_pointer address(const_reference r) { return &r; }

		//  memory allocation
		inline pointer allocate(size_type cnt, typename
std::allocator<void>::const_pointer = 0)
		{
			MY_ASSERT( cnt == 1, "Only one object at a time can be allocated
" );
			return pool_->allocate1();
		}

		//	memory deallocation
		inline void deallocate(pointer ptr, size_type cnt)
		{
			//std::cout << "Returning to pool of " << free_.size() <<
std::endl;
			MY_ASSERT( cnt == 1, "Only one object at a time can be deallocated
" );
			pool_->deallocate1();
		}

		// size
		inline size_type max_size() const
		{
			return std::numeric_limits<size_type>::max() / (sizeof(T)
+sizeof(pointer));
		}

		//    construction/destruction
		inline void construct(pointer p, const T& t) { new(p) T(t); }
		inline void destroy(pointer p) { p->~T(); }

		inline bool operator==(AllocatorPool1NTS const& a) { return
pool_.get() == a.pool_.get(); }
		inline bool operator!=(AllocatorPool1NTS const& a) { return !
operator==(a); }

	public:
		unsigned objectsPerBlock_;

	private:
		boost::shared_ptr<pool1> pool_;
	};    //    end of class AllocatorPool1NTS


-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]




 1 Posts in Topic:
Writing a std::allocator for a map<>
markus2004x@[EMAIL PROTEC  2008-04-13 12:48:37 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu May 15 0:32:47 CDT 2008.