rosalloc_space.h revision 31f441464c0c8f840aba37e236ad133f30308d70
1cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi/*
2cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi * Copyright (C) 2013 The Android Open Source Project
3cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi *
4cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi * Licensed under the Apache License, Version 2.0 (the "License");
5cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi * you may not use this file except in compliance with the License.
6cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi * You may obtain a copy of the License at
7cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi *
8cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi *      http://www.apache.org/licenses/LICENSE-2.0
9cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi *
10cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi * Unless required by applicable law or agreed to in writing, software
11cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi * distributed under the License is distributed on an "AS IS" BASIS,
12cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi * See the License for the specific language governing permissions and
14cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi * limitations under the License.
15cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi */
16cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
17cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi#ifndef ART_RUNTIME_GC_SPACE_ROSALLOC_SPACE_H_
18cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi#define ART_RUNTIME_GC_SPACE_ROSALLOC_SPACE_H_
19cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
20cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi#include "gc/allocator/rosalloc.h"
21cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi#include "malloc_space.h"
22cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi#include "space.h"
23cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
24cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchinamespace art {
25cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchinamespace gc {
26cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
27cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchinamespace collector {
28cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  class MarkSweep;
29cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi}  // namespace collector
30cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
31cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchinamespace space {
32cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
336fac447555dc94a935b78198479cce645c837b89Ian Rogers// An alloc space implemented using a runs-of-slots memory allocator. Not final as may be
346fac447555dc94a935b78198479cce645c837b89Ian Rogers// overridden by a ValgrindMallocSpace.
35cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchiclass RosAllocSpace : public MallocSpace {
36cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi public:
37cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  // Create a RosAllocSpace with the requested sizes. The requested
38cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  // base address is not guaranteed to be granted, if it is required,
39cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  // the caller should call Begin on the returned space to confirm the
40cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  // request was granted.
41cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  static RosAllocSpace* Create(const std::string& name, size_t initial_size, size_t growth_limit,
4231f441464c0c8f840aba37e236ad133f30308d70Mathieu Chartier                               size_t capacity, byte* requested_begin, bool low_memory_mode,
4331f441464c0c8f840aba37e236ad133f30308d70Mathieu Chartier                               bool can_move_objects);
44e6da9af8dfe0a3e3fbc2be700554f6478380e7b9Mathieu Chartier  static RosAllocSpace* CreateFromMemMap(MemMap* mem_map, const std::string& name,
45e6da9af8dfe0a3e3fbc2be700554f6478380e7b9Mathieu Chartier                                         size_t starting_size, size_t initial_size,
46e6da9af8dfe0a3e3fbc2be700554f6478380e7b9Mathieu Chartier                                         size_t growth_limit, size_t capacity,
4731f441464c0c8f840aba37e236ad133f30308d70Mathieu Chartier                                         bool low_memory_mode, bool can_move_objects);
48cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
496fac447555dc94a935b78198479cce645c837b89Ian Rogers  mirror::Object* AllocWithGrowth(Thread* self, size_t num_bytes, size_t* bytes_allocated,
506fac447555dc94a935b78198479cce645c837b89Ian Rogers                                  size_t* usable_size) OVERRIDE LOCKS_EXCLUDED(lock_);
516fac447555dc94a935b78198479cce645c837b89Ian Rogers  mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated,
526fac447555dc94a935b78198479cce645c837b89Ian Rogers                        size_t* usable_size) OVERRIDE {
536fac447555dc94a935b78198479cce645c837b89Ian Rogers    return AllocNonvirtual(self, num_bytes, bytes_allocated, usable_size);
546fac447555dc94a935b78198479cce645c837b89Ian Rogers  }
556fac447555dc94a935b78198479cce645c837b89Ian Rogers  size_t AllocationSize(mirror::Object* obj, size_t* usable_size) OVERRIDE {
566fac447555dc94a935b78198479cce645c837b89Ian Rogers    return AllocationSizeNonvirtual(obj, usable_size);
576fac447555dc94a935b78198479cce645c837b89Ian Rogers  }
586fac447555dc94a935b78198479cce645c837b89Ian Rogers  size_t Free(Thread* self, mirror::Object* ptr) OVERRIDE
59ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
606fac447555dc94a935b78198479cce645c837b89Ian Rogers  size_t FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) OVERRIDE
61ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
62cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
636fac447555dc94a935b78198479cce645c837b89Ian Rogers  mirror::Object* AllocNonvirtual(Thread* self, size_t num_bytes, size_t* bytes_allocated,
646fac447555dc94a935b78198479cce645c837b89Ian Rogers                                  size_t* usable_size) {
656fac447555dc94a935b78198479cce645c837b89Ian Rogers    // RosAlloc zeroes memory internally.
666fac447555dc94a935b78198479cce645c837b89Ian Rogers    return AllocCommon(self, num_bytes, bytes_allocated, usable_size);
67cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  }
68cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
696fac447555dc94a935b78198479cce645c837b89Ian Rogers  // TODO: NO_THREAD_SAFETY_ANALYSIS because SizeOf() requires that mutator_lock is held.
706fac447555dc94a935b78198479cce645c837b89Ian Rogers  size_t AllocationSizeNonvirtual(mirror::Object* obj, size_t* usable_size)
716fac447555dc94a935b78198479cce645c837b89Ian Rogers      NO_THREAD_SAFETY_ANALYSIS;
726fac447555dc94a935b78198479cce645c837b89Ian Rogers
736fac447555dc94a935b78198479cce645c837b89Ian Rogers  allocator::RosAlloc* GetRosAlloc() const {
74cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi    return rosalloc_;
75cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  }
76cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
776fac447555dc94a935b78198479cce645c837b89Ian Rogers  size_t Trim() OVERRIDE;
786fac447555dc94a935b78198479cce645c837b89Ian Rogers  void Walk(WalkCallback callback, void* arg) OVERRIDE LOCKS_EXCLUDED(lock_);
796fac447555dc94a935b78198479cce645c837b89Ian Rogers  size_t GetFootprint() OVERRIDE;
806fac447555dc94a935b78198479cce645c837b89Ian Rogers  size_t GetFootprintLimit() OVERRIDE;
816fac447555dc94a935b78198479cce645c837b89Ian Rogers  void SetFootprintLimit(size_t limit) OVERRIDE;
82cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
836fac447555dc94a935b78198479cce645c837b89Ian Rogers  void Clear() OVERRIDE;
8431f441464c0c8f840aba37e236ad133f30308d70Mathieu Chartier
85cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  MallocSpace* CreateInstance(const std::string& name, MemMap* mem_map, void* allocator,
8631f441464c0c8f840aba37e236ad133f30308d70Mathieu Chartier                              byte* begin, byte* end, byte* limit, size_t growth_limit,
8731f441464c0c8f840aba37e236ad133f30308d70Mathieu Chartier                              bool can_move_objects) OVERRIDE;
88cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
896fac447555dc94a935b78198479cce645c837b89Ian Rogers  uint64_t GetBytesAllocated() OVERRIDE;
906fac447555dc94a935b78198479cce645c837b89Ian Rogers  uint64_t GetObjectsAllocated() OVERRIDE;
91cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
92cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  void RevokeThreadLocalBuffers(Thread* thread);
93cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  void RevokeAllThreadLocalBuffers();
94c93c530efc175954160c3834c93961a1a946a35aHiroshi Yamauchi  void AssertAllThreadLocalBuffersAreRevoked();
95cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
96cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  // Returns the class of a recently freed object.
97cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  mirror::Class* FindRecentFreedObject(const mirror::Object* obj);
98cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
996fac447555dc94a935b78198479cce645c837b89Ian Rogers  bool IsRosAllocSpace() const OVERRIDE {
100cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi    return true;
101cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  }
1026fac447555dc94a935b78198479cce645c837b89Ian Rogers
1036fac447555dc94a935b78198479cce645c837b89Ian Rogers  RosAllocSpace* AsRosAllocSpace() OVERRIDE {
104cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi    return this;
105cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  }
106cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
107a4adbfd44032d70e166e6f18096bbbed05a990baHiroshi Yamauchi  void Verify() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_) {
108a4adbfd44032d70e166e6f18096bbbed05a990baHiroshi Yamauchi    rosalloc_->Verify();
109a4adbfd44032d70e166e6f18096bbbed05a990baHiroshi Yamauchi  }
110a4adbfd44032d70e166e6f18096bbbed05a990baHiroshi Yamauchi
111661974a5561e5ccdfbac8cb5d8df8b7e6f3483b8Mathieu Chartier  virtual ~RosAllocSpace();
112661974a5561e5ccdfbac8cb5d8df8b7e6f3483b8Mathieu Chartier
113cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi protected:
114cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  RosAllocSpace(const std::string& name, MemMap* mem_map, allocator::RosAlloc* rosalloc,
11531f441464c0c8f840aba37e236ad133f30308d70Mathieu Chartier                byte* begin, byte* end, byte* limit, size_t growth_limit, bool can_move_objects,
11631f441464c0c8f840aba37e236ad133f30308d70Mathieu Chartier                size_t starting_size, size_t initial_size, bool low_memory_mode);
117cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
118cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi private:
1196fac447555dc94a935b78198479cce645c837b89Ian Rogers  mirror::Object* AllocCommon(Thread* self, size_t num_bytes, size_t* bytes_allocated,
1206fac447555dc94a935b78198479cce645c837b89Ian Rogers                              size_t* usable_size);
121cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
1226fac447555dc94a935b78198479cce645c837b89Ian Rogers  void* CreateAllocator(void* base, size_t morecore_start, size_t initial_size,
12326d69ffc0ebc98fbc5f316d8cd3ee6ba5b2001acHiroshi Yamauchi                        size_t maximum_size, bool low_memory_mode) OVERRIDE {
12426d69ffc0ebc98fbc5f316d8cd3ee6ba5b2001acHiroshi Yamauchi    return CreateRosAlloc(base, morecore_start, initial_size, maximum_size, low_memory_mode);
125cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  }
126573f7d2d68e1838a0485e6b40d90c967526e00c2Hiroshi Yamauchi  static allocator::RosAlloc* CreateRosAlloc(void* base, size_t morecore_start, size_t initial_size,
12726d69ffc0ebc98fbc5f316d8cd3ee6ba5b2001acHiroshi Yamauchi                                             size_t maximum_size, bool low_memory_mode);
128cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
129cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  void InspectAllRosAlloc(void (*callback)(void *start, void *end, size_t num_bytes, void* callback_arg),
1301cd53dbb2163f18b689d2a65cf9c6bdcdb01b407Hiroshi Yamauchi                          void* arg, bool do_null_callback_at_end)
1311cd53dbb2163f18b689d2a65cf9c6bdcdb01b407Hiroshi Yamauchi      LOCKS_EXCLUDED(Locks::runtime_shutdown_lock_, Locks::thread_list_lock_);
1321cd53dbb2163f18b689d2a65cf9c6bdcdb01b407Hiroshi Yamauchi  void InspectAllRosAllocWithSuspendAll(
1331cd53dbb2163f18b689d2a65cf9c6bdcdb01b407Hiroshi Yamauchi      void (*callback)(void *start, void *end, size_t num_bytes, void* callback_arg),
1341cd53dbb2163f18b689d2a65cf9c6bdcdb01b407Hiroshi Yamauchi      void* arg, bool do_null_callback_at_end)
135cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi      LOCKS_EXCLUDED(Locks::runtime_shutdown_lock_, Locks::thread_list_lock_);
136cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
137cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  // Underlying rosalloc.
13831f441464c0c8f840aba37e236ad133f30308d70Mathieu Chartier  allocator::RosAlloc* rosalloc_;
13931f441464c0c8f840aba37e236ad133f30308d70Mathieu Chartier
14031f441464c0c8f840aba37e236ad133f30308d70Mathieu Chartier  const bool low_memory_mode_;
1414ce1f00cc74867188347e463f4a5ecb9fe55cde5Hiroshi Yamauchi
142cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  friend class collector::MarkSweep;
143cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
144cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  DISALLOW_COPY_AND_ASSIGN(RosAllocSpace);
145cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi};
146cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
147cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi}  // namespace space
148cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi}  // namespace gc
149cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi}  // namespace art
150cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
151cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi#endif  // ART_RUNTIME_GC_SPACE_ROSALLOC_SPACE_H_
152