1 /* libs/pixelflinger/codeflinger/CodeCache.h
2 **
3 ** Copyright 2006, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 ** http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17
18
19 #ifndef ANDROID_CODECACHE_H
20 #define ANDROID_CODECACHE_H
21
22 #include <stdint.h>
23 #include <pthread.h>
24 #include <sys/types.h>
25
26 #include "utils/KeyedVector.h"
27 #include "tinyutils/smartpointer.h"
28
29 namespace android {
30
31 using namespace tinyutils;
32
33 // ----------------------------------------------------------------------------
34
35 class AssemblyKeyBase {
36 public:
~AssemblyKeyBase()37 virtual ~AssemblyKeyBase() { }
38 virtual int compare_type(const AssemblyKeyBase& key) const = 0;
39 };
40
41 template <typename T>
42 class AssemblyKey : public AssemblyKeyBase
43 {
44 public:
AssemblyKey(const T & rhs)45 AssemblyKey(const T& rhs) : mKey(rhs) { }
compare_type(const AssemblyKeyBase & key)46 virtual int compare_type(const AssemblyKeyBase& key) const {
47 const T& rhs = static_cast<const AssemblyKey&>(key).mKey;
48 return android::compare_type(mKey, rhs);
49 }
50 private:
51 T mKey;
52 };
53
54 // ----------------------------------------------------------------------------
55
56 class Assembly
57 {
58 public:
59 Assembly(size_t size);
60 virtual ~Assembly();
61
62 ssize_t size() const;
63 uint32_t* base() const;
64 ssize_t resize(size_t size);
65
66 // protocol for sp<>
67 void incStrong(const void* id) const;
68 void decStrong(const void* id) const;
69 typedef void weakref_type;
70
71 private:
72 mutable int32_t mCount;
73 uint32_t* mBase;
74 size_t mSize;
75 };
76
77 // ----------------------------------------------------------------------------
78
79 class CodeCache
80 {
81 public:
82 // pretty simple cache API...
83 CodeCache(size_t size);
84 ~CodeCache();
85
86 sp<Assembly> lookup(const AssemblyKeyBase& key) const;
87
88 int cache( const AssemblyKeyBase& key,
89 const sp<Assembly>& assembly);
90
91 private:
92 // nothing to see here...
93 struct cache_entry_t {
cache_entry_tcache_entry_t94 inline cache_entry_t() { }
cache_entry_tcache_entry_t95 inline cache_entry_t(const sp<Assembly>& a, int64_t w)
96 : entry(a), when(w) { }
97 sp<Assembly> entry;
98 mutable int64_t when;
99 };
100
101 class key_t {
102 friend int compare_type(
103 const key_value_pair_t<key_t, cache_entry_t>&,
104 const key_value_pair_t<key_t, cache_entry_t>&);
105 const AssemblyKeyBase* mKey;
106 public:
key_t()107 key_t() { };
key_t(const AssemblyKeyBase & k)108 key_t(const AssemblyKeyBase& k) : mKey(&k) { }
109 };
110
111 mutable pthread_mutex_t mLock;
112 mutable int64_t mWhen;
113 size_t mCacheSize;
114 size_t mCacheInUse;
115 KeyedVector<key_t, cache_entry_t> mCacheData;
116
117 friend int compare_type(
118 const key_value_pair_t<key_t, cache_entry_t>&,
119 const key_value_pair_t<key_t, cache_entry_t>&);
120 };
121
122 // KeyedVector uses compare_type(), which is more efficient, than
123 // just using operator < ()
compare_type(const key_value_pair_t<CodeCache::key_t,CodeCache::cache_entry_t> & lhs,const key_value_pair_t<CodeCache::key_t,CodeCache::cache_entry_t> & rhs)124 inline int compare_type(
125 const key_value_pair_t<CodeCache::key_t, CodeCache::cache_entry_t>& lhs,
126 const key_value_pair_t<CodeCache::key_t, CodeCache::cache_entry_t>& rhs)
127 {
128 return lhs.key.mKey->compare_type(*(rhs.key.mKey));
129 }
130
131 // ----------------------------------------------------------------------------
132
133 }; // namespace android
134
135 #endif //ANDROID_CODECACHE_H
136