OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "mojo/public/bindings/lib/scratch_buffer.h" | |
6 | |
7 #include <assert.h> | |
8 #include <stdlib.h> | |
9 #include <string.h> | |
10 | |
11 #include <algorithm> | |
12 | |
13 #include "mojo/public/bindings/lib/bindings_serialization.h" | |
14 | |
15 // Scrub memory in debug builds to help catch use-after-free bugs. | |
16 #ifdef NDEBUG | |
17 #define DEBUG_SCRUB(address, size) (void) (address), (void) (size) | |
18 #else | |
19 #define DEBUG_SCRUB(address, size) memset(address, 0xCD, size) | |
20 #endif | |
21 | |
22 namespace mojo { | |
23 namespace internal { | |
24 | |
25 ScratchBuffer::ScratchBuffer() | |
26 : overflow_(NULL) { | |
27 fixed_.next = NULL; | |
28 fixed_.cursor = fixed_data_; | |
29 fixed_.end = fixed_data_ + kMinSegmentSize; | |
30 } | |
31 | |
32 ScratchBuffer::~ScratchBuffer() { | |
33 // Invoke destructors in reverse order to mirror allocation order. | |
34 std::deque<PendingDestructor>::reverse_iterator it; | |
35 for (it = pending_dtors_.rbegin(); it != pending_dtors_.rend(); ++it) | |
36 it->func(it->address); | |
37 | |
38 while (overflow_) { | |
39 Segment* doomed = overflow_; | |
40 overflow_ = overflow_->next; | |
41 DEBUG_SCRUB(doomed, doomed->end - reinterpret_cast<char*>(doomed)); | |
42 free(doomed); | |
43 } | |
44 DEBUG_SCRUB(fixed_data_, sizeof(fixed_data_)); | |
45 } | |
46 | |
47 void* ScratchBuffer::Allocate(size_t delta, Destructor func) { | |
48 delta = internal::Align(delta); | |
49 | |
50 void* result = AllocateInSegment(&fixed_, delta); | |
51 if (!result) { | |
52 if (overflow_) | |
53 result = AllocateInSegment(overflow_, delta); | |
54 | |
55 if (!result) { | |
56 AddOverflowSegment(delta); | |
57 result = AllocateInSegment(overflow_, delta); | |
58 } | |
59 } | |
60 | |
61 if (func) { | |
62 PendingDestructor dtor; | |
63 dtor.func = func; | |
64 dtor.address = result; | |
65 pending_dtors_.push_back(dtor); | |
66 } | |
67 return result; | |
68 } | |
69 | |
70 void* ScratchBuffer::AllocateInSegment(Segment* segment, size_t delta) { | |
71 void* result; | |
72 if (static_cast<size_t>(segment->end - segment->cursor) >= delta) { | |
73 result = segment->cursor; | |
74 memset(result, 0, delta); | |
75 segment->cursor += delta; | |
76 } else { | |
77 result = NULL; | |
78 } | |
79 return result; | |
80 } | |
81 | |
82 void ScratchBuffer::AddOverflowSegment(size_t delta) { | |
83 if (delta < kMinSegmentSize) | |
84 delta = kMinSegmentSize; | |
85 | |
86 // Ensure segment buffer is aligned. | |
87 size_t segment_size = internal::Align(sizeof(Segment)) + delta; | |
88 | |
89 Segment* segment = static_cast<Segment*>(malloc(segment_size)); | |
90 segment->next = overflow_; | |
91 segment->cursor = reinterpret_cast<char*>(segment + 1); | |
92 segment->end = segment->cursor + delta; | |
93 | |
94 overflow_ = segment; | |
95 } | |
96 | |
97 } // namespace internal | |
98 } // namespace mojo | |
OLD | NEW |