CodeCache.h revision 4f6e8d7a00cbeda1e70cc15be9c4af1018bdad53
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
28#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:
71    mutable int32_t     mCount;
72            uint32_t*   mBase;
73            ssize_t     mSize;
74};
75
76// ----------------------------------------------------------------------------
77
78class CodeCache
79{
80public:
81// pretty simple cache API...
82                CodeCache(size_t size);
83                ~CodeCache();
84
85            sp<Assembly>        lookup(const AssemblyKeyBase& key) const;
86
87            int                 cache(  const AssemblyKeyBase& key,
88                                        const sp<Assembly>& assembly);
89
90private:
91    // nothing to see here...
92    struct cache_entry_t {
93        inline cache_entry_t() { }
94        inline cache_entry_t(const sp<Assembly>& a, int64_t w)
95                : entry(a), when(w) { }
96        sp<Assembly>            entry;
97        mutable int64_t         when;
98    };
99
100    class key_t {
101        friend int compare_type(
102            const key_value_pair_t<key_t, cache_entry_t>&,
103            const key_value_pair_t<key_t, cache_entry_t>&);
104        const AssemblyKeyBase* mKey;
105    public:
106        key_t() { };
107        key_t(const AssemblyKeyBase& k) : mKey(&k)  { }
108    };
109
110    mutable pthread_mutex_t             mLock;
111    mutable int64_t                     mWhen;
112    size_t                              mCacheSize;
113    size_t                              mCacheInUse;
114    KeyedVector<key_t, cache_entry_t>   mCacheData;
115
116    friend int compare_type(
117        const key_value_pair_t<key_t, cache_entry_t>&,
118        const key_value_pair_t<key_t, cache_entry_t>&);
119};
120
121// KeyedVector uses compare_type(), which is more efficient, than
122// just using operator < ()
123inline int compare_type(
124    const key_value_pair_t<CodeCache::key_t, CodeCache::cache_entry_t>& lhs,
125    const key_value_pair_t<CodeCache::key_t, CodeCache::cache_entry_t>& rhs)
126{
127    return lhs.key.mKey->compare_type(*(rhs.key.mKey));
128}
129
130// ----------------------------------------------------------------------------
131
132}; // namespace android
133
134#endif //ANDROID_CODECACHE_H
135