blob: 1e2a58b3dc81247b02cddd59688268ca5a134939 [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"
Carl Shapiro69759ea2011-07-21 18:13:35 -070011
12namespace art {
13
14class Object;
15
16// A space contains memory allocated for managed objects.
17class Space {
18 public:
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -070019 // Create a Space with the requested sizes. The requested
20 // base address is not guaranteed to be granted, if it is required,
21 // the caller should call GetBase on the returned space to confirm
22 // the request was granted.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070023 static Space* Create(size_t initial_size, size_t maximum_size, byte* requested_base);
24
25 // create a Space from an image file. cannot be used for future allocation or collected.
Brian Carlstrom58ae9412011-10-04 00:56:06 -070026 static Space* CreateFromImage(const std::string& image);
Carl Shapiro69759ea2011-07-21 18:13:35 -070027
28 ~Space();
29
30 Object* AllocWithGrowth(size_t num_bytes);
31
32 Object* AllocWithoutGrowth(size_t num_bytes);
33
34 size_t Free(void* ptr);
35
36 void Trim();
37
38 size_t MaxAllowedFootprint();
39
40 void Grow(size_t num_bytes);
41
Brian Carlstromdb4d5402011-08-09 12:18:28 -070042 byte* GetBase() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -070043 return base_;
44 }
45
Brian Carlstromdb4d5402011-08-09 12:18:28 -070046 byte* GetLimit() const {
Carl Shapiro58551df2011-07-24 03:09:51 -070047 return limit_;
48 }
49
Brian Carlstromdb4d5402011-08-09 12:18:28 -070050 size_t Size() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -070051 return limit_ - base_;
52 }
53
Brian Carlstrom58ae9412011-10-04 00:56:06 -070054 bool IsImageSpace() const {
55 return (image_header_ != NULL);
56 }
57
Brian Carlstroma663ea52011-08-19 23:33:41 -070058 const ImageHeader& GetImageHeader() const {
Brian Carlstrom58ae9412011-10-04 00:56:06 -070059 CHECK(IsImageSpace());
Brian Carlstroma663ea52011-08-19 23:33:41 -070060 return *image_header_;
61 }
62
Carl Shapiro58551df2011-07-24 03:09:51 -070063 size_t AllocationSize(const Object* obj);
64
Brian Carlstromdb4d5402011-08-09 12:18:28 -070065 bool IsCondemned() const {
Brian Carlstrom16192862011-09-12 17:50:06 -070066 return mspace_ != NULL;
Carl Shapiro58551df2011-07-24 03:09:51 -070067 }
68
Carl Shapiro69759ea2011-07-21 18:13:35 -070069 private:
Carl Shapiro58551df2011-07-24 03:09:51 -070070 // The boundary tag overhead.
71 static const size_t kChunkOverhead = kWordSize;
72
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070073 // create a Space from an existing memory mapping, taking ownership of the address space.
74 static Space* Create(MemMap* mem_map);
75
Brian Carlstroma663ea52011-08-19 23:33:41 -070076 Space() : mspace_(NULL), maximum_size_(0), image_header_(NULL), base_(0), limit_(0) {}
Carl Shapiro69759ea2011-07-21 18:13:35 -070077
Carl Shapiro58551df2011-07-24 03:09:51 -070078 // Initializes the space and underlying storage.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070079 bool Init(size_t initial_size, size_t maximum_size, byte* requested_base);
Carl Shapiro69759ea2011-07-21 18:13:35 -070080
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070081 // Initializes the space from existing storage, taking ownership of the storage.
82 void Init(MemMap* map);
83
84 // Initializes the space from an image file
Brian Carlstrom58ae9412011-10-04 00:56:06 -070085 bool InitFromImage(const std::string& image_file_name);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070086
87 void* CreateMallocSpace(void* base, size_t initial_size, size_t maximum_size);
Carl Shapiro69759ea2011-07-21 18:13:35 -070088
89 static void DontNeed(void* start, void* end, void* num_bytes);
90
Brian Carlstroma663ea52011-08-19 23:33:41 -070091 // TODO: have a Space subclass for non-image Spaces with mspace_ and maximum_size_
Carl Shapiro69759ea2011-07-21 18:13:35 -070092 void* mspace_;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070093 size_t maximum_size_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070094
Brian Carlstroma663ea52011-08-19 23:33:41 -070095 // TODO: have a Space subclass for image Spaces with image_header_
96 ImageHeader* image_header_;
97
Elliott Hughes90a33692011-08-30 13:27:07 -070098 UniquePtr<MemMap> mem_map_;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070099
Carl Shapiro69759ea2011-07-21 18:13:35 -0700100 byte* base_;
101
102 byte* limit_;
103
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700104 DISALLOW_COPY_AND_ASSIGN(Space);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700105};
106
107} // namespace art
108
109#endif // ART_SRC_SPACE_H_