blob: a932d7b126c67272122606005ddc3b5ee7cce5dd [file] [log] [blame]
Elliott Hughes307f75d2011-10-12 18:04:40 -07001/*
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 Shapiro69759ea2011-07-21 18:13:35 -070016
17#ifndef ART_SRC_SPACE_H_
18#define ART_SRC_SPACE_H_
19
Elliott Hughes307f75d2011-10-12 18:04:40 -070020#include <string>
21
Elliott Hughes90a33692011-08-30 13:27:07 -070022#include "UniquePtr.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070023#include "globals.h"
Brian Carlstroma663ea52011-08-19 23:33:41 -070024#include "image.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070025#include "macros.h"
Ian Rogers30fab402012-01-23 15:43:46 -080026#include "dlmalloc.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070027#include "mem_map.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070028
29namespace art {
30
Ian Rogers30fab402012-01-23 15:43:46 -080031class AllocSpace;
32class ImageSpace;
Carl Shapiro69759ea2011-07-21 18:13:35 -070033class Object;
34
35// A space contains memory allocated for managed objects.
36class Space {
37 public:
Ian Rogers30fab402012-01-23 15:43:46 -080038 // Create a AllocSpace with the requested sizes. The requested
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -070039 // base address is not guaranteed to be granted, if it is required,
Ian Rogers30fab402012-01-23 15:43:46 -080040 // the caller should call Begin on the returned space to confirm
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -070041 // the request was granted.
Ian Rogers30fab402012-01-23 15:43:46 -080042 static AllocSpace* CreateAllocSpace(const std::string& name, size_t initial_size,
43 size_t growth_limit, size_t capacity,
44 byte* requested_begin);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070045
46 // create a Space from an image file. cannot be used for future allocation or collected.
Ian Rogers30fab402012-01-23 15:43:46 -080047 static ImageSpace* CreateImageSpace(const std::string& image);
Carl Shapiro69759ea2011-07-21 18:13:35 -070048
Ian Rogers30fab402012-01-23 15:43:46 -080049 virtual ~Space() {}
Carl Shapiro69759ea2011-07-21 18:13:35 -070050
Ian Rogers30fab402012-01-23 15:43:46 -080051 const std::string& GetSpaceName() const {
52 return name_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070053 }
54
Ian Rogers30fab402012-01-23 15:43:46 -080055 // Address at which the space begins
56 byte* Begin() const {
57 return begin_;
58 }
59
60 // Address at which the space ends, which may vary as the space is filled
61 byte* End() const {
62 return end_;
63 }
64
65 // Is object within this space?
66 bool Contains(const Object* obj) const {
67 const byte* byte_ptr = reinterpret_cast<const byte*>(obj);
68 return Begin() <= byte_ptr && byte_ptr < End();
69 }
70
71 // Current size of space
72 size_t Size() const {
73 return End() - Begin();
74 }
75
76 // Maximum size of space
77 virtual size_t Capacity() const {
78 return mem_map_->Size();
79 }
80
81 // Support for having an impediment (GrowthLimit) removed from the space
82 virtual size_t UnimpededCapacity() const {
83 return Capacity();
84 }
85
86 ImageSpace* AsImageSpace() {
87 DCHECK(IsImageSpace());
88 return down_cast<ImageSpace*>(this);
89 }
90
91 AllocSpace* AsAllocSpace() {
92 DCHECK(IsAllocSpace());
93 return down_cast<AllocSpace*>(this);
94 }
95
96 virtual bool IsAllocSpace() const = 0;
97 virtual bool IsImageSpace() const = 0;
98
99 protected:
100 Space(const std::string& name, MemMap* mem_map, byte* end) : name_(name), mem_map_(mem_map),
101 begin_(mem_map->Begin()), end_(end) {}
102
103 std::string name_;
104 // Underlying storage of the space
105 UniquePtr<MemMap> mem_map_;
106
107 // The beginning of the storage for fast access (always equals mem_map_->GetAddress())
108 byte* const begin_;
109
110 // Current end of the space
111 byte* end_;
112
113 DISALLOW_COPY_AND_ASSIGN(Space);
114};
115
116std::ostream& operator<<(std::ostream& os, const Space& space);
117
118// An alloc space is a space where objects may be allocated and garbage collected.
119class AllocSpace : public Space {
120 public:
121 // Allocate num_bytes without allowing the underlying mspace to grow
122 Object* AllocWithGrowth(size_t num_bytes);
123
124 // Allocate num_bytes allowing the underlying mspace to grow
125 Object* AllocWithoutGrowth(size_t num_bytes);
126
127 // Return the storage space required by obj
128 size_t AllocationSize(const Object* obj);
129
130 void Free(Object* ptr);
131
132 void FreeList(size_t num_ptrs, Object** ptrs);
133
134 void* MoreCore(intptr_t increment);
135
136 void* GetMspace() const {
137 return mspace_;
138 }
139
140 // Hand unused pages back to the system
141 void Trim();
142
143 // Perform a mspace_inspect_all which calls back for each allocation chunk. The chunk may not be
144 // in use, indicated by num_bytes equaling zero
145 void Walk(void(*callback)(void *start, void *end, size_t num_bytes, void* callback_arg),
146 void* arg);
147
148 // Returns the number of bytes that the heap is allowed to obtain from the system via MoreCore
149 size_t GetFootprintLimit();
150
151 // Set the maximum number of bytes that the heap is allowed to obtain from the system via MoreCore
152 void SetFootprintLimit(size_t limit);
153
154 // Removes the fork time growth limit (fence on capacity), allowing the application to allocate
155 // up to the maximum heap size.
156 void ClearGrowthLimit() {
157 growth_limit_ = UnimpededCapacity();
158 }
159
160 // Override capacity so that we only return the possibly limited capacity
161 virtual size_t Capacity() const {
jeffhaoc1160702011-10-27 15:48:45 -0700162 return growth_limit_;
Carl Shapiro58551df2011-07-24 03:09:51 -0700163 }
164
Ian Rogers30fab402012-01-23 15:43:46 -0800165 virtual size_t UnimpededCapacity() const {
166 return mem_map_->End() - mem_map_->Begin();
Ian Rogers5d76c432011-10-31 21:42:49 -0700167 }
168
Ian Rogers30fab402012-01-23 15:43:46 -0800169 virtual bool IsAllocSpace() const {
170 return true;
Elliott Hughes307f75d2011-10-12 18:04:40 -0700171 }
172
Ian Rogers30fab402012-01-23 15:43:46 -0800173 virtual bool IsImageSpace() const {
174 return false;
Ian Rogers5d76c432011-10-31 21:42:49 -0700175 }
176
Carl Shapiro69759ea2011-07-21 18:13:35 -0700177 private:
Ian Rogers30fab402012-01-23 15:43:46 -0800178 friend class Space;
179
180 AllocSpace(const std::string& name, MemMap* mem_map, void* mspace, byte* end,
181 size_t growth_limit) :
182 Space(name, mem_map, end), mspace_(mspace), growth_limit_(growth_limit) {
183 CHECK(mspace != NULL);
184 }
185
186 bool Init(size_t initial_size, size_t maximum_size, size_t growth_size, byte* requested_base);
187
188 static void* CreateMallocSpace(void* base, size_t initial_size, size_t maximum_size);
189
Carl Shapiro58551df2011-07-24 03:09:51 -0700190 // The boundary tag overhead.
191 static const size_t kChunkOverhead = kWordSize;
192
Ian Rogers30fab402012-01-23 15:43:46 -0800193 // Underlying malloc space
194 void* const mspace_;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700195
Ian Rogers30fab402012-01-23 15:43:46 -0800196 // The capacity of the alloc space until such time that ClearGrowthLimit is called.
197 // The underlying mem_map_ controls the maximum size we allow the heap to grow to. The growth
198 // limit is a value <= to the mem_map_ capacity used for ergonomic reasons because of the zygote.
199 // Prior to forking the zygote the heap will have a maximally sized mem_map_ but the growth_limit_
200 // will be set to a lower value. The growth_limit_ is used as the capacity of the alloc_space_,
201 // however, capacity normally can't vary. In the case of the growth_limit_ it can be cleared
202 // one time by a call to ClearGrowthLimit.
203 size_t growth_limit_;
204
205 DISALLOW_COPY_AND_ASSIGN(AllocSpace);
206};
207
208// An image space is a space backed with a memory mapped image
209class ImageSpace : public Space {
210 public:
211 const ImageHeader& GetImageHeader() const {
212 return *reinterpret_cast<ImageHeader*>(Begin());
Elliott Hughes307f75d2011-10-12 18:04:40 -0700213 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700214
Ian Rogers30fab402012-01-23 15:43:46 -0800215 const std::string& GetImageFilename() const {
216 return name_;
217 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700218
Ian Rogers30fab402012-01-23 15:43:46 -0800219 // Mark the objects defined in this space in the given live bitmap
220 void RecordImageAllocations(HeapBitmap* live_bitmap) const;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700221
Ian Rogers30fab402012-01-23 15:43:46 -0800222 virtual bool IsAllocSpace() const {
223 return false;
224 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700225
Ian Rogers30fab402012-01-23 15:43:46 -0800226 virtual bool IsImageSpace() const {
227 return true;
228 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700229
Ian Rogers30fab402012-01-23 15:43:46 -0800230 private:
231 friend class Space;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700232
Ian Rogers30fab402012-01-23 15:43:46 -0800233 ImageSpace(const std::string& name, MemMap* mem_map) :
234 Space(name, mem_map, mem_map->End()) {}
Elliott Hughes307f75d2011-10-12 18:04:40 -0700235
Ian Rogers30fab402012-01-23 15:43:46 -0800236 DISALLOW_COPY_AND_ASSIGN(ImageSpace);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700237};
238
239} // namespace art
240
241#endif // ART_SRC_SPACE_H_