blob: f6d119110bcc2f300d07afa24fa2a31d6f917b1d [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
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "space.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -07004
Brian Carlstrom9b7f2c22011-09-27 14:35:04 -07005#include "common_test.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07006#include "globals.h"
Elliott Hughes90a33692011-08-30 13:27:07 -07007#include "UniquePtr.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -07008
9namespace art {
10
Brian Carlstrom9b7f2c22011-09-27 14:35:04 -070011class SpaceTest : public CommonTest {};
12
13TEST_F(SpaceTest, Init) {
Carl Shapiro69759ea2011-07-21 18:13:35 -070014 {
jeffhaoc1160702011-10-27 15:48:45 -070015 // Init < max == growth
Ian Rogers30fab402012-01-23 15:43:46 -080016 UniquePtr<Space> space(Space::CreateAllocSpace("test", 16 * MB, 32 * MB, 32 * MB, NULL));
Elliott Hughes90a33692011-08-30 13:27:07 -070017 EXPECT_TRUE(space.get() != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -070018 }
19 {
jeffhaoc1160702011-10-27 15:48:45 -070020 // Init == max == growth
Ian Rogers30fab402012-01-23 15:43:46 -080021 UniquePtr<Space> space(Space::CreateAllocSpace("test", 16 * MB, 16 * MB, 16 * MB, NULL));
Elliott Hughes90a33692011-08-30 13:27:07 -070022 EXPECT_TRUE(space.get() != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -070023 }
24 {
jeffhaoc1160702011-10-27 15:48:45 -070025 // Init > max == growth
Ian Rogers30fab402012-01-23 15:43:46 -080026 UniquePtr<Space> space(Space::CreateAllocSpace("test", 32 * MB, 16 * MB, 16 * MB, NULL));
jeffhaoc1160702011-10-27 15:48:45 -070027 EXPECT_TRUE(space.get() == NULL);
28 }
29 {
30 // Growth == init < max
Ian Rogers30fab402012-01-23 15:43:46 -080031 UniquePtr<Space> space(Space::CreateAllocSpace("test", 16 * MB, 16 * MB, 32 * MB, NULL));
jeffhaoc1160702011-10-27 15:48:45 -070032 EXPECT_TRUE(space.get() != NULL);
33 }
34 {
35 // Growth < init < max
Ian Rogers30fab402012-01-23 15:43:46 -080036 UniquePtr<Space> space(Space::CreateAllocSpace("test", 16 * MB, 8 * MB, 32 * MB, NULL));
jeffhaoc1160702011-10-27 15:48:45 -070037 EXPECT_TRUE(space.get() == NULL);
38 }
39 {
40 // Init < growth < max
Ian Rogers30fab402012-01-23 15:43:46 -080041 UniquePtr<Space> space(Space::CreateAllocSpace("test", 8 * MB, 16 * MB, 32 * MB, NULL));
jeffhaoc1160702011-10-27 15:48:45 -070042 EXPECT_TRUE(space.get() != NULL);
43 }
44 {
45 // Init < max < growth
Ian Rogers30fab402012-01-23 15:43:46 -080046 UniquePtr<Space> space(Space::CreateAllocSpace("test", 8 * MB, 32 * MB, 16 * MB, NULL));
Elliott Hughes90a33692011-08-30 13:27:07 -070047 EXPECT_TRUE(space.get() == NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -070048 }
49}
50
Brian Carlstrom9b7f2c22011-09-27 14:35:04 -070051TEST_F(SpaceTest, AllocAndFree) {
Ian Rogers30fab402012-01-23 15:43:46 -080052 AllocSpace* space(Space::CreateAllocSpace("test", 4 * MB, 16 * MB, 16 * MB, NULL));
53 ASSERT_TRUE(space != NULL);
54
55 // Make space findable to the heap, will also delete class when runtime is cleaned up
56 Heap::AddSpace(space);
Carl Shapiro69759ea2011-07-21 18:13:35 -070057
58 // Succeeds, fits without adjusting the max allowed footprint.
Ian Rogers30fab402012-01-23 15:43:46 -080059 Object* ptr1 = space->AllocWithoutGrowth(1 * MB);
Carl Shapiro69759ea2011-07-21 18:13:35 -070060 EXPECT_TRUE(ptr1 != NULL);
61
62 // Fails, requires a higher allowed footprint.
Ian Rogers30fab402012-01-23 15:43:46 -080063 Object* ptr2 = space->AllocWithoutGrowth(8 * MB);
Carl Shapiro69759ea2011-07-21 18:13:35 -070064 EXPECT_TRUE(ptr2 == NULL);
65
66 // Succeeds, adjusts the footprint.
Ian Rogers30fab402012-01-23 15:43:46 -080067 Object* ptr3 = space->AllocWithGrowth(8 * MB);
Carl Shapiro69759ea2011-07-21 18:13:35 -070068 EXPECT_TRUE(ptr3 != NULL);
69
70 // Fails, requires a higher allowed footprint.
Ian Rogers30fab402012-01-23 15:43:46 -080071 Object* ptr4 = space->AllocWithoutGrowth(8 * MB);
Carl Shapiro69759ea2011-07-21 18:13:35 -070072 EXPECT_FALSE(ptr4 != NULL);
73
74 // Also fails, requires a higher allowed footprint.
Ian Rogers30fab402012-01-23 15:43:46 -080075 Object* ptr5 = space->AllocWithGrowth(8 * MB);
Carl Shapiro69759ea2011-07-21 18:13:35 -070076 EXPECT_FALSE(ptr5 != NULL);
77
78 // Release some memory.
Ian Rogers30fab402012-01-23 15:43:46 -080079 size_t free3 = space->AllocationSize(ptr3);
80 space->Free(ptr3);
Carl Shapiro69759ea2011-07-21 18:13:35 -070081 EXPECT_LE(8U * MB, free3);
82
83 // Succeeds, now that memory has been freed.
84 void* ptr6 = space->AllocWithGrowth(9 * MB);
85 EXPECT_TRUE(ptr6 != NULL);
86
87 // Final clean up.
Ian Rogers30fab402012-01-23 15:43:46 -080088 size_t free1 = space->AllocationSize(ptr1);
89 space->Free(ptr1);
Carl Shapiro69759ea2011-07-21 18:13:35 -070090 EXPECT_LE(1U * MB, free1);
91}
92
93} // namespace art