Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / mojo / public / cpp / bindings / lib / fixed_buffer.cc
blob6293233765b533ee5ce976f105f6e2c7c2692193
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.
5 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h"
7 #include <stdlib.h>
9 #include <algorithm>
11 #include "mojo/public/cpp/bindings/lib/bindings_serialization.h"
12 #include "mojo/public/cpp/environment/logging.h"
14 namespace mojo {
15 namespace internal {
17 FixedBuffer::FixedBuffer(size_t size)
18 : ptr_(NULL),
19 cursor_(0),
20 size_(internal::Align(size)) {
21 // calloc() required to zero memory and thus avoid info leaks.
22 ptr_ = static_cast<char*>(calloc(size_, 1));
25 FixedBuffer::~FixedBuffer() {
26 free(ptr_);
29 void* FixedBuffer::Allocate(size_t delta) {
30 delta = internal::Align(delta);
32 if (delta == 0 || delta > size_ - cursor_) {
33 MOJO_DCHECK(false) << "Not reached";
34 return NULL;
37 char* result = ptr_ + cursor_;
38 cursor_ += delta;
40 return result;
43 void* FixedBuffer::Leak() {
44 char* ptr = ptr_;
45 ptr_ = NULL;
46 cursor_ = 0;
47 size_ = 0;
48 return ptr;
51 } // namespace internal
52 } // namespace mojo