| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "platform/assert.h" | 5 #include "platform/assert.h" |
| 6 #include "vm/freelist.h" | 6 #include "vm/freelist.h" |
| 7 #include "vm/unit_test.h" | 7 #include "vm/unit_test.h" |
| 8 | 8 |
| 9 namespace dart { | 9 namespace dart { |
| 10 | 10 |
| 11 TEST_CASE(FreeList) { | 11 TEST_CASE(FreeList) { |
| 12 FreeList* free_list = new FreeList(); | 12 FreeList* free_list = new FreeList(); |
| 13 intptr_t kBlobSize = 1 * MB; | 13 intptr_t kBlobSize = 1 * MB; |
| 14 intptr_t kSmallObjectSize = 4 * kWordSize; | 14 intptr_t kSmallObjectSize = 4 * kWordSize; |
| 15 intptr_t kMediumObjectSize = 16 * kWordSize; | 15 intptr_t kMediumObjectSize = 16 * kWordSize; |
| 16 intptr_t kLargeObjectSize = 8 * KB; | 16 intptr_t kLargeObjectSize = 8 * KB; |
| 17 uword blob = reinterpret_cast<uword>(malloc(kBlobSize)); | 17 uword blob = reinterpret_cast<uword>(malloc(kBlobSize)); |
| 18 // Enqueue the large blob as one free block. | 18 // Enqueue the large blob as one free block. |
| 19 free_list->Free(blob, kBlobSize); | 19 free_list->Free(blob, kBlobSize); |
| 20 // Allocate a small object. Expect it to be positioned as the first element. | 20 // Allocate a small object. Expect it to be positioned as the first element. |
| 21 uword small_object = free_list->TryAllocate(kSmallObjectSize); | 21 uword small_object = free_list->TryAllocate(kSmallObjectSize, false); |
| 22 EXPECT_EQ(blob, small_object); | 22 EXPECT_EQ(blob, small_object); |
| 23 // Freeing and allocating should give us the same memory back. | 23 // Freeing and allocating should give us the same memory back. |
| 24 free_list->Free(small_object, kSmallObjectSize); | 24 free_list->Free(small_object, kSmallObjectSize); |
| 25 small_object = free_list->TryAllocate(kSmallObjectSize); | 25 small_object = free_list->TryAllocate(kSmallObjectSize, false); |
| 26 EXPECT_EQ(blob, small_object); | 26 EXPECT_EQ(blob, small_object); |
| 27 // Splitting the remainder further with small and medium objects. | 27 // Splitting the remainder further with small and medium objects. |
| 28 uword small_object2 = free_list->TryAllocate(kSmallObjectSize); | 28 uword small_object2 = free_list->TryAllocate(kSmallObjectSize, false); |
| 29 EXPECT_EQ(blob + kSmallObjectSize, small_object2); | 29 EXPECT_EQ(blob + kSmallObjectSize, small_object2); |
| 30 uword med_object = free_list->TryAllocate(kMediumObjectSize); | 30 uword med_object = free_list->TryAllocate(kMediumObjectSize, false); |
| 31 EXPECT_EQ(small_object2 + kSmallObjectSize, med_object); | 31 EXPECT_EQ(small_object2 + kSmallObjectSize, med_object); |
| 32 // Allocate a large object. | 32 // Allocate a large object. |
| 33 uword large_object = free_list->TryAllocate(kLargeObjectSize); | 33 uword large_object = free_list->TryAllocate(kLargeObjectSize, false); |
| 34 EXPECT_EQ(med_object + kMediumObjectSize, large_object); | 34 EXPECT_EQ(med_object + kMediumObjectSize, large_object); |
| 35 // Make sure that small objects can still split the remainder. | 35 // Make sure that small objects can still split the remainder. |
| 36 uword small_object3 = free_list->TryAllocate(kSmallObjectSize); | 36 uword small_object3 = free_list->TryAllocate(kSmallObjectSize, false); |
| 37 EXPECT_EQ(large_object + kLargeObjectSize, small_object3); | 37 EXPECT_EQ(large_object + kLargeObjectSize, small_object3); |
| 38 // Split the large object. | 38 // Split the large object. |
| 39 free_list->Free(large_object, kLargeObjectSize); | 39 free_list->Free(large_object, kLargeObjectSize); |
| 40 uword small_object4 = free_list->TryAllocate(kSmallObjectSize); | 40 uword small_object4 = free_list->TryAllocate(kSmallObjectSize, false); |
| 41 EXPECT_EQ(large_object, small_object4); | 41 EXPECT_EQ(large_object, small_object4); |
| 42 // Get the full remainder of the large object. | 42 // Get the full remainder of the large object. |
| 43 large_object = free_list->TryAllocate(kLargeObjectSize - kSmallObjectSize); | 43 large_object = |
| 44 free_list->TryAllocate(kLargeObjectSize - kSmallObjectSize, false); |
| 44 EXPECT_EQ(small_object4 + kSmallObjectSize, large_object); | 45 EXPECT_EQ(small_object4 + kSmallObjectSize, large_object); |
| 45 // Get another large object from the large unallocated remainder. | 46 // Get another large object from the large unallocated remainder. |
| 46 uword large_object2 = free_list->TryAllocate(kLargeObjectSize); | 47 uword large_object2 = free_list->TryAllocate(kLargeObjectSize, false); |
| 47 EXPECT_EQ(small_object3 + kSmallObjectSize, large_object2); | 48 EXPECT_EQ(small_object3 + kSmallObjectSize, large_object2); |
| 48 // Delete the memory associated with the test. | 49 // Delete the memory associated with the test. |
| 49 free(reinterpret_cast<void*>(blob)); | 50 free(reinterpret_cast<void*>(blob)); |
| 50 delete free_list; | 51 delete free_list; |
| 51 } | 52 } |
| 52 | 53 |
| 53 } // namespace dart | 54 } // namespace dart |
| OLD | NEW |