blob: aaafd26cc942950928040e4cefe96d5d470a3534 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/* 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>
Nick Kralevichbeeeee72010-05-06 15:05:52 -070025#include <cutils/mspace.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080026
Mathias Agopian006ba852009-06-01 15:27:46 -070027#include "tinyutils/KeyedVector.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080028#include "tinyutils/smartpointer.h"
29
30namespace android {
31
32// ----------------------------------------------------------------------------
33
34class AssemblyKeyBase {
35public:
36 virtual ~AssemblyKeyBase() { }
37 virtual int compare_type(const AssemblyKeyBase& key) const = 0;
38};
39
40template <typename T>
41class AssemblyKey : public AssemblyKeyBase
42{
43public:
44 AssemblyKey(const T& rhs) : mKey(rhs) { }
45 virtual int compare_type(const AssemblyKeyBase& key) const {
46 const T& rhs = static_cast<const AssemblyKey&>(key).mKey;
47 return android::compare_type(mKey, rhs);
48 }
49private:
50 T mKey;
51};
52
53// ----------------------------------------------------------------------------
54
55class Assembly
56{
57public:
58 Assembly(size_t size);
59 virtual ~Assembly();
60
61 ssize_t size() const;
62 uint32_t* base() const;
63 ssize_t resize(size_t size);
64
65 // protocol for sp<>
66 void incStrong(const void* id) const;
67 void decStrong(const void* id) const;
68 typedef void weakref_type;
69
70private:
Nick Kralevichbeeeee72010-05-06 15:05:52 -070071 static mspace getMspace();
72 void ensureMbaseExecutable();
73
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080074 mutable int32_t mCount;
75 uint32_t* mBase;
Nick Kralevichbeeeee72010-05-06 15:05:52 -070076 size_t mSize;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080077};
78
79// ----------------------------------------------------------------------------
80
81class CodeCache
82{
83public:
84// pretty simple cache API...
85 CodeCache(size_t size);
86 ~CodeCache();
87
88 sp<Assembly> lookup(const AssemblyKeyBase& key) const;
89
90 int cache( const AssemblyKeyBase& key,
91 const sp<Assembly>& assembly);
92
93private:
94 // nothing to see here...
95 struct cache_entry_t {
96 inline cache_entry_t() { }
97 inline cache_entry_t(const sp<Assembly>& a, int64_t w)
98 : entry(a), when(w) { }
99 sp<Assembly> entry;
100 mutable int64_t when;
101 };
102
103 class key_t {
104 friend int compare_type(
105 const key_value_pair_t<key_t, cache_entry_t>&,
106 const key_value_pair_t<key_t, cache_entry_t>&);
107 const AssemblyKeyBase* mKey;
108 public:
109 key_t() { };
110 key_t(const AssemblyKeyBase& k) : mKey(&k) { }
111 };
112
113 mutable pthread_mutex_t mLock;
114 mutable int64_t mWhen;
115 size_t mCacheSize;
116 size_t mCacheInUse;
117 KeyedVector<key_t, cache_entry_t> mCacheData;
118
119 friend int compare_type(
120 const key_value_pair_t<key_t, cache_entry_t>&,
121 const key_value_pair_t<key_t, cache_entry_t>&);
122};
123
124// KeyedVector uses compare_type(), which is more efficient, than
125// just using operator < ()
126inline int compare_type(
127 const key_value_pair_t<CodeCache::key_t, CodeCache::cache_entry_t>& lhs,
128 const key_value_pair_t<CodeCache::key_t, CodeCache::cache_entry_t>& rhs)
129{
130 return lhs.key.mKey->compare_type(*(rhs.key.mKey));
131}
132
133// ----------------------------------------------------------------------------
134
135}; // namespace android
136
137#endif //ANDROID_CODECACHE_H