Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 1 | /* |
| 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 Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 16 | |
| 17 | #ifndef ART_SRC_SPACE_H_ |
| 18 | #define ART_SRC_SPACE_H_ |
| 19 | |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 20 | #include <string> |
| 21 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 22 | #include "UniquePtr.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 23 | #include "globals.h" |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 24 | #include "image.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 25 | #include "macros.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 26 | #include "mem_map.h" |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 27 | #include "mspace.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 28 | |
| 29 | namespace art { |
| 30 | |
| 31 | class Object; |
| 32 | |
| 33 | // A space contains memory allocated for managed objects. |
| 34 | class Space { |
| 35 | public: |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 36 | // 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 Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 40 | static Space* Create(const std::string& name, |
| 41 | size_t initial_size, size_t maximum_size, byte* requested_base); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 42 | |
| 43 | // create a Space from an image file. cannot be used for future allocation or collected. |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 44 | static Space* CreateFromImage(const std::string& image); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 45 | |
| 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 Liao | 7f1caab | 2011-10-06 12:11:04 -0700 | [diff] [blame] | 56 | size_t GetMaxAllowedFootprint(); |
| 57 | void SetMaxAllowedFootprint(size_t limit); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 58 | |
| 59 | void Grow(size_t num_bytes); |
| 60 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 61 | byte* GetBase() const { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 62 | return base_; |
| 63 | } |
| 64 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 65 | byte* GetLimit() const { |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 66 | return limit_; |
| 67 | } |
| 68 | |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 69 | const std::string& GetName() const { |
| 70 | return name_; |
| 71 | } |
| 72 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 73 | size_t Size() const { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 74 | return limit_ - base_; |
| 75 | } |
| 76 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 77 | bool IsImageSpace() const { |
| 78 | return (image_header_ != NULL); |
| 79 | } |
| 80 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 81 | const ImageHeader& GetImageHeader() const { |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 82 | CHECK(IsImageSpace()); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 83 | return *image_header_; |
| 84 | } |
| 85 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 86 | size_t AllocationSize(const Object* obj); |
| 87 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 88 | private: |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 89 | // The boundary tag overhead. |
| 90 | static const size_t kChunkOverhead = kWordSize; |
| 91 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 92 | // create a Space from an existing memory mapping, taking ownership of the address space. |
| 93 | static Space* Create(MemMap* mem_map); |
| 94 | |
Elliott Hughes | a51a3dd | 2011-10-17 15:19:26 -0700 | [diff] [blame] | 95 | explicit Space(const std::string& name) |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 96 | : name_(name), mspace_(NULL), maximum_size_(0), image_header_(NULL), base_(0), limit_(0) { |
| 97 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 98 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 99 | // Initializes the space and underlying storage. |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 100 | bool Init(size_t initial_size, size_t maximum_size, byte* requested_base); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 101 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 102 | // 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 Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 106 | bool InitFromImage(const std::string& image_file_name); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 107 | |
| 108 | void* CreateMallocSpace(void* base, size_t initial_size, size_t maximum_size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 109 | |
| 110 | static void DontNeed(void* start, void* end, void* num_bytes); |
| 111 | |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 112 | std::string name_; |
| 113 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 114 | // TODO: have a Space subclass for non-image Spaces with mspace_ and maximum_size_ |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 115 | void* mspace_; |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 116 | size_t maximum_size_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 117 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 118 | // TODO: have a Space subclass for image Spaces with image_header_ |
| 119 | ImageHeader* image_header_; |
| 120 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 121 | UniquePtr<MemMap> mem_map_; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 122 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 123 | byte* base_; |
| 124 | |
| 125 | byte* limit_; |
| 126 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 127 | DISALLOW_COPY_AND_ASSIGN(Space); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | } // namespace art |
| 131 | |
| 132 | #endif // ART_SRC_SPACE_H_ |