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