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

Side by Side Diff: mojo/public/cpp/bindings/tests/buffer_unittest.cc

Issue 265403003: Make sure that ScratchBuffer::Allocate() always return 8-byte aligned address. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix on-stack allocation alignment. Created 6 years, 7 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 <limits> 5 #include <limits>
6 6
7 #include "mojo/public/cpp/bindings/buffer.h" 7 #include "mojo/public/cpp/bindings/buffer.h"
8 #include "mojo/public/cpp/bindings/lib/bindings_serialization.h" 8 #include "mojo/public/cpp/bindings/lib/bindings_serialization.h"
9 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" 9 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h"
10 #include "mojo/public/cpp/bindings/lib/scratch_buffer.h" 10 #include "mojo/public/cpp/bindings/lib/scratch_buffer.h"
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 46
47 // And a request so large it will fail. 47 // And a request so large it will fail.
48 void* fail = buf.Allocate(std::numeric_limits<size_t>::max() - 1024u); 48 void* fail = buf.Allocate(std::numeric_limits<size_t>::max() - 1024u);
49 EXPECT_TRUE(!fail); 49 EXPECT_TRUE(!fail);
50 50
51 // And a request so large it will overflow and fail. 51 // And a request so large it will overflow and fail.
52 void* overflow = buf.Allocate(std::numeric_limits<size_t>::max() - 12u); 52 void* overflow = buf.Allocate(std::numeric_limits<size_t>::max() - 12u);
53 EXPECT_TRUE(!overflow); 53 EXPECT_TRUE(!overflow);
54 } 54 }
55 55
56 TEST(ScratchBufferTest, Alignment) {
57 Environment env;
58
59 internal::ScratchBuffer buf;
60 // Test that small allocations on the stack are aligned properly.
61 void* small = buf.Allocate(1);
62 EXPECT_EQ(0, reinterpret_cast<ptrdiff_t>(small) % 8);
63 small = buf.Allocate(2);
64 EXPECT_EQ(0, reinterpret_cast<ptrdiff_t>(small) % 8);
65
66 // Test that large allocations on the heap are aligned properly.
67 void* large = buf.Allocate(10*1024);
68 EXPECT_EQ(0, reinterpret_cast<ptrdiff_t>(large) % 8);
69 large = buf.Allocate(100*1024);
70 EXPECT_EQ(0, reinterpret_cast<ptrdiff_t>(large) % 8);
71 }
72
56 // Tests that Buffer::current() returns the correct value. 73 // Tests that Buffer::current() returns the correct value.
57 TEST(ScratchBufferTest, Stacked) { 74 TEST(ScratchBufferTest, Stacked) {
58 Environment env; 75 Environment env;
59 76
60 EXPECT_FALSE(Buffer::current()); 77 EXPECT_FALSE(Buffer::current());
61 78
62 { 79 {
63 internal::ScratchBuffer a; 80 internal::ScratchBuffer a;
64 EXPECT_EQ(&a, Buffer::current()); 81 EXPECT_EQ(&a, Buffer::current());
65 82
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 156
140 // A lot too large, leading to possible integer overflow. 157 // A lot too large, leading to possible integer overflow.
141 EXPECT_EQ(reinterpret_cast<void*>(0), 158 EXPECT_EQ(reinterpret_cast<void*>(0),
142 buf.Allocate(std::numeric_limits<size_t>::max() - 8u)); 159 buf.Allocate(std::numeric_limits<size_t>::max() - 8u));
143 } 160 }
144 #endif 161 #endif
145 162
146 } // namespace 163 } // namespace
147 } // namespace test 164 } // namespace test
148 } // namespace mojo 165 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698