OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 28 matching lines...) Expand all Loading... |
39 #include <string.h> | 39 #include <string.h> |
40 | 40 |
41 #if OS(UNIX) | 41 #if OS(UNIX) |
42 #include <sys/mman.h> | 42 #include <sys/mman.h> |
43 | 43 |
44 #ifndef MAP_ANONYMOUS | 44 #ifndef MAP_ANONYMOUS |
45 #define MAP_ANONYMOUS MAP_ANON | 45 #define MAP_ANONYMOUS MAP_ANON |
46 #endif | 46 #endif |
47 #endif // OS(UNIX) | 47 #endif // OS(UNIX) |
48 | 48 |
49 #if defined(NDEBUG) && !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) | 49 #if !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) |
50 | 50 |
51 namespace { | 51 namespace { |
52 | 52 |
53 static PartitionRoot root; | 53 static PartitionRoot root; |
54 | 54 |
55 static const int kTestAllocSize = sizeof(void*); | 55 static const int kTestAllocSize = sizeof(void*); |
56 | 56 |
57 static void RandomNumberSource(unsigned char* buf, size_t len) | 57 static void RandomNumberSource(unsigned char* buf, size_t len) |
58 { | 58 { |
59 memset(buf, '\0', len); | 59 memset(buf, '\0', len); |
60 } | 60 } |
61 | 61 |
62 static void TestSetup() | 62 static void TestSetup() |
63 { | 63 { |
64 WTF::setRandomSource(RandomNumberSource); | 64 WTF::setRandomSource(RandomNumberSource); |
65 partitionAllocInit(&root); | 65 partitionAllocInit(&root); |
66 } | 66 } |
67 | 67 |
68 static void TestShutdown() | 68 static void TestShutdown() |
69 { | 69 { |
70 partitionAllocShutdown(&root); | 70 // We expect no leaks in the general case. We have a test for leak |
| 71 // detection. |
| 72 EXPECT_TRUE(partitionAllocShutdown(&root)); |
71 } | 73 } |
72 | 74 |
73 static WTF::PartitionPageHeader* GetFullPage(size_t size) | 75 static WTF::PartitionPageHeader* GetFullPage(size_t size) |
74 { | 76 { |
75 size_t bucketIdx = size >> WTF::kBucketShift; | 77 size_t bucketIdx = size >> WTF::kBucketShift; |
76 WTF::PartitionBucket* bucket = &root.buckets[bucketIdx]; | 78 WTF::PartitionBucket* bucket = &root.buckets[bucketIdx]; |
77 size_t numSlots = (WTF::kPartitionPageSize - sizeof(WTF::PartitionPageHeader
)) / size; | 79 size_t numSlots = (WTF::kPartitionPageSize - sizeof(WTF::PartitionPageHeader
)) / size; |
78 void* first = 0; | 80 void* first = 0; |
79 void* last = 0; | 81 void* last = 0; |
80 size_t i; | 82 size_t i; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 // Check that the offset appears to include a guard page. | 128 // Check that the offset appears to include a guard page. |
127 EXPECT_EQ(WTF::kPartitionPageSize + sizeof(WTF::PartitionPageHeader), reinte
rpret_cast<size_t>(ptr) & WTF::kSuperPageOffsetMask); | 129 EXPECT_EQ(WTF::kPartitionPageSize + sizeof(WTF::PartitionPageHeader), reinte
rpret_cast<size_t>(ptr) & WTF::kSuperPageOffsetMask); |
128 | 130 |
129 partitionFree(ptr); | 131 partitionFree(ptr); |
130 // Expect that a just-freed page doesn't get tossed to the freelist. | 132 // Expect that a just-freed page doesn't get tossed to the freelist. |
131 EXPECT_EQ(0, bucket->freePages); | 133 EXPECT_EQ(0, bucket->freePages); |
132 | 134 |
133 TestShutdown(); | 135 TestShutdown(); |
134 } | 136 } |
135 | 137 |
| 138 // Check that we can detect a memory leak. |
| 139 TEST(WTF_PartitionAlloc, SimpleLeak) |
| 140 { |
| 141 TestSetup(); |
| 142 void* leakedPtr = partitionAlloc(&root, kTestAllocSize); |
| 143 EXPECT_FALSE(partitionAllocShutdown(&root)); |
| 144 } |
| 145 |
136 // Test multiple allocations, and freelist handling. | 146 // Test multiple allocations, and freelist handling. |
137 TEST(WTF_PartitionAlloc, MultiAlloc) | 147 TEST(WTF_PartitionAlloc, MultiAlloc) |
138 { | 148 { |
139 TestSetup(); | 149 TestSetup(); |
140 | 150 |
141 char* ptr1 = reinterpret_cast<char*>(partitionAlloc(&root, kTestAllocSize)); | 151 char* ptr1 = reinterpret_cast<char*>(partitionAlloc(&root, kTestAllocSize)); |
142 char* ptr2 = reinterpret_cast<char*>(partitionAlloc(&root, kTestAllocSize)); | 152 char* ptr2 = reinterpret_cast<char*>(partitionAlloc(&root, kTestAllocSize)); |
143 EXPECT_TRUE(ptr1); | 153 EXPECT_TRUE(ptr1); |
144 EXPECT_TRUE(ptr2); | 154 EXPECT_TRUE(ptr2); |
145 ptrdiff_t diff = ptr2 - ptr1; | 155 ptrdiff_t diff = ptr2 - ptr1; |
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
375 munmap(map1, WTF::kSystemPageSize); | 385 munmap(map1, WTF::kSystemPageSize); |
376 munmap(map2, WTF::kSystemPageSize); | 386 munmap(map2, WTF::kSystemPageSize); |
377 | 387 |
378 TestShutdown(); | 388 TestShutdown(); |
379 } | 389 } |
380 | 390 |
381 #endif // OS(UNIX) | 391 #endif // OS(UNIX) |
382 | 392 |
383 } // namespace | 393 } // namespace |
384 | 394 |
385 #endif // defined(NDEBUG) && !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) | 395 #endif // !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) |
OLD | NEW |