blob: 79c587168a721157ff788d060a92c6e6901d0219 [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 <assert.h>
11#include <stdio.h>
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000012
alokp@chromium.org4e89d232010-05-14 19:37:21 +000013#include <map>
14#include <string>
15#include <vector>
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000016
daniel@transgaming.come6842292010-04-20 18:52:50 +000017#include "compiler/PoolAlloc.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000018
alokp@chromium.org4e89d232010-05-14 19:37:21 +000019typedef int TSourceLoc;
20
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000021//
22// Put POOL_ALLOCATOR_NEW_DELETE in base classes to make them use this scheme.
23//
24#define POOL_ALLOCATOR_NEW_DELETE(A) \
25 void* operator new(size_t s) { return (A).allocate(s); } \
26 void* operator new(size_t, void *_Where) { return (_Where); } \
27 void operator delete(void*) { } \
28 void operator delete(void *, void *) { } \
29 void* operator new[](size_t s) { return (A).allocate(s); } \
30 void* operator new[](size_t, void *_Where) { return (_Where); } \
31 void operator delete[](void*) { } \
32 void operator delete[](void *, void *) { }
33
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000034//
35// Pool version of string.
36//
37typedef pool_allocator<char> TStringAllocator;
38typedef std::basic_string <char, std::char_traits<char>, TStringAllocator > TString;
39inline TString* NewPoolTString(const char* s)
40{
41 void* memory = GlobalPoolAllocator.allocate(sizeof(TString));
42 return new(memory) TString(s);
43}
44
45//
46// Pool allocator versions of vectors, lists, and maps
47//
48template <class T> class TVector : public std::vector<T, pool_allocator<T> > {
49public:
50 typedef typename std::vector<T, pool_allocator<T> >::size_type size_type;
51 TVector() : std::vector<T, pool_allocator<T> >() {}
52 TVector(const pool_allocator<T>& a) : std::vector<T, pool_allocator<T> >(a) {}
53 TVector(size_type i): std::vector<T, pool_allocator<T> >(i) {}
54};
55
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000056template <class K, class D, class CMP = std::less<K> >
alokp@chromium.org91a01a12010-05-12 18:39:04 +000057class TMap : public std::map<K, D, CMP, pool_allocator<std::pair<const K, D> > > {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000058public:
alokp@chromium.org91a01a12010-05-12 18:39:04 +000059 typedef pool_allocator<std::pair<const K, D> > tAllocator;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000060
alokp@chromium.org91a01a12010-05-12 18:39:04 +000061 TMap() : std::map<K, D, CMP, tAllocator>() {}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000062 // use correct two-stage name lookup supported in gcc 3.4 and above
alokp@chromium.org91a01a12010-05-12 18:39:04 +000063 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 +000064};
65
66//
67// Persistent string memory. Should only be used for strings that survive
68// across compiles/links.
69//
70typedef std::basic_string<char> TPersistString;
71
72//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000073// Create a TString object from an integer.
74//
75inline const TString String(const int i, const int base = 10)
76{
77 char text[16]; // 32 bit ints are at most 10 digits in base 10
78
79 #ifdef _WIN32
80 _itoa(i, text, base);
81 #else
82 // we assume base 10 for all cases
83 sprintf(text, "%d", i);
84 #endif
85
86 return text;
87}
88
89const unsigned int SourceLocLineMask = 0xffff;
90const unsigned int SourceLocStringShift = 16;
91
92__inline TPersistString FormatSourceLoc(const TSourceLoc loc)
93{
94 char locText[64];
95
96 int string = loc >> SourceLocStringShift;
97 int line = loc & SourceLocLineMask;
98
99 if (line)
100 sprintf(locText, "%d:%d", string, line);
101 else
102 sprintf(locText, "%d:? ", string);
103
104 return TPersistString(locText);
105}
106
107
108typedef TMap<TString, TString> TPragmaTable;
109typedef TMap<TString, TString>::tAllocator TPragmaTableAllocator;
110
111#endif // _COMMON_INCLUDED_