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