From acf735c13998ad2a175f5a17e7bfce220073279d Mon Sep 17 00:00:00 2001 From: Calin Juravle Date: Thu, 12 Feb 2015 15:25:22 +0000 Subject: 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 --- runtime/handle_scope.h | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'runtime/handle_scope.h') 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 + #include "base/logging.h" #include "base/macros.h" #include "handle.h" @@ -176,6 +178,54 @@ class PACKED(4) StackHandleScope FINAL : public HandleScope { template 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 + MutableHandle NewHandle(T* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { + if (scopes_.empty() || current_scope_num_refs_ >= kNumReferencesPerScope) { + StackHandleScope* scope = + new StackHandleScope(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*> scopes_; + size_t current_scope_num_refs_; + + DISALLOW_COPY_AND_ASSIGN(StackHandleScopeCollection); +}; + } // namespace art #endif // ART_RUNTIME_HANDLE_SCOPE_H_ -- cgit v1.2.3