OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/bindings/lib/buffer.h" | 5 #include "mojo/public/bindings/lib/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 16 matching lines...) Expand all Loading... |
27 while (overflow_) { | 27 while (overflow_) { |
28 Segment* doomed = overflow_; | 28 Segment* doomed = overflow_; |
29 overflow_ = overflow_->next; | 29 overflow_ = overflow_->next; |
30 free(doomed); | 30 free(doomed); |
31 } | 31 } |
32 } | 32 } |
33 | 33 |
34 void* ScratchBuffer::Allocate(size_t delta) { | 34 void* ScratchBuffer::Allocate(size_t delta) { |
35 delta = internal::Align(delta); | 35 delta = internal::Align(delta); |
36 | 36 |
37 void* result = | 37 void* result = AllocateInSegment(&fixed_, delta); |
38 AllocateInSegment((overflow_ != NULL) ? overflow_ : &fixed_, delta); | |
39 if (result) | 38 if (result) |
40 return result; | 39 return result; |
41 | 40 |
| 41 if (overflow_) { |
| 42 result = AllocateInSegment(overflow_, delta); |
| 43 if (result) |
| 44 return result; |
| 45 } |
| 46 |
42 AddOverflowSegment(delta); | 47 AddOverflowSegment(delta); |
43 return Allocate(delta); | 48 return AllocateInSegment(overflow_, delta); |
44 } | 49 } |
45 | 50 |
46 void* ScratchBuffer::AllocateInSegment(Segment* segment, size_t delta) { | 51 void* ScratchBuffer::AllocateInSegment(Segment* segment, size_t delta) { |
47 void* result; | 52 void* result; |
48 if (static_cast<size_t>(segment->end - segment->cursor) >= delta) { | 53 if (static_cast<size_t>(segment->end - segment->cursor) >= delta) { |
49 result = segment->cursor; | 54 result = segment->cursor; |
50 memset(result, 0, delta); | 55 memset(result, 0, delta); |
51 segment->cursor += delta; | 56 segment->cursor += delta; |
52 } else { | 57 } else { |
53 result = NULL; | 58 result = NULL; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 | 105 |
101 void* FixedBuffer::Leak() { | 106 void* FixedBuffer::Leak() { |
102 char* ptr = ptr_; | 107 char* ptr = ptr_; |
103 ptr_ = NULL; | 108 ptr_ = NULL; |
104 cursor_ = 0; | 109 cursor_ = 0; |
105 size_ = 0; | 110 size_ = 0; |
106 return ptr; | 111 return ptr; |
107 } | 112 } |
108 | 113 |
109 } // namespace mojo | 114 } // namespace mojo |
OLD | NEW |