blob: 83c94a0948a0f3f9f6cb3b1109b806cd9a77b1b6 [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
39 size_t MaxAllowedFootprint();
40
41 void Grow(size_t num_bytes);
42
Brian Carlstromdb4d5402011-08-09 12:18:28 -070043 byte* GetBase() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -070044 return base_;
45 }
46
Brian Carlstromdb4d5402011-08-09 12:18:28 -070047 byte* GetLimit() const {
Carl Shapiro58551df2011-07-24 03:09:51 -070048 return limit_;
49 }
50
Brian Carlstromdb4d5402011-08-09 12:18:28 -070051 size_t Size() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -070052 return limit_ - base_;
53 }
54
Brian Carlstrom58ae9412011-10-04 00:56:06 -070055 bool IsImageSpace() const {
56 return (image_header_ != NULL);
57 }
58
Brian Carlstroma663ea52011-08-19 23:33:41 -070059 const ImageHeader& GetImageHeader() const {
Brian Carlstrom58ae9412011-10-04 00:56:06 -070060 CHECK(IsImageSpace());
Brian Carlstroma663ea52011-08-19 23:33:41 -070061 return *image_header_;
62 }
63
Carl Shapiro58551df2011-07-24 03:09:51 -070064 size_t AllocationSize(const Object* obj);
65
Brian Carlstromdb4d5402011-08-09 12:18:28 -070066 bool IsCondemned() const {
Brian Carlstrom16192862011-09-12 17:50:06 -070067 return mspace_ != NULL;
Carl Shapiro58551df2011-07-24 03:09:51 -070068 }
69
Carl Shapiro69759ea2011-07-21 18:13:35 -070070 private:
Carl Shapiro58551df2011-07-24 03:09:51 -070071 // The boundary tag overhead.
72 static const size_t kChunkOverhead = kWordSize;
73
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070074 // create a Space from an existing memory mapping, taking ownership of the address space.
75 static Space* Create(MemMap* mem_map);
76
Brian Carlstroma663ea52011-08-19 23:33:41 -070077 Space() : mspace_(NULL), maximum_size_(0), image_header_(NULL), base_(0), limit_(0) {}
Carl Shapiro69759ea2011-07-21 18:13:35 -070078
Carl Shapiro58551df2011-07-24 03:09:51 -070079 // Initializes the space and underlying storage.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070080 bool Init(size_t initial_size, size_t maximum_size, byte* requested_base);
Carl Shapiro69759ea2011-07-21 18:13:35 -070081
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070082 // Initializes the space from existing storage, taking ownership of the storage.
83 void Init(MemMap* map);
84
85 // Initializes the space from an image file
Brian Carlstrom58ae9412011-10-04 00:56:06 -070086 bool InitFromImage(const std::string& image_file_name);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070087
88 void* CreateMallocSpace(void* base, size_t initial_size, size_t maximum_size);
Carl Shapiro69759ea2011-07-21 18:13:35 -070089
90 static void DontNeed(void* start, void* end, void* num_bytes);
91
Brian Carlstroma663ea52011-08-19 23:33:41 -070092 // TODO: have a Space subclass for non-image Spaces with mspace_ and maximum_size_
Carl Shapiro69759ea2011-07-21 18:13:35 -070093 void* mspace_;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070094 size_t maximum_size_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070095
Brian Carlstroma663ea52011-08-19 23:33:41 -070096 // TODO: have a Space subclass for image Spaces with image_header_
97 ImageHeader* image_header_;
98
Elliott Hughes90a33692011-08-30 13:27:07 -070099 UniquePtr<MemMap> mem_map_;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700100
Carl Shapiro69759ea2011-07-21 18:13:35 -0700101 byte* base_;
102
103 byte* limit_;
104
Shih-wei Liao8c2f6412011-10-03 22:58:14 -0700105 friend class Heap;
106
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700107 DISALLOW_COPY_AND_ASSIGN(Space);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700108};
109
110} // namespace art
111
112#endif // ART_SRC_SPACE_H_