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/fixed_buffer.h" | 5 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" |
6 | 6 |
7 #include <stddef.h> | |
8 #include <stdlib.h> | 7 #include <stdlib.h> |
9 | 8 |
10 #include <algorithm> | |
11 | |
12 #include "base/logging.h" | |
13 #include "mojo/public/cpp/bindings/lib/serialization_util.h" | |
14 | |
15 namespace mojo { | 9 namespace mojo { |
16 namespace internal { | 10 namespace internal { |
17 | 11 |
18 FixedBuffer::FixedBuffer() : ptr_(nullptr), cursor_(0), size_(0) {} | |
19 | |
20 void FixedBuffer::Initialize(void* memory, size_t size) { | |
21 DCHECK(size == internal::Align(size)); | |
22 | |
23 ptr_ = static_cast<char*>(memory); | |
24 cursor_ = 0; | |
25 size_ = size; | |
26 } | |
27 | |
28 void* FixedBuffer::Allocate(size_t delta) { | |
29 delta = internal::Align(delta); | |
30 | |
31 if (delta == 0 || delta > size_ - cursor_) { | |
32 NOTREACHED(); | |
33 return nullptr; | |
34 } | |
35 | |
36 char* result = ptr_ + cursor_; | |
37 cursor_ += delta; | |
38 | |
39 return result; | |
40 } | |
41 | |
42 FixedBufferForTesting::FixedBufferForTesting(size_t size) { | 12 FixedBufferForTesting::FixedBufferForTesting(size_t size) { |
43 size_ = internal::Align(size); | 13 size = internal::Align(size); |
44 // Use calloc here to ensure all message memory is zero'd out. | 14 // Use calloc here to ensure all message memory is zero'd out. |
45 ptr_ = static_cast<char*>(calloc(size_, 1)); | 15 void* ptr = calloc(size, 1); |
| 16 Initialize(ptr, size); |
46 } | 17 } |
47 | 18 |
48 FixedBufferForTesting::~FixedBufferForTesting() { | 19 FixedBufferForTesting::~FixedBufferForTesting() { |
49 free(ptr_); | 20 free(data()); |
50 } | 21 } |
51 | 22 |
52 void* FixedBufferForTesting::Leak() { | 23 void* FixedBufferForTesting::Leak() { |
53 char* ptr = ptr_; | 24 void* ptr = data(); |
54 ptr_ = nullptr; | 25 Initialize(nullptr, 0); |
55 cursor_ = 0; | |
56 size_ = 0; | |
57 return ptr; | 26 return ptr; |
58 } | 27 } |
59 | 28 |
60 } // namespace internal | 29 } // namespace internal |
61 } // namespace mojo | 30 } // namespace mojo |
OLD | NEW |