Line data Source code
1 : //
2 : // Copyright (c) 2015 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 : // Cache.cpp: Implements a cache for various commonly created objects.
8 :
9 : #include <limits>
10 :
11 : #include "common/angleutils.h"
12 : #include "common/debug.h"
13 : #include "compiler/translator/Cache.h"
14 :
15 : namespace sh
16 : {
17 :
18 : namespace
19 : {
20 :
21 : class TScopedAllocator : angle::NonCopyable
22 : {
23 : public:
24 0 : TScopedAllocator(TPoolAllocator *allocator)
25 0 : : mPreviousAllocator(GetGlobalPoolAllocator())
26 : {
27 0 : SetGlobalPoolAllocator(allocator);
28 0 : }
29 0 : ~TScopedAllocator()
30 0 : {
31 0 : SetGlobalPoolAllocator(mPreviousAllocator);
32 0 : }
33 :
34 : private:
35 : TPoolAllocator *mPreviousAllocator;
36 : };
37 :
38 : } // namespace
39 :
40 0 : TCache::TypeKey::TypeKey(TBasicType basicType,
41 : TPrecision precision,
42 : TQualifier qualifier,
43 : unsigned char primarySize,
44 0 : unsigned char secondarySize)
45 : {
46 : static_assert(sizeof(components) <= sizeof(value),
47 : "TypeKey::value is too small");
48 :
49 0 : const size_t MaxEnumValue = std::numeric_limits<EnumComponentType>::max();
50 :
51 : // TODO: change to static_assert() once we deprecate MSVC 2013 support
52 : ASSERT(MaxEnumValue >= EbtLast &&
53 : MaxEnumValue >= EbpLast &&
54 : MaxEnumValue >= EvqLast &&
55 : "TypeKey::EnumComponentType is too small");
56 :
57 0 : value = 0;
58 0 : components.basicType = static_cast<EnumComponentType>(basicType);
59 0 : components.precision = static_cast<EnumComponentType>(precision);
60 0 : components.qualifier = static_cast<EnumComponentType>(qualifier);
61 0 : components.primarySize = primarySize;
62 0 : components.secondarySize = secondarySize;
63 0 : }
64 :
65 : TCache *TCache::sCache = nullptr;
66 :
67 0 : void TCache::initialize()
68 : {
69 0 : if (sCache == nullptr)
70 : {
71 0 : sCache = new TCache();
72 : }
73 0 : }
74 :
75 0 : void TCache::destroy()
76 : {
77 0 : SafeDelete(sCache);
78 0 : }
79 :
80 0 : const TType *TCache::getType(TBasicType basicType,
81 : TPrecision precision,
82 : TQualifier qualifier,
83 : unsigned char primarySize,
84 : unsigned char secondarySize)
85 : {
86 : TypeKey key(basicType, precision, qualifier,
87 0 : primarySize, secondarySize);
88 0 : auto it = sCache->mTypes.find(key);
89 0 : if (it != sCache->mTypes.end())
90 : {
91 0 : return it->second;
92 : }
93 :
94 0 : TScopedAllocator scopedAllocator(&sCache->mAllocator);
95 :
96 : TType *type = new TType(basicType, precision, qualifier,
97 0 : primarySize, secondarySize);
98 0 : type->realize();
99 0 : sCache->mTypes.insert(std::make_pair(key, type));
100 :
101 0 : return type;
102 : }
103 :
104 : } // namespace sh
|