Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(214)

Side by Side Diff: Source/wtf/PartitionAllocTest.cpp

Issue 21666003: Enhancements to PartitionAlloc to support threading and arbitrary allocation sizes. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Review comments. Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « Source/wtf/PartitionAlloc.cpp ('k') | Source/wtf/SpinLock.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 343 matching lines...) Expand 10 before | Expand all | Expand 10 after
354 EXPECT_EQ(0u, secondSuperPageBase & WTF::kSuperPageOffsetMask); 354 EXPECT_EQ(0u, secondSuperPageBase & WTF::kSuperPageOffsetMask);
355 } 355 }
356 } 356 }
357 for (i = 0; i < numPagesNeeded; ++i) { 357 for (i = 0; i < numPagesNeeded; ++i) {
358 FreeFullPage(pages[i], kTestAllocSize); 358 FreeFullPage(pages[i], kTestAllocSize);
359 } 359 }
360 360
361 TestShutdown(); 361 TestShutdown();
362 } 362 }
363 363
364 // Test the generic allocation functions that can handle arbitrary sizes and
365 // reallocing etc.
366 TEST(WTF_PartitionAlloc, GenericAlloc)
367 {
368 TestSetup();
369
370 void* ptr = partitionAllocGeneric(&root, 1);
371 EXPECT_TRUE(ptr);
372 partitionFreeGeneric(ptr, 1);
373 ptr = partitionAllocGeneric(&root, WTF::kMaxAllocation + 1);
374 EXPECT_TRUE(ptr);
375 partitionFreeGeneric(ptr, WTF::kMaxAllocation + 1);
376
377 ptr = partitionAllocGeneric(&root, 1);
378 EXPECT_TRUE(ptr);
379 void* origPtr = ptr;
380 char* charPtr = static_cast<char*>(ptr);
381 *charPtr = 'A';
382
383 // Change the size of the realloc, remaining inside the same bucket.
384 void* newPtr = partitionReallocGeneric(&root, ptr, 1, 2);
385 EXPECT_EQ(ptr, newPtr);
386 newPtr = partitionReallocGeneric(&root, ptr, 2, 1);
387 EXPECT_EQ(ptr, newPtr);
388
389 // Change the size of the realloc, switching buckets.
390 newPtr = partitionReallocGeneric(&root, ptr, 1, WTF::kAllocationGranularity + 1);
391 EXPECT_TRUE(newPtr != ptr);
392 // Check that the realloc copied correctly.
393 char* newCharPtr = static_cast<char*>(newPtr);
394 EXPECT_EQ(*newCharPtr, 'A');
395 *newCharPtr = 'B';
396 // The realloc moved. To check that the old allocation was freed, we can
397 // do an alloc of the old allocation size and check that the old allocation
398 // address is at the head of the freelist and reused.
399 void* reusedPtr = partitionAllocGeneric(&root, 1);
400 EXPECT_EQ(reusedPtr, origPtr);
401 partitionFreeGeneric(reusedPtr, 1);
402
403 // Downsize the realloc.
404 ptr = newPtr;
405 newPtr = partitionReallocGeneric(&root, ptr, WTF::kAllocationGranularity + 1 , 1);
406 EXPECT_EQ(newPtr, origPtr);
407 newCharPtr = static_cast<char*>(newPtr);
408 EXPECT_EQ(*newCharPtr, 'B');
409 *newCharPtr = 'C';
410
411 // Upsize the realloc to outside the partition.
412 ptr = newPtr;
413 newPtr = partitionReallocGeneric(&root, ptr, 1, WTF::kMaxAllocation + 1);
414 EXPECT_TRUE(newPtr != ptr);
415 newCharPtr = static_cast<char*>(newPtr);
416 EXPECT_EQ(*newCharPtr, 'C');
417 *newCharPtr = 'D';
418
419 // Upsize and downsize the realloc, remaining outside the partition.
420 ptr = newPtr;
421 newPtr = partitionReallocGeneric(&root, ptr, WTF::kMaxAllocation + 1, WTF::k MaxAllocation * 10);
422 newCharPtr = static_cast<char*>(newPtr);
423 EXPECT_EQ(*newCharPtr, 'D');
424 *newCharPtr = 'E';
425 ptr = newPtr;
426 newPtr = partitionReallocGeneric(&root, ptr, WTF::kMaxAllocation * 10, WTF:: kMaxAllocation * 2);
427 newCharPtr = static_cast<char*>(newPtr);
428 EXPECT_EQ(*newCharPtr, 'E');
429 *newCharPtr = 'F';
430
431 // Downsize the realloc to inside the partition.
432 ptr = newPtr;
433 newPtr = partitionReallocGeneric(&root, ptr, WTF::kMaxAllocation * 2, 1);
434 EXPECT_TRUE(newPtr != ptr);
435 EXPECT_EQ(newPtr, origPtr);
436 newCharPtr = static_cast<char*>(newPtr);
437 EXPECT_EQ(*newCharPtr, 'F');
438
439 partitionFreeGeneric(newPtr, 1);
440 TestShutdown();
441 }
442
364 #if OS(UNIX) 443 #if OS(UNIX)
365 444
366 // Test correct handling if our mapping collides with another. 445 // Test correct handling if our mapping collides with another.
367 TEST(WTF_PartitionAlloc, MappingCollision) 446 TEST(WTF_PartitionAlloc, MappingCollision)
368 { 447 {
369 TestSetup(); 448 TestSetup();
370 449
371 WTF::PartitionPageHeader* page1 = GetFullPage(kTestAllocSize); 450 WTF::PartitionPageHeader* page1 = GetFullPage(kTestAllocSize);
372 char* pageBase = reinterpret_cast<char*>(page1); 451 char* pageBase = reinterpret_cast<char*>(page1);
373 // Map a single system page either side of the mapping for our allocations, 452 // Map a single system page either side of the mapping for our allocations,
(...skipping 12 matching lines...) Expand all
386 munmap(map2, WTF::kSystemPageSize); 465 munmap(map2, WTF::kSystemPageSize);
387 466
388 TestShutdown(); 467 TestShutdown();
389 } 468 }
390 469
391 #endif // OS(UNIX) 470 #endif // OS(UNIX)
392 471
393 } // namespace 472 } // namespace
394 473
395 #endif // !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) 474 #endif // !defined(MEMORY_TOOL_REPLACES_ALLOCATOR)
OLDNEW
« no previous file with comments | « Source/wtf/PartitionAlloc.cpp ('k') | Source/wtf/SpinLock.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698