OLD | NEW |
| (Empty) |
1 // Copyright (c) 2016, the Dartino 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.md file. | |
4 | |
5 #include <stdio.h> | |
6 #include <cinttypes> | |
7 | |
8 #define TESTING | |
9 #include "src/shared/assert.h" | |
10 #include "src/shared/atomic.h" | |
11 | |
12 #include "platforms/stm/disco_fletch/src/page_allocator.h" | |
13 | |
14 void Test() { | |
15 PageAllocator *allocator = new PageAllocator(); | |
16 void* arena1; | |
17 int rc = posix_memalign(&arena1, PAGE_SIZE, 10); | |
18 EXPECT_EQ(0, rc); | |
19 uintptr_t start = reinterpret_cast<uintptr_t>(arena1); | |
20 int arena1_bit = allocator->AddArena("test", start, PAGE_SIZE * 10); | |
21 EXPECT_EQ(1, arena1_bit); | |
22 | |
23 void* allocated[9]; | |
24 for (int i = 0; i < 9; i++) { | |
25 void* result = allocator->AllocatePages(1); | |
26 allocated[i] = result; | |
27 EXPECT_EQ(start + (i + 1) * PAGE_SIZE, reinterpret_cast<uintptr_t>(result)); | |
28 } | |
29 EXPECT_EQ(reinterpret_cast<void*>(NULL), allocator->AllocatePages(1)); | |
30 for (int i = 0; i < 9; i++) { | |
31 allocator->FreePages(allocated[i], 1); | |
32 void* result = allocator->AllocatePages(1); | |
33 EXPECT_EQ(allocated[i], result); | |
34 EXPECT_EQ(reinterpret_cast<void*>(NULL), allocator->AllocatePages(1)); | |
35 } | |
36 for (int i = 0; i < 9; i++) { | |
37 allocator->FreePages(allocated[i], 1); | |
38 } | |
39 EXPECT_EQ(reinterpret_cast<void*>(NULL), allocator->AllocatePages(10)); | |
40 for (int i = 0; i < 5; i++) { | |
41 void* result = allocator->AllocatePages(9); | |
42 EXPECT_EQ(start + PAGE_SIZE, reinterpret_cast<uintptr_t>(result)); | |
43 allocator->FreePages(result, 9); | |
44 } | |
45 | |
46 free(arena1); | |
47 } | |
48 | |
49 void TestExternalMap() { | |
50 const int kMapSize = 10; | |
51 | |
52 // Allocate memory for an arena. | |
53 void* arena; | |
54 int rc = posix_memalign(&arena, PAGE_SIZE, kMapSize); | |
55 EXPECT_EQ(0, rc); | |
56 uintptr_t start = reinterpret_cast<uintptr_t>(arena); | |
57 | |
58 uint8_t map[kMapSize + 1]; | |
59 memset(map, 0xaa, kMapSize + 1); | |
60 | |
61 void* page; | |
62 PageAllocator *allocator; | |
63 allocator = new PageAllocator(); | |
64 allocator->AddArena("test", start, PAGE_SIZE * kMapSize, map, kMapSize); | |
65 page = allocator->AllocatePages(1); | |
66 EXPECT_EQ(arena, page); | |
67 // This tests that the implementation actually uses the external | |
68 // map. However the way the implementation uses the external map is | |
69 // *not* part if the API contract, and if the implementation is | |
70 // changed this test should be as well. | |
71 EXPECT_EQ(1, map[0]); | |
72 EXPECT_EQ(0, map[1]); | |
73 allocator->FreePages(page, 1); | |
74 EXPECT_EQ(0, map[0]); | |
75 EXPECT_EQ(0, map[1]); | |
76 | |
77 page = allocator->AllocatePages(kMapSize); | |
78 EXPECT_EQ(arena, page); | |
79 for (int i = 0; i < kMapSize; i++) { | |
80 EXPECT_EQ(1, map[i]); | |
81 } | |
82 EXPECT_EQ(0xaa, map[kMapSize]); | |
83 | |
84 allocator = new PageAllocator(); | |
85 allocator->AddArena("test", start, PAGE_SIZE * kMapSize, map, kMapSize - 1); | |
86 | |
87 memset(map, 0xaa, kMapSize + 1); | |
88 page = allocator->AllocatePages(1); | |
89 EXPECT_EQ(reinterpret_cast<uint8_t*>(arena) + PAGE_SIZE, page); | |
90 for (int i = 0; i < kMapSize + 1; i++) { | |
91 EXPECT_EQ(0xaa, map[i]); | |
92 } | |
93 allocator->FreePages(page, 1); | |
94 page = allocator->AllocatePages(kMapSize); | |
95 EXPECT(page == NULL); | |
96 for (int i = 0; i < kMapSize + 1; i++) { | |
97 EXPECT_EQ(0xaa, map[i]); | |
98 } | |
99 | |
100 free(arena); | |
101 } | |
102 | |
103 int main(int argc, char** argv) { | |
104 Test(); | |
105 TestExternalMap(); | |
106 } | |
OLD | NEW |