| Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 16 | |
| 17 | #ifndef ART_SRC_MARK_STACK_H_ |
| 18 | #define ART_SRC_MARK_STACK_H_ |
| 19 | |
| Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 20 | #include <string> |
| 21 | |
| 22 | #include "atomic.h" |
| Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 23 | #include "UniquePtr.h" |
| Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 24 | #include "logging.h" |
| 25 | #include "macros.h" |
| Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 26 | #include "mem_map.h" |
| Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 27 | |
| 28 | namespace art { |
| 29 | |
| 30 | class Object; |
| 31 | |
| 32 | class MarkStack { |
| 33 | public: |
| Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 34 | // Length is in bytes. |
| 35 | static MarkStack* Create(const std::string& name, size_t length); |
| Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 36 | |
| 37 | ~MarkStack(); |
| 38 | |
| 39 | void Push(const Object* obj) { |
| 40 | DCHECK(obj != NULL); |
| 41 | DCHECK_NE(ptr_, limit_); |
| 42 | *ptr_ = obj; |
| 43 | ++ptr_; |
| 44 | } |
| 45 | |
| Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 46 | // Beware: Atomic pushes and pops don't mix well. |
| 47 | void AtomicPush(const Object* obj) { |
| 48 | DCHECK(obj != NULL); |
| 49 | DCHECK_NE(ptr_, limit_); |
| 50 | DCHECK_EQ(sizeof(ptr_), sizeof(int32_t)); |
| Mathieu Chartier | 556fad3 | 2012-08-20 16:13:20 -0700 | [diff] [blame] | 51 | int32_t* ptr = reinterpret_cast<int32_t*>(reinterpret_cast<size_t>(&ptr_)); |
| Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 52 | *reinterpret_cast<const Object**>(android_atomic_add(sizeof(*ptr_), ptr)) = obj; |
| 53 | } |
| 54 | |
| Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 55 | const Object* Pop() { |
| Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 56 | DCHECK_NE(ptr_, begin_); |
| Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 57 | --ptr_; |
| 58 | DCHECK(*ptr_ != NULL); |
| 59 | return *ptr_; |
| 60 | } |
| 61 | |
| 62 | bool IsEmpty() const { |
| Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 63 | return ptr_ == begin_; |
| Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 66 | size_t Size() const { |
| 67 | DCHECK_GE(ptr_, begin_); |
| 68 | return ptr_ - begin_; |
| 69 | } |
| 70 | |
| 71 | const Object* Get(size_t index) const { |
| 72 | DCHECK_LT(index, Size()); |
| 73 | return begin_[index]; |
| 74 | } |
| 75 | |
| 76 | Object** Begin() { |
| 77 | return const_cast<Object**>(begin_); |
| 78 | } |
| 79 | |
| Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 80 | Object** End() { |
| 81 | return const_cast<Object**>(ptr_); |
| 82 | } |
| 83 | |
| Mathieu Chartier | 5301cd2 | 2012-05-31 12:11:36 -0700 | [diff] [blame] | 84 | void Reset(); |
| 85 | |
| Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 86 | private: |
| Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 87 | MarkStack(const std::string& name) : name_(name), begin_(NULL), limit_(NULL), ptr_(NULL) {} |
| Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 88 | |
| Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 89 | void Init(size_t length); |
| 90 | |
| 91 | // Name of the mark stack. |
| 92 | const std::string& name_; |
| Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 93 | |
| Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 94 | // Memory mapping of the mark stack. |
| Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 95 | UniquePtr<MemMap> mem_map_; |
| Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 96 | |
| Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 97 | // Base of the mark stack. |
| Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 98 | const Object* const* begin_; |
| Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 99 | |
| 100 | // Exclusive limit of the mark stack. |
| 101 | const Object* const* limit_; |
| 102 | |
| 103 | // Pointer to the top of the mark stack. |
| Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 104 | const Object** ptr_; |
| Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 105 | |
| 106 | DISALLOW_COPY_AND_ASSIGN(MarkStack); |
| 107 | }; |
| 108 | |
| 109 | } // namespace art |
| 110 | |
| 111 | #endif // ART_SRC_MARK_STACK_H_ |