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 "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #if defined(OS_MACOSX) | 6 #if defined(OS_MACOSX) |
7 #include "base/mac/scoped_nsautorelease_pool.h" | 7 #include "base/mac/scoped_nsautorelease_pool.h" |
8 #endif | 8 #endif |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/shared_memory.h" | 10 #include "base/shared_memory.h" |
| 11 #include "base/sys_info.h" |
11 #include "base/test/multiprocess_test.h" | 12 #include "base/test/multiprocess_test.h" |
12 #include "base/threading/platform_thread.h" | 13 #include "base/threading/platform_thread.h" |
13 #include "base/time.h" | 14 #include "base/time.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
15 #include "testing/multiprocess_func_list.h" | 16 #include "testing/multiprocess_func_list.h" |
16 | 17 |
17 #if defined(OS_MACOSX) | 18 #if defined(OS_MACOSX) |
18 #include "base/mac/scoped_nsautorelease_pool.h" | 19 #include "base/mac/scoped_nsautorelease_pool.h" |
19 #endif | 20 #endif |
20 | 21 |
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 else | 337 else |
337 EXPECT_EQ(0, pointers[j][0]); | 338 EXPECT_EQ(0, pointers[j][0]); |
338 } | 339 } |
339 } | 340 } |
340 | 341 |
341 for (int i = 0; i < count; i++) { | 342 for (int i = 0; i < count; i++) { |
342 memories[i].Close(); | 343 memories[i].Close(); |
343 } | 344 } |
344 } | 345 } |
345 | 346 |
| 347 TEST(SharedMemoryTest, MapAt) { |
| 348 ASSERT_TRUE(SysInfo::VMAllocationGranularity() >= sizeof(uint32)); |
| 349 const size_t kCount = SysInfo::VMAllocationGranularity(); |
| 350 const size_t kDataSize = kCount * sizeof(uint32); |
| 351 |
| 352 SharedMemory memory; |
| 353 ASSERT_TRUE(memory.CreateAndMapAnonymous(kDataSize)); |
| 354 ASSERT_TRUE(memory.Map(kDataSize)); |
| 355 uint32* ptr = static_cast<uint32*>(memory.memory()); |
| 356 ASSERT_NE(ptr, static_cast<void*>(NULL)); |
| 357 |
| 358 for (size_t i = 0; i < kCount; ++i) { |
| 359 ptr[i] = i; |
| 360 } |
| 361 |
| 362 memory.Unmap(); |
| 363 |
| 364 off_t offset = SysInfo::VMAllocationGranularity(); |
| 365 ASSERT_TRUE(memory.MapAt(offset, kDataSize - offset)); |
| 366 offset /= sizeof(uint32); |
| 367 ptr = static_cast<uint32*>(memory.memory()); |
| 368 ASSERT_NE(ptr, static_cast<void*>(NULL)); |
| 369 for (size_t i = offset; i < kCount; ++i) { |
| 370 EXPECT_EQ(ptr[i - offset], i); |
| 371 } |
| 372 } |
| 373 |
346 #if defined(OS_POSIX) | 374 #if defined(OS_POSIX) |
347 // Create a shared memory object, mmap it, and mprotect it to PROT_EXEC. | 375 // Create a shared memory object, mmap it, and mprotect it to PROT_EXEC. |
348 TEST(SharedMemoryTest, AnonymousExecutable) { | 376 TEST(SharedMemoryTest, AnonymousExecutable) { |
349 const uint32 kTestSize = 1 << 16; | 377 const uint32 kTestSize = 1 << 16; |
350 | 378 |
351 SharedMemory shared_memory; | 379 SharedMemory shared_memory; |
352 SharedMemoryCreateOptions options; | 380 SharedMemoryCreateOptions options; |
353 options.size = kTestSize; | 381 options.size = kTestSize; |
354 options.executable = true; | 382 options.executable = true; |
355 | 383 |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
441 SharedMemoryProcessTest::CleanUp(); | 469 SharedMemoryProcessTest::CleanUp(); |
442 } | 470 } |
443 | 471 |
444 MULTIPROCESS_TEST_MAIN(SharedMemoryTestMain) { | 472 MULTIPROCESS_TEST_MAIN(SharedMemoryTestMain) { |
445 return SharedMemoryProcessTest::TaskTestMain(); | 473 return SharedMemoryProcessTest::TaskTestMain(); |
446 } | 474 } |
447 | 475 |
448 #endif // !OS_IOS | 476 #endif // !OS_IOS |
449 | 477 |
450 } // namespace base | 478 } // namespace base |
OLD | NEW |