blob: 758d80c0a1a8953a62fbece51918f12f4b4ba539 [file] [log] [blame]
Carl Shapiro69759ea2011-07-21 18:13:35 -07001// Copyright 2011 Google Inc. All Rights Reserved.
Carl Shapiro69759ea2011-07-21 18:13:35 -07002
3#ifndef ART_SRC_SPACE_H_
4#define ART_SRC_SPACE_H_
5
Elliott Hughes90a33692011-08-30 13:27:07 -07006#include "UniquePtr.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07007#include "globals.h"
Brian Carlstroma663ea52011-08-19 23:33:41 -07008#include "image.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07009#include "macros.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070010#include "mem_map.h"
Shih-wei Liao8c2f6412011-10-03 22:58:14 -070011#include "mspace.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070012
13namespace art {
14
15class Object;
16
17// A space contains memory allocated for managed objects.
18class Space {
19 public:
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -070020 // Create a Space with the requested sizes. The requested
21 // base address is not guaranteed to be granted, if it is required,
22 // the caller should call GetBase on the returned space to confirm
23 // the request was granted.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070024 static Space* Create(size_t initial_size, size_t maximum_size, byte* requested_base);
25
26 // create a Space from an image file. cannot be used for future allocation or collected.
Brian Carlstrom58ae9412011-10-04 00:56:06 -070027 static Space* CreateFromImage(const std::string& image);
Carl Shapiro69759ea2011-07-21 18:13:35 -070028
29 ~Space();
30
31 Object* AllocWithGrowth(size_t num_bytes);
32
33 Object* AllocWithoutGrowth(size_t num_bytes);
34
35 size_t Free(void* ptr);
36
37 void Trim();
38
Shih-wei Liao7f1caab2011-10-06 12:11:04 -070039 size_t GetMaxAllowedFootprint();
40 void SetMaxAllowedFootprint(size_t limit);
Carl Shapiro69759ea2011-07-21 18:13:35 -070041
42 void Grow(size_t num_bytes);
43
Brian Carlstromdb4d5402011-08-09 12:18:28 -070044 byte* GetBase() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -070045 return base_;
46 }
47
Brian Carlstromdb4d5402011-08-09 12:18:28 -070048 byte* GetLimit() const {
Carl Shapiro58551df2011-07-24 03:09:51 -070049 return limit_;
50 }
51
Brian Carlstromdb4d5402011-08-09 12:18:28 -070052 size_t Size() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -070053 return limit_ - base_;
54 }
55
Brian Carlstrom58ae9412011-10-04 00:56:06 -070056 bool IsImageSpace() const {
57 return (image_header_ != NULL);
58 }
59
Brian Carlstroma663ea52011-08-19 23:33:41 -070060 const ImageHeader& GetImageHeader() const {
Brian Carlstrom58ae9412011-10-04 00:56:06 -070061 CHECK(IsImageSpace());
Brian Carlstroma663ea52011-08-19 23:33:41 -070062 return *image_header_;
63 }
64
Carl Shapiro58551df2011-07-24 03:09:51 -070065 size_t AllocationSize(const Object* obj);
66
Brian Carlstromdb4d5402011-08-09 12:18:28 -070067 bool IsCondemned() const {
Brian Carlstrom16192862011-09-12 17:50:06 -070068 return mspace_ != NULL;
Carl Shapiro58551df2011-07-24 03:09:51 -070069 }
70
Carl Shapiro69759ea2011-07-21 18:13:35 -070071 private:
Carl Shapiro58551df2011-07-24 03:09:51 -070072 // The boundary tag overhead.
73 static const size_t kChunkOverhead = kWordSize;
74
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070075 // create a Space from an existing memory mapping, taking ownership of the address space.
76 static Space* Create(MemMap* mem_map);
77
Brian Carlstroma663ea52011-08-19 23:33:41 -070078 Space() : mspace_(NULL), maximum_size_(0), image_header_(NULL), base_(0), limit_(0) {}
Carl Shapiro69759ea2011-07-21 18:13:35 -070079
Carl Shapiro58551df2011-07-24 03:09:51 -070080 // Initializes the space and underlying storage.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070081 bool Init(size_t initial_size, size_t maximum_size, byte* requested_base);
Carl Shapiro69759ea2011-07-21 18:13:35 -070082
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070083 // Initializes the space from existing storage, taking ownership of the storage.
84 void Init(MemMap* map);
85
86 // Initializes the space from an image file
Brian Carlstrom58ae9412011-10-04 00:56:06 -070087 bool InitFromImage(const std::string& image_file_name);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070088
89 void* CreateMallocSpace(void* base, size_t initial_size, size_t maximum_size);
Carl Shapiro69759ea2011-07-21 18:13:35 -070090
91 static void DontNeed(void* start, void* end, void* num_bytes);
92
Brian Carlstroma663ea52011-08-19 23:33:41 -070093 // TODO: have a Space subclass for non-image Spaces with mspace_ and maximum_size_
Carl Shapiro69759ea2011-07-21 18:13:35 -070094 void* mspace_;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070095 size_t maximum_size_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070096
Brian Carlstroma663ea52011-08-19 23:33:41 -070097 // TODO: have a Space subclass for image Spaces with image_header_
98 ImageHeader* image_header_;
99
Elliott Hughes90a33692011-08-30 13:27:07 -0700100 UniquePtr<MemMap> mem_map_;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700101
Carl Shapiro69759ea2011-07-21 18:13:35 -0700102 byte* base_;
103
104 byte* limit_;
105
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700106 DISALLOW_COPY_AND_ASSIGN(Space);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700107};
108
109} // namespace art
110
111#endif // ART_SRC_SPACE_H_