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

Side by Side Diff: test/cctest/test-spaces.cc

Issue 1404523002: [heap] inline allocation steps refactor (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Add tests and comments Created 5 years, 2 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
« src/heap/spaces.h ('K') | « test/cctest/cctest.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2011 the V8 project authors. All rights reserved. 1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 627 matching lines...) Expand 10 before | Expand all | Expand 10 after
638 638
639 // Turn the allocation into a proper object so isolate teardown won't 639 // Turn the allocation into a proper object so isolate teardown won't
640 // crash. 640 // crash.
641 HeapObject* free_space = NULL; 641 HeapObject* free_space = NULL;
642 CHECK(allocation.To(&free_space)); 642 CHECK(allocation.To(&free_space));
643 new_space->heap()->CreateFillerObjectAt(free_space->address(), 80); 643 new_space->heap()->CreateFillerObjectAt(free_space->address(), 80);
644 } 644 }
645 } 645 }
646 isolate->Dispose(); 646 isolate->Dispose();
647 } 647 }
648
649
650 static HeapObject* AllocateUnaligned(NewSpace* space, int size) {
651 AllocationResult allocation = space->AllocateRawUnaligned(size);
652 CHECK(!allocation.IsRetry());
653 HeapObject* filler = NULL;
654 CHECK(allocation.To(&filler));
655 space->heap()->CreateFillerObjectAt(filler->address(), size);
656 return filler;
657 }
658
659
660 static void observer1(int bytes_allocated, void* arg) {
661 int* counter = static_cast<int*>(arg);
662 (*counter)++;
663 }
664
665
666 static void observer2(int bytes_allocated, void* arg) {
667 int* counter = static_cast<int*>(arg);
668 (*counter)++;
669 }
670
671
672 UNINITIALIZED_TEST(InlineAllocationObserver) {
673 v8::Isolate::CreateParams create_params;
674 create_params.array_buffer_allocator = CcTest::array_buffer_allocator();
675 v8::Isolate* isolate = v8::Isolate::New(create_params);
676 {
677 v8::Isolate::Scope isolate_scope(isolate);
678 v8::HandleScope handle_scope(isolate);
679 v8::Context::New(isolate)->Enter();
680
681 Isolate* i_isolate = reinterpret_cast<Isolate*>(isolate);
682
683 NewSpace* new_space = i_isolate->heap()->new_space();
684
685 int counter1 = 0;
686 new_space->AddInlineAllocationObserver(128, observer1, &counter1);
687
688 // The observer should not get notified until we have allocated a 128 bytes.
689 AllocateUnaligned(new_space, 64);
690 CHECK_EQ(counter1, 0);
691
692 // The observer should get notified when have allocated precisely 128 bytes.
693 AllocateUnaligned(new_space, 64);
694 CHECK_EQ(counter1, 1);
695
696 // Another 128 bytes should get another notification.
697 AllocateUnaligned(new_space, 128);
698 CHECK_EQ(counter1, 2);
699
700 // Allocating a large object should get only one notification.
701 AllocateUnaligned(new_space, 1024);
702 CHECK_EQ(counter1, 3);
703
704 // Allocating another 2048 bytes in small objects should get 16
705 // notifications.
706 for (int i = 0; i < 64; ++i) {
707 AllocateUnaligned(new_space, 32);
708 }
709 CHECK_EQ(counter1, 19);
710
711 // Multiple observers should work.
712 int counter2 = 0;
713 new_space->AddInlineAllocationObserver(96, observer2, &counter2);
714
715 AllocateUnaligned(new_space, 2048);
716 CHECK_EQ(counter1, 20);
717 CHECK_EQ(counter2, 1);
718
719 AllocateUnaligned(new_space, 96);
720 CHECK_EQ(counter1, 20);
721 CHECK_EQ(counter2, 2);
722
723 // Callback should stop getting called after an observer is removed.
724 new_space->RemoveInlineAllocationObserver(observer1);
725
726 AllocateUnaligned(new_space, 384);
727 CHECK_EQ(counter1, 20); // no more notifications.
728 CHECK_EQ(counter2, 3); // this one is still active.
729
730 // The same function pointer can be added multiple times.
731 int counter3 = 0;
732 new_space->AddInlineAllocationObserver(32, observer2, &counter3);
733
734 // Both counter2, and counter3 should be getting updated now.
735 AllocateUnaligned(new_space, 128);
736 CHECK_EQ(counter1, 20);
737 CHECK_EQ(counter2, 4);
738 CHECK_EQ(counter3, 1);
739
740 // Removing an duplicated observer removes it in the order of addition.
741 new_space->RemoveInlineAllocationObserver(observer2);
742 AllocateUnaligned(new_space, 128);
743 CHECK_EQ(counter1, 20);
744 CHECK_EQ(counter2, 4);
745 CHECK_EQ(counter3, 2); // this one should still get updated.
746
747 // Should be able to remove the second instance of observer2.
748 new_space->RemoveInlineAllocationObserver(observer2);
749 CHECK_EQ(counter1, 20);
750 CHECK_EQ(counter2, 4);
751 CHECK_EQ(counter3, 2);
752 }
753 isolate->Dispose();
754 }
OLDNEW
« src/heap/spaces.h ('K') | « test/cctest/cctest.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698