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

Side by Side Diff: mojo/public/cpp/bindings/lib/scratch_buffer.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 "mojo/public/cpp/bindings/lib/scratch_buffer.h" 5 #include "mojo/public/cpp/bindings/lib/scratch_buffer.h"
6 6
7 #include <assert.h> 7 #include <assert.h>
8 #include <stdlib.h> 8 #include <stdlib.h>
9 #include <string.h> 9 #include <string.h>
10 10
11 #include <algorithm> 11 #include <algorithm>
12 12
13 #include "mojo/public/cpp/bindings/lib/bindings_serialization.h" 13 #include "mojo/public/cpp/bindings/lib/bindings_serialization.h"
14 14
15 // Scrub memory in debug builds to help catch use-after-free bugs. 15 // Scrub memory in debug builds to help catch use-after-free bugs.
16 #ifdef NDEBUG 16 #ifdef NDEBUG
17 #define DEBUG_SCRUB(address, size) (void) (address), (void) (size) 17 #define DEBUG_SCRUB(address, size) (void) (address), (void) (size)
18 #else 18 #else
19 #define DEBUG_SCRUB(address, size) memset(address, 0xCD, size) 19 #define DEBUG_SCRUB(address, size) memset(address, 0xCD, size)
20 #endif 20 #endif
21 21
22 namespace mojo { 22 namespace mojo {
23 namespace internal { 23 namespace internal {
24 24
25 ScratchBuffer::ScratchBuffer() 25 ScratchBuffer::ScratchBuffer()
26 : overflow_(NULL) { 26 : overflow_(NULL) {
27 fixed_.next = NULL; 27 fixed_.next = NULL;
28 fixed_.cursor = fixed_data_; 28 fixed_.cursor = internal::AlignPointer(fixed_data_);
darin (slow to review) 2014/05/06 07:32:55 maybe we should align fixed_.end too? doesn't thi
darin (slow to review) 2014/05/06 07:35:29 nevermind. that would increase fixed_.end in some
yzshen1 2014/05/06 08:06:43 On 32-bit systems, it is possible that pointers ar
29 fixed_.end = fixed_data_ + kMinSegmentSize; 29 fixed_.end = fixed_data_ + kMinSegmentSize;
30 } 30 }
31 31
32 ScratchBuffer::~ScratchBuffer() { 32 ScratchBuffer::~ScratchBuffer() {
33 // Invoke destructors in reverse order to mirror allocation order. 33 // Invoke destructors in reverse order to mirror allocation order.
34 std::deque<PendingDestructor>::reverse_iterator it; 34 std::deque<PendingDestructor>::reverse_iterator it;
35 for (it = pending_dtors_.rbegin(); it != pending_dtors_.rend(); ++it) 35 for (it = pending_dtors_.rbegin(); it != pending_dtors_.rend(); ++it)
36 it->func(it->address); 36 it->func(it->address);
37 37
38 while (overflow_) { 38 while (overflow_) {
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 } 73 }
74 74
75 bool ScratchBuffer::AddOverflowSegment(size_t delta) { 75 bool ScratchBuffer::AddOverflowSegment(size_t delta) {
76 if (delta < kMinSegmentSize) 76 if (delta < kMinSegmentSize)
77 delta = kMinSegmentSize; 77 delta = kMinSegmentSize;
78 78
79 if (delta > kMaxSegmentSize) 79 if (delta > kMaxSegmentSize)
80 return false; 80 return false;
81 81
82 // Ensure segment buffer is aligned. 82 // Ensure segment buffer is aligned.
83 size_t segment_size = internal::Align(sizeof(Segment)) + delta; 83 size_t padded_segment_size = internal::Align(sizeof(Segment));
84 Segment* segment = static_cast<Segment*>(malloc(segment_size)); 84 Segment* segment = static_cast<Segment*>(
85 malloc(padded_segment_size + delta));
85 if (segment) { 86 if (segment) {
86 segment->next = overflow_; 87 segment->next = overflow_;
87 segment->cursor = reinterpret_cast<char*>(segment + 1); 88 segment->cursor = reinterpret_cast<char*>(segment) + padded_segment_size;
88 segment->end = segment->cursor + delta; 89 segment->end = segment->cursor + delta;
89 overflow_ = segment; 90 overflow_ = segment;
90 return true; 91 return true;
91 } 92 }
92 93
93 return false; 94 return false;
94 } 95 }
95 96
96 } // namespace internal 97 } // namespace internal
97 } // namespace mojo 98 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/lib/bindings_serialization.cc ('k') | mojo/public/cpp/bindings/tests/buffer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698