Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / mojo / public / bindings / lib / fixed_buffer.cc
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/bindings/lib/fixed_buffer.h"
6
7 #include <assert.h>
8 #include <stdlib.h>
9 #include <string.h>
10
11 #include <algorithm>
12
13 #include "mojo/public/bindings/lib/bindings_serialization.h"
14
15 namespace mojo {
16 namespace internal {
17
18 FixedBuffer::FixedBuffer(size_t size)
19     : ptr_(NULL),
20       cursor_(0),
21       size_(internal::Align(size)) {
22   ptr_ = static_cast<char*>(calloc(size_, 1));
23 }
24
25 FixedBuffer::~FixedBuffer() {
26   free(ptr_);
27 }
28
29 void* FixedBuffer::Allocate(size_t delta, Destructor dtor) {
30   assert(!dtor);
31
32   delta = internal::Align(delta);
33
34   // TODO(darin): Using <assert.h> is probably not going to cut it.
35   assert(delta > 0);
36   assert(cursor_ + delta <= size_);
37   if (cursor_ + delta > size_)
38     return NULL;
39
40   char* result = ptr_ + cursor_;
41   cursor_ += delta;
42
43   return result;
44 }
45
46 void* FixedBuffer::Leak() {
47   char* ptr = ptr_;
48   ptr_ = NULL;
49   cursor_ = 0;
50   size_ = 0;
51   return ptr;
52 }
53
54 }  // namespace internal
55 }  // namespace mojo