blob: 24bcefadd207bdc548b4f2afca5b6648ecb5548d [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"
Brian Carlstroma663ea52011-08-19 23:33:41 -07007#include "image.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07008#include "macros.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -07009#include "mem_map.h"
10#include "scoped_ptr.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 Carlstrom4a289ed2011-08-16 17:17:49 -070019 // create a Space with the requested sizes requesting a specific base address.
20 static Space* Create(size_t initial_size, size_t maximum_size, byte* requested_base);
21
22 // create a Space from an image file. cannot be used for future allocation or collected.
23 static Space* Create(const char* image);
Carl Shapiro69759ea2011-07-21 18:13:35 -070024
25 ~Space();
26
27 Object* AllocWithGrowth(size_t num_bytes);
28
29 Object* AllocWithoutGrowth(size_t num_bytes);
30
31 size_t Free(void* ptr);
32
33 void Trim();
34
35 size_t MaxAllowedFootprint();
36
37 void Grow(size_t num_bytes);
38
Brian Carlstromdb4d5402011-08-09 12:18:28 -070039 byte* GetBase() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -070040 return base_;
41 }
42
Brian Carlstromdb4d5402011-08-09 12:18:28 -070043 byte* GetLimit() const {
Carl Shapiro58551df2011-07-24 03:09:51 -070044 return limit_;
45 }
46
Brian Carlstromdb4d5402011-08-09 12:18:28 -070047 size_t Size() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -070048 return limit_ - base_;
49 }
50
Brian Carlstroma663ea52011-08-19 23:33:41 -070051 const ImageHeader& GetImageHeader() const {
52 CHECK(image_header_ != NULL);
53 return *image_header_;
54 }
55
Carl Shapiro58551df2011-07-24 03:09:51 -070056 size_t AllocationSize(const Object* obj);
57
Brian Carlstromdb4d5402011-08-09 12:18:28 -070058 bool IsCondemned() const {
Carl Shapiro58551df2011-07-24 03:09:51 -070059 return true; // TODO
60 }
61
Carl Shapiro69759ea2011-07-21 18:13:35 -070062 private:
Carl Shapiro58551df2011-07-24 03:09:51 -070063 // The boundary tag overhead.
64 static const size_t kChunkOverhead = kWordSize;
65
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070066 // create a Space from an existing memory mapping, taking ownership of the address space.
67 static Space* Create(MemMap* mem_map);
68
Brian Carlstroma663ea52011-08-19 23:33:41 -070069 Space() : mspace_(NULL), maximum_size_(0), image_header_(NULL), base_(0), limit_(0) {}
Carl Shapiro69759ea2011-07-21 18:13:35 -070070
Carl Shapiro58551df2011-07-24 03:09:51 -070071 // Initializes the space and underlying storage.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070072 bool Init(size_t initial_size, size_t maximum_size, byte* requested_base);
Carl Shapiro69759ea2011-07-21 18:13:35 -070073
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070074 // Initializes the space from existing storage, taking ownership of the storage.
75 void Init(MemMap* map);
76
77 // Initializes the space from an image file
78 bool Init(const char* image_file_name);
79
80 void* CreateMallocSpace(void* base, size_t initial_size, size_t maximum_size);
Carl Shapiro69759ea2011-07-21 18:13:35 -070081
82 static void DontNeed(void* start, void* end, void* num_bytes);
83
Brian Carlstroma663ea52011-08-19 23:33:41 -070084 // TODO: have a Space subclass for non-image Spaces with mspace_ and maximum_size_
Carl Shapiro69759ea2011-07-21 18:13:35 -070085 void* mspace_;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070086 size_t maximum_size_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070087
Brian Carlstroma663ea52011-08-19 23:33:41 -070088 // TODO: have a Space subclass for image Spaces with image_header_
89 ImageHeader* image_header_;
90
Brian Carlstromdb4d5402011-08-09 12:18:28 -070091 scoped_ptr<MemMap> mem_map_;
92
Carl Shapiro69759ea2011-07-21 18:13:35 -070093 byte* base_;
94
95 byte* limit_;
96
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070097 // bool is_condemned_; // TODO: with IsCondemned
Carl Shapiro69759ea2011-07-21 18:13:35 -070098
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070099 DISALLOW_COPY_AND_ASSIGN(Space);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700100};
101
102} // namespace art
103
104#endif // ART_SRC_SPACE_H_