blob: 57f676def3f66e5413bff02ad122199afc531cbf [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
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07006#include "globals.h"
7#include "macros.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -07008#include "mem_map.h"
9#include "scoped_ptr.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070010
11namespace art {
12
13class Object;
14
15// A space contains memory allocated for managed objects.
16class Space {
17 public:
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070018 // create a Space with the requested sizes requesting a specific base address.
19 static Space* Create(size_t initial_size, size_t maximum_size, byte* requested_base);
20
21 // create a Space from an image file. cannot be used for future allocation or collected.
22 static Space* Create(const char* image);
Carl Shapiro69759ea2011-07-21 18:13:35 -070023
24 ~Space();
25
26 Object* AllocWithGrowth(size_t num_bytes);
27
28 Object* AllocWithoutGrowth(size_t num_bytes);
29
30 size_t Free(void* ptr);
31
32 void Trim();
33
34 size_t MaxAllowedFootprint();
35
36 void Grow(size_t num_bytes);
37
Brian Carlstromdb4d5402011-08-09 12:18:28 -070038 byte* GetBase() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -070039 return base_;
40 }
41
Brian Carlstromdb4d5402011-08-09 12:18:28 -070042 byte* GetLimit() const {
Carl Shapiro58551df2011-07-24 03:09:51 -070043 return limit_;
44 }
45
Brian Carlstromdb4d5402011-08-09 12:18:28 -070046 size_t Size() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -070047 return limit_ - base_;
48 }
49
Carl Shapiro58551df2011-07-24 03:09:51 -070050 size_t AllocationSize(const Object* obj);
51
Brian Carlstromdb4d5402011-08-09 12:18:28 -070052 bool IsCondemned() const {
Carl Shapiro58551df2011-07-24 03:09:51 -070053 return true; // TODO
54 }
55
Carl Shapiro69759ea2011-07-21 18:13:35 -070056 private:
Carl Shapiro58551df2011-07-24 03:09:51 -070057 // The boundary tag overhead.
58 static const size_t kChunkOverhead = kWordSize;
59
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070060 // create a Space from an existing memory mapping, taking ownership of the address space.
61 static Space* Create(MemMap* mem_map);
62
63 Space() : mspace_(NULL), maximum_size_(0), base_(0), limit_(0) {}
Carl Shapiro69759ea2011-07-21 18:13:35 -070064
Carl Shapiro58551df2011-07-24 03:09:51 -070065 // Initializes the space and underlying storage.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070066 bool Init(size_t initial_size, size_t maximum_size, byte* requested_base);
Carl Shapiro69759ea2011-07-21 18:13:35 -070067
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070068 // Initializes the space from existing storage, taking ownership of the storage.
69 void Init(MemMap* map);
70
71 // Initializes the space from an image file
72 bool Init(const char* image_file_name);
73
74 void* CreateMallocSpace(void* base, size_t initial_size, size_t maximum_size);
Carl Shapiro69759ea2011-07-21 18:13:35 -070075
76 static void DontNeed(void* start, void* end, void* num_bytes);
77
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070078 // TODO: have a Space subclass for methods that depend on mspace_ and maximum_size_
Carl Shapiro69759ea2011-07-21 18:13:35 -070079 void* mspace_;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070080 size_t maximum_size_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070081
Brian Carlstromdb4d5402011-08-09 12:18:28 -070082 scoped_ptr<MemMap> mem_map_;
83
Carl Shapiro69759ea2011-07-21 18:13:35 -070084 byte* base_;
85
86 byte* limit_;
87
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070088 // bool is_condemned_; // TODO: with IsCondemned
Carl Shapiro69759ea2011-07-21 18:13:35 -070089
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070090 DISALLOW_COPY_AND_ASSIGN(Space);
Carl Shapiro69759ea2011-07-21 18:13:35 -070091};
92
93} // namespace art
94
95#endif // ART_SRC_SPACE_H_