OLD | NEW |
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 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 padded_segment_size = internal::Align(sizeof(Segment)); | 83 size_t segment_size = internal::Align(sizeof(Segment)) + delta; |
84 Segment* segment = static_cast<Segment*>( | 84 Segment* segment = static_cast<Segment*>(malloc(segment_size)); |
85 malloc(padded_segment_size + delta)); | |
86 if (segment) { | 85 if (segment) { |
87 segment->next = overflow_; | 86 segment->next = overflow_; |
88 segment->cursor = reinterpret_cast<char*>(segment) + padded_segment_size; | 87 segment->cursor = reinterpret_cast<char*>(segment + 1); |
89 segment->end = segment->cursor + delta; | 88 segment->end = segment->cursor + delta; |
90 overflow_ = segment; | 89 overflow_ = segment; |
91 return true; | 90 return true; |
92 } | 91 } |
93 | 92 |
94 return false; | 93 return false; |
95 } | 94 } |
96 | 95 |
97 } // namespace internal | 96 } // namespace internal |
98 } // namespace mojo | 97 } // namespace mojo |
OLD | NEW |