1 /*
2  * Copyright (C) 2014 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_ZYGOTE_SPACE_H_
18 #define ART_RUNTIME_GC_SPACE_ZYGOTE_SPACE_H_
19 
20 #include "base/mem_map.h"
21 #include "gc/accounting/space_bitmap.h"
22 #include "malloc_space.h"
23 
24 namespace art {
25 namespace gc {
26 
27 namespace space {
28 
29 // A zygote space is a space which you cannot allocate into or free from.
30 class ZygoteSpace final : public ContinuousMemMapAllocSpace {
31  public:
32   // Returns the remaining storage in the out_map field.
33   static ZygoteSpace* Create(const std::string& name,
34                              MemMap&& mem_map,
35                              accounting::ContinuousSpaceBitmap&& live_bitmap,
36                              accounting::ContinuousSpaceBitmap&& mark_bitmap)
37       REQUIRES_SHARED(Locks::mutator_lock_);
38   // In PreZygoteFork() we set mark-bit of all live objects to avoid page
39   // getting dirtied due to it.
40   void SetMarkBitInLiveObjects();
41   void Dump(std::ostream& os) const override;
42 
GetType()43   SpaceType GetType() const override {
44     return kSpaceTypeZygoteSpace;
45   }
46 
AsZygoteSpace()47   ZygoteSpace* AsZygoteSpace() override {
48     return this;
49   }
50 
51   mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated,
52                         size_t* usable_size, size_t* bytes_tl_bulk_allocated) override;
53 
54   size_t AllocationSize(mirror::Object* obj, size_t* usable_size) override;
55 
56   size_t Free(Thread* self, mirror::Object* ptr) override;
57 
58   size_t FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) override;
59 
60   // ZygoteSpaces don't have thread local state.
RevokeThreadLocalBuffers(art::Thread *)61   size_t RevokeThreadLocalBuffers(art::Thread*) override {
62     return 0U;
63   }
RevokeAllThreadLocalBuffers()64   size_t RevokeAllThreadLocalBuffers() override {
65     return 0U;
66   }
67 
GetBytesAllocated()68   uint64_t GetBytesAllocated() override {
69     return Size();
70   }
71 
GetObjectsAllocated()72   uint64_t GetObjectsAllocated() override {
73     return objects_allocated_.load();
74   }
75 
76   void Clear() override;
77 
CanMoveObjects()78   bool CanMoveObjects() const override {
79     return false;
80   }
81 
82   void LogFragmentationAllocFailure(std::ostream& os, size_t failed_alloc_bytes) override
83       REQUIRES_SHARED(Locks::mutator_lock_);
84 
85  protected:
GetSweepCallback()86   accounting::ContinuousSpaceBitmap::SweepCallback* GetSweepCallback() override {
87     return &SweepCallback;
88   }
89 
90  private:
91   ZygoteSpace(const std::string& name, MemMap&& mem_map, size_t objects_allocated);
92   static void SweepCallback(size_t num_ptrs, mirror::Object** ptrs, void* arg);
93 
94   AtomicInteger objects_allocated_;
95 
96   friend class Space;
97   DISALLOW_COPY_AND_ASSIGN(ZygoteSpace);
98 };
99 
100 }  // namespace space
101 }  // namespace gc
102 }  // namespace art
103 
104 #endif  // ART_RUNTIME_GC_SPACE_ZYGOTE_SPACE_H_
105