blob: be7dfdc7096f0523893e8bd32125f00ce6142465 [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"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070026#include "mem_map.h"
Shih-wei Liao8c2f6412011-10-03 22:58:14 -070027#include "mspace.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070028
29namespace art {
30
31class Object;
32
33// A space contains memory allocated for managed objects.
34class Space {
35 public:
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -070036 // Create a Space with the requested sizes. The requested
37 // base address is not guaranteed to be granted, if it is required,
38 // the caller should call GetBase on the returned space to confirm
39 // the request was granted.
jeffhaoc1160702011-10-27 15:48:45 -070040 static Space* Create(const std::string& name, size_t initial_size,
41 size_t maximum_size, size_t growth_size, byte* requested_base);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070042
43 // create a Space from an image file. cannot be used for future allocation or collected.
Brian Carlstrom58ae9412011-10-04 00:56:06 -070044 static Space* CreateFromImage(const std::string& image);
Carl Shapiro69759ea2011-07-21 18:13:35 -070045
46 ~Space();
47
48 Object* AllocWithGrowth(size_t num_bytes);
49
50 Object* AllocWithoutGrowth(size_t num_bytes);
51
52 size_t Free(void* ptr);
53
Ian Rogers5d76c432011-10-31 21:42:49 -070054 size_t FreeList(size_t num_ptrs, void** ptrs);
55
Carl Shapiro69759ea2011-07-21 18:13:35 -070056 void Trim();
57
Shih-wei Liao7f1caab2011-10-06 12:11:04 -070058 size_t GetMaxAllowedFootprint();
59 void SetMaxAllowedFootprint(size_t limit);
Carl Shapiro69759ea2011-07-21 18:13:35 -070060
61 void Grow(size_t num_bytes);
62
Brian Carlstromdb4d5402011-08-09 12:18:28 -070063 byte* GetBase() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -070064 return base_;
65 }
66
Brian Carlstromdb4d5402011-08-09 12:18:28 -070067 byte* GetLimit() const {
jeffhaoc1160702011-10-27 15:48:45 -070068 return growth_limit_;
Carl Shapiro58551df2011-07-24 03:09:51 -070069 }
70
Ian Rogers5d76c432011-10-31 21:42:49 -070071 byte* GetMax() const {
72 return base_ + maximum_size_;
73 }
74
Elliott Hughes307f75d2011-10-12 18:04:40 -070075 const std::string& GetName() const {
76 return name_;
77 }
78
Brian Carlstromdb4d5402011-08-09 12:18:28 -070079 size_t Size() const {
jeffhaoc1160702011-10-27 15:48:45 -070080 return growth_limit_ - base_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070081 }
82
Brian Carlstrom58ae9412011-10-04 00:56:06 -070083 bool IsImageSpace() const {
84 return (image_header_ != NULL);
85 }
86
Brian Carlstroma663ea52011-08-19 23:33:41 -070087 const ImageHeader& GetImageHeader() const {
Brian Carlstrom58ae9412011-10-04 00:56:06 -070088 CHECK(IsImageSpace());
Brian Carlstroma663ea52011-08-19 23:33:41 -070089 return *image_header_;
90 }
91
jeffhaoc393a4f2011-10-19 13:46:09 -070092 const std::string& GetImageFilename() const {
93 CHECK(IsImageSpace());
94 return name_;
95 }
96
Carl Shapiro58551df2011-07-24 03:09:51 -070097 size_t AllocationSize(const Object* obj);
98
jeffhaoc1160702011-10-27 15:48:45 -070099 void ClearGrowthLimit() {
100 CHECK_GE(maximum_size_, growth_size_);
101 CHECK_GE(limit_, growth_limit_);
102 growth_size_ = maximum_size_;
103 growth_limit_ = limit_;
104 }
105
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700106 void Walk(void(*callback)(const void*, size_t, const void*, size_t, void*), void* arg);
107
Ian Rogers5d76c432011-10-31 21:42:49 -0700108 bool Contains(const Object* obj) const {
109 const byte* byte_ptr = reinterpret_cast<const byte*>(obj);
110 return GetBase() <= byte_ptr && byte_ptr < GetLimit();
111 }
112
Carl Shapiro69759ea2011-07-21 18:13:35 -0700113 private:
Carl Shapiro58551df2011-07-24 03:09:51 -0700114 // The boundary tag overhead.
115 static const size_t kChunkOverhead = kWordSize;
116
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700117 // create a Space from an existing memory mapping, taking ownership of the address space.
118 static Space* Create(MemMap* mem_map);
119
Elliott Hughesa51a3dd2011-10-17 15:19:26 -0700120 explicit Space(const std::string& name)
jeffhaoc1160702011-10-27 15:48:45 -0700121 : name_(name), mspace_(NULL), maximum_size_(0), growth_size_(0),
122 image_header_(NULL), base_(0), limit_(0), growth_limit_(0) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700123 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700124
Carl Shapiro58551df2011-07-24 03:09:51 -0700125 // Initializes the space and underlying storage.
jeffhaoc1160702011-10-27 15:48:45 -0700126 bool Init(size_t initial_size, size_t maximum_size, size_t growth_size, byte* requested_base);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700127
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700128 // Initializes the space from existing storage, taking ownership of the storage.
Elliott Hughes6c9c06d2011-11-07 16:43:47 -0800129 void InitFromMemMap(MemMap* map);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700130
131 // Initializes the space from an image file
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700132 bool InitFromImage(const std::string& image_file_name);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700133
134 void* CreateMallocSpace(void* base, size_t initial_size, size_t maximum_size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700135
136 static void DontNeed(void* start, void* end, void* num_bytes);
137
Elliott Hughes307f75d2011-10-12 18:04:40 -0700138 std::string name_;
139
Brian Carlstroma663ea52011-08-19 23:33:41 -0700140 // TODO: have a Space subclass for non-image Spaces with mspace_ and maximum_size_
Carl Shapiro69759ea2011-07-21 18:13:35 -0700141 void* mspace_;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700142 size_t maximum_size_;
jeffhaoc1160702011-10-27 15:48:45 -0700143 size_t growth_size_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700144
Brian Carlstroma663ea52011-08-19 23:33:41 -0700145 // TODO: have a Space subclass for image Spaces with image_header_
146 ImageHeader* image_header_;
147
Elliott Hughes90a33692011-08-30 13:27:07 -0700148 UniquePtr<MemMap> mem_map_;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700149
Carl Shapiro69759ea2011-07-21 18:13:35 -0700150 byte* base_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700151 byte* limit_;
jeffhaoc1160702011-10-27 15:48:45 -0700152 byte* growth_limit_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700153
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700154 DISALLOW_COPY_AND_ASSIGN(Space);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700155};
156
157} // namespace art
158
159#endif // ART_SRC_SPACE_H_