OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <stdio.h> | 5 #include <stdio.h> |
6 #include <stdlib.h> | 6 #include <stdlib.h> |
7 #include <algorithm> // for min() | 7 #include <algorithm> // for min() |
8 #include "base/atomicops.h" | 8 #include "base/atomicops.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
(...skipping 461 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
472 unsigned char* dst = | 472 unsigned char* dst = |
473 reinterpret_cast<unsigned char*>(_recalloc(src, 1, dst_size)); | 473 reinterpret_cast<unsigned char*>(_recalloc(src, 1, dst_size)); |
474 EXPECT_TRUE(Valid(dst, min(src_size, dst_size))); | 474 EXPECT_TRUE(Valid(dst, min(src_size, dst_size))); |
475 Fill(dst, dst_size); | 475 Fill(dst, dst_size); |
476 EXPECT_TRUE(Valid(dst, dst_size)); | 476 EXPECT_TRUE(Valid(dst, dst_size)); |
477 if (dst != NULL) | 477 if (dst != NULL) |
478 free(dst); | 478 free(dst); |
479 } | 479 } |
480 } | 480 } |
481 } | 481 } |
| 482 |
| 483 // Test windows specific _aligned_malloc() and _aligned_free() methods. |
| 484 TEST(Allocators, AlignedMalloc) { |
| 485 // Try allocating data with a bunch of alignments and sizes |
| 486 static const int kTestAlignments[] = {8, 16, 256, 4096, 8192, 16384}; |
| 487 for (int size = 1; size > 0; size = NextSize(size)) { |
| 488 for (int i = 0; i < ARRAYSIZE(kTestAlignments); ++i) { |
| 489 unsigned char* ptr = static_cast<unsigned char*>( |
| 490 _aligned_malloc(size, kTestAlignments[i])); |
| 491 CheckAlignment(ptr, kTestAlignments[i]); |
| 492 Fill(ptr, size); |
| 493 EXPECT_TRUE(Valid(ptr, size)); |
| 494 |
| 495 // Make a second allocation of the same size and alignment to prevent |
| 496 // allocators from passing this test by accident. Per jar, tcmalloc |
| 497 // provides allocations for new (never before seen) sizes out of a thread |
| 498 // local heap of a given "size class." Each time the test requests a new |
| 499 // size, it will usually get the first element of a span, which is a |
| 500 // 4K aligned allocation. |
| 501 unsigned char* ptr2 = static_cast<unsigned char*>( |
| 502 _aligned_malloc(size, kTestAlignments[i])); |
| 503 CheckAlignment(ptr2, kTestAlignments[i]); |
| 504 Fill(ptr2, size); |
| 505 EXPECT_TRUE(Valid(ptr2, size)); |
| 506 |
| 507 // Should never happen, but sanity check just in case. |
| 508 ASSERT_NE(ptr, ptr2); |
| 509 _aligned_free(ptr); |
| 510 _aligned_free(ptr2); |
| 511 } |
| 512 } |
| 513 } |
| 514 |
482 #endif | 515 #endif |
483 | 516 |
484 | 517 |
485 int main(int argc, char** argv) { | 518 int main(int argc, char** argv) { |
486 testing::InitGoogleTest(&argc, argv); | 519 testing::InitGoogleTest(&argc, argv); |
487 return RUN_ALL_TESTS(); | 520 return RUN_ALL_TESTS(); |
488 } | 521 } |
OLD | NEW |