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() : ptr_(nullptr), cursor_(0), size_(0) {} | |
18 | |
19 void FixedBuffer::Initialize(void* memory, size_t size) { | |
20 ptr_ = static_cast<char*>(memory); | |
21 cursor_ = 0; | |
22 size_ = size; | |
23 } | |
24 | |
25 void* FixedBuffer::Allocate(size_t delta) { | |
26 // Ensure that all memory returned by Allocate() is 8-byte aligned w.r.t the | |
27 // start of the buffer. | |
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 FixedBufferForTesting::FixedBufferForTesting(size_t size) { | |
42 size_ = internal::Align(size); | |
43 // Use calloc here to ensure all message memory is zero'd out. | |
44 ptr_ = static_cast<char*>(calloc(size_, 1)); | |
45 } | |
46 | |
47 FixedBufferForTesting::~FixedBufferForTesting() { | |
48 free(ptr_); | |
49 } | |
50 | |
51 void* FixedBufferForTesting::Leak() { | |
52 char* ptr = ptr_; | |
53 ptr_ = nullptr; | |
54 cursor_ = 0; | |
55 size_ = 0; | |
56 return ptr; | |
57 } | |
58 | |
59 } // namespace internal | |
60 } // namespace mojo | |
OLD | NEW |