1/*
2 * Copyright (C) 2013 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 */
16
17#ifndef ART_RUNTIME_GC_SPACE_ROSALLOC_SPACE_H_
18#define ART_RUNTIME_GC_SPACE_ROSALLOC_SPACE_H_
19
20#include "gc/allocator/rosalloc.h"
21#include "malloc_space.h"
22#include "space.h"
23
24namespace art {
25namespace gc {
26
27namespace collector {
28  class MarkSweep;
29}  // namespace collector
30
31namespace space {
32
33// An alloc space implemented using a runs-of-slots memory allocator. Not final as may be
34// overridden by a ValgrindMallocSpace.
35class RosAllocSpace : public MallocSpace {
36 public:
37  // Create a RosAllocSpace with the requested sizes. The requested
38  // base address is not guaranteed to be granted, if it is required,
39  // the caller should call Begin on the returned space to confirm the
40  // request was granted.
41  static RosAllocSpace* Create(const std::string& name, size_t initial_size, size_t growth_limit,
42                               size_t capacity, byte* requested_begin, bool low_memory_mode,
43                               bool can_move_objects);
44  static RosAllocSpace* CreateFromMemMap(MemMap* mem_map, const std::string& name,
45                                         size_t starting_size, size_t initial_size,
46                                         size_t growth_limit, size_t capacity,
47                                         bool low_memory_mode, bool can_move_objects);
48
49  mirror::Object* AllocWithGrowth(Thread* self, size_t num_bytes, size_t* bytes_allocated,
50                                  size_t* usable_size) OVERRIDE LOCKS_EXCLUDED(lock_);
51  mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated,
52                        size_t* usable_size) OVERRIDE {
53    return AllocNonvirtual(self, num_bytes, bytes_allocated, usable_size);
54  }
55  mirror::Object* AllocThreadUnsafe(Thread* self, size_t num_bytes, size_t* bytes_allocated,
56                                    size_t* usable_size)
57      OVERRIDE EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_) {
58    return AllocNonvirtualThreadUnsafe(self, num_bytes, bytes_allocated, usable_size);
59  }
60  size_t AllocationSize(mirror::Object* obj, size_t* usable_size) OVERRIDE {
61    return AllocationSizeNonvirtual(obj, usable_size);
62  }
63  size_t Free(Thread* self, mirror::Object* ptr) OVERRIDE
64      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
65  size_t FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) OVERRIDE
66      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
67
68  mirror::Object* AllocNonvirtual(Thread* self, size_t num_bytes, size_t* bytes_allocated,
69                                  size_t* usable_size) {
70    // RosAlloc zeroes memory internally.
71    return AllocCommon(self, num_bytes, bytes_allocated, usable_size);
72  }
73  mirror::Object* AllocNonvirtualThreadUnsafe(Thread* self, size_t num_bytes,
74                                              size_t* bytes_allocated, size_t* usable_size) {
75    // RosAlloc zeroes memory internally. Pass in false for thread unsafe.
76    return AllocCommon<false>(self, num_bytes, bytes_allocated, usable_size);
77  }
78
79  // TODO: NO_THREAD_SAFETY_ANALYSIS because SizeOf() requires that mutator_lock is held.
80  size_t AllocationSizeNonvirtual(mirror::Object* obj, size_t* usable_size)
81      NO_THREAD_SAFETY_ANALYSIS;
82
83  allocator::RosAlloc* GetRosAlloc() const {
84    return rosalloc_;
85  }
86
87  size_t Trim() OVERRIDE;
88  void Walk(WalkCallback callback, void* arg) OVERRIDE LOCKS_EXCLUDED(lock_);
89  size_t GetFootprint() OVERRIDE;
90  size_t GetFootprintLimit() OVERRIDE;
91  void SetFootprintLimit(size_t limit) OVERRIDE;
92
93  void Clear() OVERRIDE;
94
95  MallocSpace* CreateInstance(const std::string& name, MemMap* mem_map, void* allocator,
96                              byte* begin, byte* end, byte* limit, size_t growth_limit,
97                              bool can_move_objects) OVERRIDE;
98
99  uint64_t GetBytesAllocated() OVERRIDE;
100  uint64_t GetObjectsAllocated() OVERRIDE;
101
102  void RevokeThreadLocalBuffers(Thread* thread);
103  void RevokeAllThreadLocalBuffers();
104  void AssertAllThreadLocalBuffersAreRevoked();
105
106  // Returns the class of a recently freed object.
107  mirror::Class* FindRecentFreedObject(const mirror::Object* obj);
108
109  bool IsRosAllocSpace() const OVERRIDE {
110    return true;
111  }
112
113  RosAllocSpace* AsRosAllocSpace() OVERRIDE {
114    return this;
115  }
116
117  void Verify() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_) {
118    rosalloc_->Verify();
119  }
120
121  virtual ~RosAllocSpace();
122
123  void LogFragmentationAllocFailure(std::ostream& os, size_t failed_alloc_bytes) OVERRIDE {
124    rosalloc_->LogFragmentationAllocFailure(os, failed_alloc_bytes);
125  }
126
127 protected:
128  RosAllocSpace(const std::string& name, MemMap* mem_map, allocator::RosAlloc* rosalloc,
129                byte* begin, byte* end, byte* limit, size_t growth_limit, bool can_move_objects,
130                size_t starting_size, size_t initial_size, bool low_memory_mode);
131
132 private:
133  template<bool kThreadSafe = true>
134  mirror::Object* AllocCommon(Thread* self, size_t num_bytes, size_t* bytes_allocated,
135                              size_t* usable_size);
136
137  void* CreateAllocator(void* base, size_t morecore_start, size_t initial_size,
138                        size_t maximum_size, bool low_memory_mode) OVERRIDE {
139    return CreateRosAlloc(base, morecore_start, initial_size, maximum_size, low_memory_mode);
140  }
141  static allocator::RosAlloc* CreateRosAlloc(void* base, size_t morecore_start, size_t initial_size,
142                                             size_t maximum_size, bool low_memory_mode);
143
144  void InspectAllRosAlloc(void (*callback)(void *start, void *end, size_t num_bytes, void* callback_arg),
145                          void* arg, bool do_null_callback_at_end)
146      LOCKS_EXCLUDED(Locks::runtime_shutdown_lock_, Locks::thread_list_lock_);
147  void InspectAllRosAllocWithSuspendAll(
148      void (*callback)(void *start, void *end, size_t num_bytes, void* callback_arg),
149      void* arg, bool do_null_callback_at_end)
150      LOCKS_EXCLUDED(Locks::runtime_shutdown_lock_, Locks::thread_list_lock_);
151
152  // Underlying rosalloc.
153  allocator::RosAlloc* rosalloc_;
154
155  const bool low_memory_mode_;
156
157  friend class collector::MarkSweep;
158
159  DISALLOW_COPY_AND_ASSIGN(RosAllocSpace);
160};
161
162}  // namespace space
163}  // namespace gc
164}  // namespace art
165
166#endif  // ART_RUNTIME_GC_SPACE_ROSALLOC_SPACE_H_
167