blob: b1fb55d3f1fc5830d97218ec2a77538bf8dac7f1 [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
Ian Rogers3bb17a62012-01-27 23:56:44 -080081 // Size of the space without a limit on its growth. By default this is just the Capacity, but
82 // for the allocation space we support starting with a small heap and then extending it.
83 virtual size_t NonGrowthLimitCapacity() const {
Ian Rogers30fab402012-01-23 15:43:46 -080084 return Capacity();
85 }
86
87 ImageSpace* AsImageSpace() {
88 DCHECK(IsImageSpace());
89 return down_cast<ImageSpace*>(this);
90 }
91
92 AllocSpace* AsAllocSpace() {
93 DCHECK(IsAllocSpace());
94 return down_cast<AllocSpace*>(this);
95 }
96
97 virtual bool IsAllocSpace() const = 0;
98 virtual bool IsImageSpace() const = 0;
99
100 protected:
101 Space(const std::string& name, MemMap* mem_map, byte* end) : name_(name), mem_map_(mem_map),
102 begin_(mem_map->Begin()), end_(end) {}
103
104 std::string name_;
105 // Underlying storage of the space
106 UniquePtr<MemMap> mem_map_;
107
108 // The beginning of the storage for fast access (always equals mem_map_->GetAddress())
109 byte* const begin_;
110
111 // Current end of the space
112 byte* end_;
113
114 DISALLOW_COPY_AND_ASSIGN(Space);
115};
116
117std::ostream& operator<<(std::ostream& os, const Space& space);
118
119// An alloc space is a space where objects may be allocated and garbage collected.
120class AllocSpace : public Space {
121 public:
Ian Rogers3bb17a62012-01-27 23:56:44 -0800122 // Allocate num_bytes without allowing the underlying mspace to grow.
Ian Rogers30fab402012-01-23 15:43:46 -0800123 Object* AllocWithGrowth(size_t num_bytes);
124
Ian Rogers3bb17a62012-01-27 23:56:44 -0800125 // Allocate num_bytes allowing the underlying mspace to grow.
Ian Rogers30fab402012-01-23 15:43:46 -0800126 Object* AllocWithoutGrowth(size_t num_bytes);
127
Ian Rogers3bb17a62012-01-27 23:56:44 -0800128 // Return the storage space required by obj.
Ian Rogers30fab402012-01-23 15:43:46 -0800129 size_t AllocationSize(const Object* obj);
130
131 void Free(Object* ptr);
132
133 void FreeList(size_t num_ptrs, Object** ptrs);
134
135 void* MoreCore(intptr_t increment);
136
137 void* GetMspace() const {
138 return mspace_;
139 }
140
Ian Rogers3bb17a62012-01-27 23:56:44 -0800141 // Hand unused pages back to the system.
Ian Rogers30fab402012-01-23 15:43:46 -0800142 void Trim();
143
144 // Perform a mspace_inspect_all which calls back for each allocation chunk. The chunk may not be
Ian Rogers3bb17a62012-01-27 23:56:44 -0800145 // in use, indicated by num_bytes equaling zero.
Ian Rogers30fab402012-01-23 15:43:46 -0800146 void Walk(void(*callback)(void *start, void *end, size_t num_bytes, void* callback_arg),
147 void* arg);
148
Ian Rogers3bb17a62012-01-27 23:56:44 -0800149 // Returns the number of bytes that the heap is allowed to obtain from the system via MoreCore.
Ian Rogers30fab402012-01-23 15:43:46 -0800150 size_t GetFootprintLimit();
151
Ian Rogers3bb17a62012-01-27 23:56:44 -0800152 // Set the maximum number of bytes that the heap is allowed to obtain from the system via
153 // MoreCore. Note this is used to stop the mspace growing beyond the limit to Capacity. When
154 // allocations fail we GC before increasing the footprint limit and allowing the mspace to grow.
Ian Rogers30fab402012-01-23 15:43:46 -0800155 void SetFootprintLimit(size_t limit);
156
Ian Rogers3bb17a62012-01-27 23:56:44 -0800157 // Removes the fork time growth limit on capacity, allowing the application to allocate up to the
158 // maximum reserved size of the heap.
Ian Rogers30fab402012-01-23 15:43:46 -0800159 void ClearGrowthLimit() {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800160 growth_limit_ = NonGrowthLimitCapacity();
Ian Rogers30fab402012-01-23 15:43:46 -0800161 }
162
163 // Override capacity so that we only return the possibly limited capacity
164 virtual size_t Capacity() const {
jeffhaoc1160702011-10-27 15:48:45 -0700165 return growth_limit_;
Carl Shapiro58551df2011-07-24 03:09:51 -0700166 }
167
Ian Rogers3bb17a62012-01-27 23:56:44 -0800168 // The total amount of memory reserved for the alloc space
169 virtual size_t NonGrowthLimitCapacity() const {
Ian Rogers30fab402012-01-23 15:43:46 -0800170 return mem_map_->End() - mem_map_->Begin();
Ian Rogers5d76c432011-10-31 21:42:49 -0700171 }
172
Ian Rogers30fab402012-01-23 15:43:46 -0800173 virtual bool IsAllocSpace() const {
174 return true;
Elliott Hughes307f75d2011-10-12 18:04:40 -0700175 }
176
Ian Rogers30fab402012-01-23 15:43:46 -0800177 virtual bool IsImageSpace() const {
178 return false;
Ian Rogers5d76c432011-10-31 21:42:49 -0700179 }
180
Carl Shapiro69759ea2011-07-21 18:13:35 -0700181 private:
Ian Rogers30fab402012-01-23 15:43:46 -0800182 friend class Space;
183
184 AllocSpace(const std::string& name, MemMap* mem_map, void* mspace, byte* end,
185 size_t growth_limit) :
186 Space(name, mem_map, end), mspace_(mspace), growth_limit_(growth_limit) {
187 CHECK(mspace != NULL);
188 }
189
190 bool Init(size_t initial_size, size_t maximum_size, size_t growth_size, byte* requested_base);
191
Ian Rogers3bb17a62012-01-27 23:56:44 -0800192 static void* CreateMallocSpace(void* base, size_t morecore_start, size_t initial_size);
Ian Rogers30fab402012-01-23 15:43:46 -0800193
Carl Shapiro58551df2011-07-24 03:09:51 -0700194 // The boundary tag overhead.
195 static const size_t kChunkOverhead = kWordSize;
196
Ian Rogers30fab402012-01-23 15:43:46 -0800197 // Underlying malloc space
198 void* const mspace_;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700199
Ian Rogers30fab402012-01-23 15:43:46 -0800200 // The capacity of the alloc space until such time that ClearGrowthLimit is called.
201 // The underlying mem_map_ controls the maximum size we allow the heap to grow to. The growth
202 // limit is a value <= to the mem_map_ capacity used for ergonomic reasons because of the zygote.
203 // Prior to forking the zygote the heap will have a maximally sized mem_map_ but the growth_limit_
204 // will be set to a lower value. The growth_limit_ is used as the capacity of the alloc_space_,
205 // however, capacity normally can't vary. In the case of the growth_limit_ it can be cleared
206 // one time by a call to ClearGrowthLimit.
207 size_t growth_limit_;
208
209 DISALLOW_COPY_AND_ASSIGN(AllocSpace);
210};
211
212// An image space is a space backed with a memory mapped image
213class ImageSpace : public Space {
214 public:
215 const ImageHeader& GetImageHeader() const {
216 return *reinterpret_cast<ImageHeader*>(Begin());
Elliott Hughes307f75d2011-10-12 18:04:40 -0700217 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700218
Ian Rogers30fab402012-01-23 15:43:46 -0800219 const std::string& GetImageFilename() const {
220 return name_;
221 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700222
Ian Rogers30fab402012-01-23 15:43:46 -0800223 // Mark the objects defined in this space in the given live bitmap
224 void RecordImageAllocations(HeapBitmap* live_bitmap) const;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700225
Ian Rogers30fab402012-01-23 15:43:46 -0800226 virtual bool IsAllocSpace() const {
227 return false;
228 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700229
Ian Rogers30fab402012-01-23 15:43:46 -0800230 virtual bool IsImageSpace() const {
231 return true;
232 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700233
Ian Rogers30fab402012-01-23 15:43:46 -0800234 private:
235 friend class Space;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700236
Ian Rogers30fab402012-01-23 15:43:46 -0800237 ImageSpace(const std::string& name, MemMap* mem_map) :
238 Space(name, mem_map, mem_map->End()) {}
Elliott Hughes307f75d2011-10-12 18:04:40 -0700239
Ian Rogers30fab402012-01-23 15:43:46 -0800240 DISALLOW_COPY_AND_ASSIGN(ImageSpace);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700241};
242
243} // namespace art
244
245#endif // ART_SRC_SPACE_H_