blob: 1536c27ae2cb0f910814f71f2443fc8b677c983e [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 Carlstrom0a5b14d2011-09-27 13:29:15 -070026 static Space* CreateFromImage(const char* 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 Carlstroma663ea52011-08-19 23:33:41 -070054 const ImageHeader& GetImageHeader() const {
55 CHECK(image_header_ != NULL);
56 return *image_header_;
57 }
58
Carl Shapiro58551df2011-07-24 03:09:51 -070059 size_t AllocationSize(const Object* obj);
60
Brian Carlstromdb4d5402011-08-09 12:18:28 -070061 bool IsCondemned() const {
Brian Carlstrom16192862011-09-12 17:50:06 -070062 return mspace_ != NULL;
Carl Shapiro58551df2011-07-24 03:09:51 -070063 }
64
Carl Shapiro69759ea2011-07-21 18:13:35 -070065 private:
Carl Shapiro58551df2011-07-24 03:09:51 -070066 // The boundary tag overhead.
67 static const size_t kChunkOverhead = kWordSize;
68
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070069 // create a Space from an existing memory mapping, taking ownership of the address space.
70 static Space* Create(MemMap* mem_map);
71
Brian Carlstroma663ea52011-08-19 23:33:41 -070072 Space() : mspace_(NULL), maximum_size_(0), image_header_(NULL), base_(0), limit_(0) {}
Carl Shapiro69759ea2011-07-21 18:13:35 -070073
Carl Shapiro58551df2011-07-24 03:09:51 -070074 // Initializes the space and underlying storage.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070075 bool Init(size_t initial_size, size_t maximum_size, byte* requested_base);
Carl Shapiro69759ea2011-07-21 18:13:35 -070076
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070077 // Initializes the space from existing storage, taking ownership of the storage.
78 void Init(MemMap* map);
79
80 // Initializes the space from an image file
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -070081 bool InitFromImage(const char* image_file_name);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070082
83 void* CreateMallocSpace(void* base, size_t initial_size, size_t maximum_size);
Carl Shapiro69759ea2011-07-21 18:13:35 -070084
85 static void DontNeed(void* start, void* end, void* num_bytes);
86
Brian Carlstroma663ea52011-08-19 23:33:41 -070087 // TODO: have a Space subclass for non-image Spaces with mspace_ and maximum_size_
Carl Shapiro69759ea2011-07-21 18:13:35 -070088 void* mspace_;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070089 size_t maximum_size_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070090
Brian Carlstroma663ea52011-08-19 23:33:41 -070091 // TODO: have a Space subclass for image Spaces with image_header_
92 ImageHeader* image_header_;
93
Elliott Hughes90a33692011-08-30 13:27:07 -070094 UniquePtr<MemMap> mem_map_;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070095
Carl Shapiro69759ea2011-07-21 18:13:35 -070096 byte* base_;
97
98 byte* limit_;
99
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700100 DISALLOW_COPY_AND_ASSIGN(Space);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700101};
102
103} // namespace art
104
105#endif // ART_SRC_SPACE_H_