CodeCache.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #ifndef ANDROID_CODECACHE_H
  18. #define ANDROID_CODECACHE_H
  19. #include <atomic>
  20. #include <stdint.h>
  21. #include <pthread.h>
  22. #include <sys/types.h>
  23. #include "utils/KeyedVector.h"
  24. #include "tinyutils/smartpointer.h"
  25. namespace android {
  26. using namespace tinyutils;
  27. // ----------------------------------------------------------------------------
  28. class AssemblyKeyBase {
  29. public:
  30. virtual ~AssemblyKeyBase() { }
  31. virtual int compare_type(const AssemblyKeyBase& key) const = 0;
  32. };
  33. template <typename T>
  34. class AssemblyKey : public AssemblyKeyBase
  35. {
  36. public:
  37. explicit AssemblyKey(const T& rhs) : mKey(rhs) { }
  38. virtual int compare_type(const AssemblyKeyBase& key) const {
  39. const T& rhs = static_cast<const AssemblyKey&>(key).mKey;
  40. return android::compare_type(mKey, rhs);
  41. }
  42. private:
  43. T mKey;
  44. };
  45. // ----------------------------------------------------------------------------
  46. class Assembly
  47. {
  48. public:
  49. explicit Assembly(size_t size);
  50. virtual ~Assembly();
  51. ssize_t size() const;
  52. uint32_t* base() const;
  53. ssize_t resize(size_t size);
  54. // protocol for sp<>
  55. void incStrong(const void* id) const;
  56. void decStrong(const void* id) const;
  57. typedef void weakref_type;
  58. private:
  59. mutable std::atomic<int32_t> mCount;
  60. uint32_t* mBase;
  61. size_t mSize;
  62. };
  63. // ----------------------------------------------------------------------------
  64. class CodeCache
  65. {
  66. public:
  67. // pretty simple cache API...
  68. explicit CodeCache(size_t size);
  69. ~CodeCache();
  70. sp<Assembly> lookup(const AssemblyKeyBase& key) const;
  71. int cache(const AssemblyKeyBase& key,
  72. const sp<Assembly>& assembly);
  73. private:
  74. // nothing to see here...
  75. struct cache_entry_t {
  76. inline cache_entry_t() { }
  77. inline cache_entry_t(const sp<Assembly>& a, int64_t w)
  78. : entry(a), when(w) { }
  79. sp<Assembly> entry;
  80. mutable int64_t when;
  81. };
  82. class key_t {
  83. friend int compare_type(
  84. const key_value_pair_t<key_t, cache_entry_t>&,
  85. const key_value_pair_t<key_t, cache_entry_t>&);
  86. const AssemblyKeyBase* mKey;
  87. public:
  88. key_t() { };
  89. explicit key_t(const AssemblyKeyBase& k) : mKey(&k) { }
  90. };
  91. mutable pthread_mutex_t mLock;
  92. mutable int64_t mWhen;
  93. size_t mCacheSize;
  94. size_t mCacheInUse;
  95. KeyedVector<key_t, cache_entry_t> mCacheData;
  96. friend int compare_type(
  97. const key_value_pair_t<key_t, cache_entry_t>&,
  98. const key_value_pair_t<key_t, cache_entry_t>&);
  99. };
  100. // KeyedVector uses compare_type(), which is more efficient, than
  101. // just using operator < ()
  102. inline int compare_type(
  103. const key_value_pair_t<CodeCache::key_t, CodeCache::cache_entry_t>& lhs,
  104. const key_value_pair_t<CodeCache::key_t, CodeCache::cache_entry_t>& rhs)
  105. {
  106. return lhs.key.mKey->compare_type(*(rhs.key.mKey));
  107. }
  108. // ----------------------------------------------------------------------------
  109. }; // namespace android
  110. #endif //ANDROID_CODECACHE_H