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