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 <stdlib.h> | 7 #include <stdlib.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 | 10 |
11 #include "mojo/public/cpp/bindings/lib/bindings_serialization.h" | 11 #include "mojo/public/cpp/bindings/lib/bindings_serialization.h" |
12 #include "mojo/public/cpp/environment/logging.h" | 12 #include "mojo/public/cpp/environment/logging.h" |
13 | 13 |
14 namespace mojo { | 14 namespace mojo { |
15 namespace internal { | 15 namespace internal { |
16 | 16 |
17 FixedBuffer::FixedBuffer(size_t size) | 17 FixedBuffer::FixedBuffer(size_t size) |
18 : ptr_(NULL), | 18 : ptr_(nullptr), |
19 cursor_(0), | 19 cursor_(0), |
20 size_(internal::Align(size)) { | 20 size_(internal::Align(size)) { |
21 // calloc() required to zero memory and thus avoid info leaks. | 21 // calloc() required to zero memory and thus avoid info leaks. |
22 ptr_ = static_cast<char*>(calloc(size_, 1)); | 22 ptr_ = static_cast<char*>(calloc(size_, 1)); |
23 } | 23 } |
24 | 24 |
25 FixedBuffer::~FixedBuffer() { | 25 FixedBuffer::~FixedBuffer() { |
26 free(ptr_); | 26 free(ptr_); |
27 } | 27 } |
28 | 28 |
29 void* FixedBuffer::Allocate(size_t delta) { | 29 void* FixedBuffer::Allocate(size_t delta) { |
30 delta = internal::Align(delta); | 30 delta = internal::Align(delta); |
31 | 31 |
32 if (delta == 0 || delta > size_ - cursor_) { | 32 if (delta == 0 || delta > size_ - cursor_) { |
33 MOJO_DCHECK(false) << "Not reached"; | 33 MOJO_DCHECK(false) << "Not reached"; |
34 return NULL; | 34 return nullptr; |
35 } | 35 } |
36 | 36 |
37 char* result = ptr_ + cursor_; | 37 char* result = ptr_ + cursor_; |
38 cursor_ += delta; | 38 cursor_ += delta; |
39 | 39 |
40 return result; | 40 return result; |
41 } | 41 } |
42 | 42 |
43 void* FixedBuffer::Leak() { | 43 void* FixedBuffer::Leak() { |
44 char* ptr = ptr_; | 44 char* ptr = ptr_; |
45 ptr_ = NULL; | 45 ptr_ = nullptr; |
46 cursor_ = 0; | 46 cursor_ = 0; |
47 size_ = 0; | 47 size_ = 0; |
48 return ptr; | 48 return ptr; |
49 } | 49 } |
50 | 50 |
51 } // namespace internal | 51 } // namespace internal |
52 } // namespace mojo | 52 } // namespace mojo |
OLD | NEW |