blob: d3123658b051020cde076dc3e03cb251dfcd82f8 [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.
Elliott Hughes307f75d2011-10-12 18:04:40 -070040 static Space* Create(const std::string& name,
41 size_t initial_size, size_t maximum_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 {
Carl Shapiro58551df2011-07-24 03:09:51 -070066 return limit_;
67 }
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 {
Carl Shapiro69759ea2011-07-21 18:13:35 -070074 return limit_ - base_;
75 }
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
Carl Shapiro58551df2011-07-24 03:09:51 -070086 size_t AllocationSize(const Object* obj);
87
Carl Shapiro69759ea2011-07-21 18:13:35 -070088 private:
Carl Shapiro58551df2011-07-24 03:09:51 -070089 // The boundary tag overhead.
90 static const size_t kChunkOverhead = kWordSize;
91
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070092 // create a Space from an existing memory mapping, taking ownership of the address space.
93 static Space* Create(MemMap* mem_map);
94
Elliott Hughesa51a3dd2011-10-17 15:19:26 -070095 explicit Space(const std::string& name)
Elliott Hughes307f75d2011-10-12 18:04:40 -070096 : name_(name), mspace_(NULL), maximum_size_(0), image_header_(NULL), base_(0), limit_(0) {
97 }
Carl Shapiro69759ea2011-07-21 18:13:35 -070098
Carl Shapiro58551df2011-07-24 03:09:51 -070099 // Initializes the space and underlying storage.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700100 bool Init(size_t initial_size, size_t maximum_size, byte* requested_base);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700101
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700102 // Initializes the space from existing storage, taking ownership of the storage.
103 void Init(MemMap* map);
104
105 // Initializes the space from an image file
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700106 bool InitFromImage(const std::string& image_file_name);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700107
108 void* CreateMallocSpace(void* base, size_t initial_size, size_t maximum_size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700109
110 static void DontNeed(void* start, void* end, void* num_bytes);
111
Elliott Hughes307f75d2011-10-12 18:04:40 -0700112 std::string name_;
113
Brian Carlstroma663ea52011-08-19 23:33:41 -0700114 // TODO: have a Space subclass for non-image Spaces with mspace_ and maximum_size_
Carl Shapiro69759ea2011-07-21 18:13:35 -0700115 void* mspace_;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700116 size_t maximum_size_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700117
Brian Carlstroma663ea52011-08-19 23:33:41 -0700118 // TODO: have a Space subclass for image Spaces with image_header_
119 ImageHeader* image_header_;
120
Elliott Hughes90a33692011-08-30 13:27:07 -0700121 UniquePtr<MemMap> mem_map_;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700122
Carl Shapiro69759ea2011-07-21 18:13:35 -0700123 byte* base_;
124
125 byte* limit_;
126
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700127 DISALLOW_COPY_AND_ASSIGN(Space);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700128};
129
130} // namespace art
131
132#endif // ART_SRC_SPACE_H_