Home | History | Annotate | Download | only in include
      1 #ifndef	_ANTLR3MEMORY_HPP
      2 #define	_ANTLR3MEMORY_HPP
      3 
      4 // [The "BSD licence"]
      5 // Copyright (c) 2005-2009 Gokulakannan Somasundaram, ElectronDB
      6 
      7 //
      8 // All rights reserved.
      9 //
     10 // Redistribution and use in source and binary forms, with or without
     11 // modification, are permitted provided that the following conditions
     12 // are met:
     13 // 1. Redistributions of source code must retain the above copyright
     14 //    notice, this list of conditions and the following disclaimer.
     15 // 2. Redistributions in binary form must reproduce the above copyright
     16 //    notice, this list of conditions and the following disclaimer in the
     17 //    documentation and/or other materials provided with the distribution.
     18 // 3. The name of the author may not be used to endorse or promote products
     19 //    derived from this software without specific prior written permission.
     20 //
     21 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24 // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26 // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31 
     32 #include <string.h>
     33 
     34 #include <deque>
     35 #include <map>
     36 #include <new>
     37 #include <set>
     38 #include <vector>
     39 
     40 #include   "antlr3defs.hpp"
     41 
     42 ANTLR_BEGIN_NAMESPACE()
     43 
     44 class DefaultAllocPolicy
     45 {
     46 public:
     47 	//limitation of c++. unable to write a typedef
     48 	template <class TYPE>
     49 	class AllocatorType : public std::allocator<TYPE>
     50 	{
     51 	public:
     52 		typedef TYPE value_type;
     53 		typedef value_type* pointer;
     54 		typedef const value_type* const_pointer;
     55 		typedef value_type& reference;
     56 		typedef const value_type& const_reference;
     57 		typedef size_t size_type;
     58 		typedef ptrdiff_t difference_type;
     59 		template<class U> struct rebind {
     60 			typedef AllocatorType<U> other;
     61 		};
     62 
     63 		AllocatorType() throw() {}
     64 		AllocatorType( const AllocatorType& alloc ) throw() {}
     65 		template<typename U> AllocatorType(const AllocatorType<U>& alloc) throw(){}
     66 	};
     67 
     68 	template<class TYPE>
     69 	class VectorType : public std::vector< TYPE, AllocatorType<TYPE> >
     70 	{
     71 	};
     72 
     73 	template<class TYPE>
     74 	class ListType : public std::deque< TYPE, AllocatorType<TYPE> >
     75 	{
     76 	};
     77 
     78 	template<class TYPE>
     79 	class StackType : public std::deque< TYPE, AllocatorType<TYPE> >
     80 	{
     81 	public:
     82 		void push( const TYPE& elem ) {  this->push_back(elem); 	}
     83 		void pop()  { this->pop_back(); }
     84 		TYPE& peek() { return this->back(); }
     85 		TYPE& top() { return this->back(); }
     86 		const TYPE& peek() const { return this->back(); }
     87 		const TYPE& top() const { return this->back(); }
     88 	};
     89 
     90 
     91 	template<class TYPE>
     92 	class OrderedSetType : public std::set< TYPE, std::less<TYPE>, AllocatorType<TYPE> >
     93 	{
     94 	};
     95 
     96 	template<class TYPE>
     97 	class UnOrderedSetType : public std::set< TYPE, std::less<TYPE>, AllocatorType<TYPE> >
     98 	{
     99 	};
    100 
    101 	template<class KeyType, class ValueType>
    102 	class UnOrderedMapType : public std::map< KeyType, ValueType, std::less<KeyType>,
    103 										AllocatorType<std::pair<KeyType, ValueType> > >
    104 	{
    105 	};
    106 
    107 	template<class KeyType, class ValueType>
    108 	class OrderedMapType : public std::map< KeyType, ValueType, std::less<KeyType>,
    109 										AllocatorType<std::pair<KeyType, ValueType> > >
    110 	{
    111 	};
    112 
    113 	ANTLR_INLINE static void* operator new (std::size_t bytes)
    114 	{
    115 		void* p = alloc(bytes);
    116 		return p;
    117 	}
    118 	ANTLR_INLINE static void* operator new (std::size_t , void* p) { return p; }
    119 	ANTLR_INLINE static void* operator new[]( std::size_t bytes)
    120 	{
    121 		void* p = alloc(bytes);
    122 		return p;
    123 	}
    124 	ANTLR_INLINE static void operator delete(void* p)
    125 	{
    126 		DefaultAllocPolicy::free(p);
    127 	}
    128 	ANTLR_INLINE static void operator delete(void* , void* ) {} //placement delete
    129 
    130 	ANTLR_INLINE static void operator delete[](void* p)
    131 	{
    132 		DefaultAllocPolicy::free(p);
    133 	}
    134 
    135 	ANTLR_INLINE static void* alloc( std::size_t bytes )
    136 	{
    137 		void* p = malloc(bytes);
    138 		if( p== NULL )
    139 			throw std::bad_alloc();
    140 		return p;
    141 	}
    142 
    143 	ANTLR_INLINE static void* alloc0( std::size_t bytes )
    144 	{
    145 		void* p = calloc(1, bytes);
    146 		if( p== NULL )
    147 			throw std::bad_alloc();
    148 		return p;
    149 	}
    150 
    151 	ANTLR_INLINE static void  free( void* p )
    152 	{
    153 		return ::free(p);
    154 	}
    155 
    156 	ANTLR_INLINE static void* realloc(void *ptr, size_t size)
    157 	{
    158 		return ::realloc( ptr, size );
    159 	}
    160 };
    161 
    162 ANTLR_END_NAMESPACE()
    163 
    164 #endif	/* _ANTLR3MEMORY_H */
    165