blob: 094e3ff577ef006a6191949db18a9b3371b57887 [file] [log] [blame]
Dmitry Skiba01971112015-07-10 14:54:00 -04001//
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
Jamie Madill45bcc782016-11-07 13:58:48 -050015namespace sh
16{
17
Dmitry Skiba01971112015-07-10 14:54:00 -040018namespace
19{
20
21class TScopedAllocator : angle::NonCopyable
22{
23 public:
24 TScopedAllocator(TPoolAllocator *allocator)
25 : mPreviousAllocator(GetGlobalPoolAllocator())
26 {
27 SetGlobalPoolAllocator(allocator);
28 }
29 ~TScopedAllocator()
30 {
31 SetGlobalPoolAllocator(mPreviousAllocator);
32 }
33
34 private:
35 TPoolAllocator *mPreviousAllocator;
36};
37
38} // namespace
39
40TCache::TypeKey::TypeKey(TBasicType basicType,
41 TPrecision precision,
42 TQualifier qualifier,
43 unsigned char primarySize,
44 unsigned char secondarySize)
45{
46 static_assert(sizeof(components) <= sizeof(value),
47 "TypeKey::value is too small");
48
49 const size_t MaxEnumValue = std::numeric_limits<EnumComponentType>::max();
Dmitry Skiba01971112015-07-10 14:54:00 -040050
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 value = 0;
58 components.basicType = static_cast<EnumComponentType>(basicType);
59 components.precision = static_cast<EnumComponentType>(precision);
60 components.qualifier = static_cast<EnumComponentType>(qualifier);
61 components.primarySize = primarySize;
62 components.secondarySize = secondarySize;
63}
64
65TCache *TCache::sCache = nullptr;
66
67void TCache::initialize()
68{
69 if (sCache == nullptr)
70 {
71 sCache = new TCache();
72 }
73}
74
75void TCache::destroy()
76{
77 SafeDelete(sCache);
78}
79
80const 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 primarySize, secondarySize);
88 auto it = sCache->mTypes.find(key);
89 if (it != sCache->mTypes.end())
90 {
91 return it->second;
92 }
93
94 TScopedAllocator scopedAllocator(&sCache->mAllocator);
95
96 TType *type = new TType(basicType, precision, qualifier,
97 primarySize, secondarySize);
98 type->realize();
99 sCache->mTypes.insert(std::make_pair(key, type));
100
101 return type;
102}
Jamie Madill45bcc782016-11-07 13:58:48 -0500103
104} // namespace sh