summaryrefslogtreecommitdiffstats
path: root/runtime/handle_scope.h
diff options
context:
space:
mode:
authorCalin Juravle <calin@google.com>2015-02-12 15:25:22 +0000
committerCalin Juravle <calin@google.com>2015-02-19 17:07:52 +0000
commitacf735c13998ad2a175f5a17e7bfce220073279d (patch)
tree94969f2387f0e6dad8c7e5712aa8187c9de2be56 /runtime/handle_scope.h
parent39109a06015c91188232e59fa9e60e0915d24cd7 (diff)
downloadandroid_art-acf735c13998ad2a175f5a17e7bfce220073279d.tar.gz
android_art-acf735c13998ad2a175f5a17e7bfce220073279d.tar.bz2
android_art-acf735c13998ad2a175f5a17e7bfce220073279d.zip
Reference type propagation
- propagate reference types between instructions - remove checked casts when possible - add StackHandleScopeCollection to manage an arbitrary number of stack handles (see comments) Change-Id: I31200067c5e7375a5ea8e2f873c4374ebdb5ee60
Diffstat (limited to 'runtime/handle_scope.h')
-rw-r--r--runtime/handle_scope.h50
1 files changed, 50 insertions, 0 deletions
diff --git a/runtime/handle_scope.h b/runtime/handle_scope.h
index 782bbeaabc..a8365782a1 100644
--- a/runtime/handle_scope.h
+++ b/runtime/handle_scope.h
@@ -17,6 +17,8 @@
#ifndef ART_RUNTIME_HANDLE_SCOPE_H_
#define ART_RUNTIME_HANDLE_SCOPE_H_
+#include <stack>
+
#include "base/logging.h"
#include "base/macros.h"
#include "handle.h"
@@ -176,6 +178,54 @@ class PACKED(4) StackHandleScope FINAL : public HandleScope {
template<size_t kNumRefs> friend class StackHandleScope;
};
+// Utility class to manage a collection (stack) of StackHandleScope. All the managed
+// scope handle have the same fixed sized.
+// Calls to NewHandle will create a new handle inside the top StackHandleScope.
+// When the handle scope becomes full a new one is created and push on top of the
+// previous.
+//
+// NB:
+// - it is not safe to use the *same* StackHandleScopeCollection intermix with
+// other StackHandleScopes.
+// - this is a an easy way around implementing a full ZoneHandleScope to manage an
+// arbitrary number of handles.
+class StackHandleScopeCollection {
+ public:
+ explicit StackHandleScopeCollection(Thread* const self) :
+ self_(self),
+ current_scope_num_refs_(0) {
+ }
+
+ ~StackHandleScopeCollection() {
+ while (!scopes_.empty()) {
+ delete scopes_.top();
+ scopes_.pop();
+ }
+ }
+
+ template<class T>
+ MutableHandle<T> NewHandle(T* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
+ if (scopes_.empty() || current_scope_num_refs_ >= kNumReferencesPerScope) {
+ StackHandleScope<kNumReferencesPerScope>* scope =
+ new StackHandleScope<kNumReferencesPerScope>(self_);
+ scopes_.push(scope);
+ current_scope_num_refs_ = 0;
+ }
+ current_scope_num_refs_++;
+ return scopes_.top()->NewHandle(object);
+ }
+
+ private:
+ static constexpr size_t kNumReferencesPerScope = 4;
+
+ Thread* const self_;
+
+ std::stack<StackHandleScope<kNumReferencesPerScope>*> scopes_;
+ size_t current_scope_num_refs_;
+
+ DISALLOW_COPY_AND_ASSIGN(StackHandleScopeCollection);
+};
+
} // namespace art
#endif // ART_RUNTIME_HANDLE_SCOPE_H_