blob: eff0da5ab323d98e3769f22b52947cbd5bb9a0f0 [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
54 void Trim();
55
Shih-wei Liao7f1caab2011-10-06 12:11:04 -070056 size_t GetMaxAllowedFootprint();
57 void SetMaxAllowedFootprint(size_t limit);
Carl Shapiro69759ea2011-07-21 18:13:35 -070058
59 void Grow(size_t num_bytes);
60
Brian Carlstromdb4d5402011-08-09 12:18:28 -070061 byte* GetBase() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -070062 return base_;
63 }
64
Brian Carlstromdb4d5402011-08-09 12:18:28 -070065 byte* GetLimit() const {
jeffhaoc1160702011-10-27 15:48:45 -070066 return growth_limit_;
Carl Shapiro58551df2011-07-24 03:09:51 -070067 }
68
Elliott Hughes307f75d2011-10-12 18:04:40 -070069 const std::string& GetName() const {
70 return name_;
71 }
72
Brian Carlstromdb4d5402011-08-09 12:18:28 -070073 size_t Size() const {
jeffhaoc1160702011-10-27 15:48:45 -070074 return growth_limit_ - base_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070075 }
76
Brian Carlstrom58ae9412011-10-04 00:56:06 -070077 bool IsImageSpace() const {
78 return (image_header_ != NULL);
79 }
80
Brian Carlstroma663ea52011-08-19 23:33:41 -070081 const ImageHeader& GetImageHeader() const {
Brian Carlstrom58ae9412011-10-04 00:56:06 -070082 CHECK(IsImageSpace());
Brian Carlstroma663ea52011-08-19 23:33:41 -070083 return *image_header_;
84 }
85
jeffhaoc393a4f2011-10-19 13:46:09 -070086 const std::string& GetImageFilename() const {
87 CHECK(IsImageSpace());
88 return name_;
89 }
90
Carl Shapiro58551df2011-07-24 03:09:51 -070091 size_t AllocationSize(const Object* obj);
92
jeffhaoc1160702011-10-27 15:48:45 -070093 void ClearGrowthLimit() {
94 CHECK_GE(maximum_size_, growth_size_);
95 CHECK_GE(limit_, growth_limit_);
96 growth_size_ = maximum_size_;
97 growth_limit_ = limit_;
98 }
99
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700100 void Walk(void(*callback)(const void*, size_t, const void*, size_t, void*), void* arg);
101
Carl Shapiro69759ea2011-07-21 18:13:35 -0700102 private:
Carl Shapiro58551df2011-07-24 03:09:51 -0700103 // The boundary tag overhead.
104 static const size_t kChunkOverhead = kWordSize;
105
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700106 // create a Space from an existing memory mapping, taking ownership of the address space.
107 static Space* Create(MemMap* mem_map);
108
Elliott Hughesa51a3dd2011-10-17 15:19:26 -0700109 explicit Space(const std::string& name)
jeffhaoc1160702011-10-27 15:48:45 -0700110 : name_(name), mspace_(NULL), maximum_size_(0), growth_size_(0),
111 image_header_(NULL), base_(0), limit_(0), growth_limit_(0) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700112 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700113
Carl Shapiro58551df2011-07-24 03:09:51 -0700114 // Initializes the space and underlying storage.
jeffhaoc1160702011-10-27 15:48:45 -0700115 bool Init(size_t initial_size, size_t maximum_size, size_t growth_size, byte* requested_base);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700116
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700117 // Initializes the space from existing storage, taking ownership of the storage.
118 void Init(MemMap* map);
119
120 // Initializes the space from an image file
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700121 bool InitFromImage(const std::string& image_file_name);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700122
123 void* CreateMallocSpace(void* base, size_t initial_size, size_t maximum_size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700124
125 static void DontNeed(void* start, void* end, void* num_bytes);
126
Elliott Hughes307f75d2011-10-12 18:04:40 -0700127 std::string name_;
128
Brian Carlstroma663ea52011-08-19 23:33:41 -0700129 // TODO: have a Space subclass for non-image Spaces with mspace_ and maximum_size_
Carl Shapiro69759ea2011-07-21 18:13:35 -0700130 void* mspace_;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700131 size_t maximum_size_;
jeffhaoc1160702011-10-27 15:48:45 -0700132 size_t growth_size_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700133
Brian Carlstroma663ea52011-08-19 23:33:41 -0700134 // TODO: have a Space subclass for image Spaces with image_header_
135 ImageHeader* image_header_;
136
Elliott Hughes90a33692011-08-30 13:27:07 -0700137 UniquePtr<MemMap> mem_map_;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700138
Carl Shapiro69759ea2011-07-21 18:13:35 -0700139 byte* base_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700140 byte* limit_;
jeffhaoc1160702011-10-27 15:48:45 -0700141 byte* growth_limit_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700142
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700143 DISALLOW_COPY_AND_ASSIGN(Space);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700144};
145
146} // namespace art
147
148#endif // ART_SRC_SPACE_H_