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