blob: 532486a95c4fbee809eed472f66db30105924403 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
2// Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7#ifndef _COMMON_INCLUDED_
8#define _COMMON_INCLUDED_
9
alokp@chromium.org4e89d232010-05-14 19:37:21 +000010#include <map>
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000011#include <sstream>
alokp@chromium.org4e89d232010-05-14 19:37:21 +000012#include <string>
13#include <vector>
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000014
daniel@transgaming.come6842292010-04-20 18:52:50 +000015#include "compiler/PoolAlloc.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000016
Jamie Madill075edd82013-07-08 13:30:19 -040017struct TSourceLoc {
18 int first_file;
19 int first_line;
20 int last_file;
21 int last_line;
22};
alokp@chromium.org4e89d232010-05-14 19:37:21 +000023
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000024//
25// Put POOL_ALLOCATOR_NEW_DELETE in base classes to make them use this scheme.
26//
27#define POOL_ALLOCATOR_NEW_DELETE(A) \
28 void* operator new(size_t s) { return (A).allocate(s); } \
29 void* operator new(size_t, void *_Where) { return (_Where); } \
30 void operator delete(void*) { } \
31 void operator delete(void *, void *) { } \
32 void* operator new[](size_t s) { return (A).allocate(s); } \
33 void* operator new[](size_t, void *_Where) { return (_Where); } \
34 void operator delete[](void*) { } \
35 void operator delete[](void *, void *) { }
36
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000037//
38// Pool version of string.
39//
40typedef pool_allocator<char> TStringAllocator;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000041typedef std::basic_string <char, std::char_traits<char>, TStringAllocator> TString;
42typedef std::basic_ostringstream<char, std::char_traits<char>, TStringAllocator> TStringStream;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000043inline TString* NewPoolTString(const char* s)
44{
45 void* memory = GlobalPoolAllocator.allocate(sizeof(TString));
46 return new(memory) TString(s);
47}
48
49//
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000050// Persistent string memory. Should only be used for strings that survive
alokp@chromium.org774d7062010-07-21 18:55:45 +000051// across compiles.
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000052//
53#define TPersistString std::string
54#define TPersistStringStream std::ostringstream
55
56//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000057// Pool allocator versions of vectors, lists, and maps
58//
59template <class T> class TVector : public std::vector<T, pool_allocator<T> > {
60public:
61 typedef typename std::vector<T, pool_allocator<T> >::size_type size_type;
62 TVector() : std::vector<T, pool_allocator<T> >() {}
63 TVector(const pool_allocator<T>& a) : std::vector<T, pool_allocator<T> >(a) {}
64 TVector(size_type i): std::vector<T, pool_allocator<T> >(i) {}
65};
66
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000067template <class K, class D, class CMP = std::less<K> >
alokp@chromium.org91a01a12010-05-12 18:39:04 +000068class TMap : public std::map<K, D, CMP, pool_allocator<std::pair<const K, D> > > {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000069public:
alokp@chromium.org91a01a12010-05-12 18:39:04 +000070 typedef pool_allocator<std::pair<const K, D> > tAllocator;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000071
alokp@chromium.org91a01a12010-05-12 18:39:04 +000072 TMap() : std::map<K, D, CMP, tAllocator>() {}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000073 // use correct two-stage name lookup supported in gcc 3.4 and above
alokp@chromium.org91a01a12010-05-12 18:39:04 +000074 TMap(const tAllocator& a) : std::map<K, D, CMP, tAllocator>(std::map<K, D, CMP, tAllocator>::key_compare(), a) {}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000075};
76
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000077#endif // _COMMON_INCLUDED_