Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(69)

Side by Side Diff: mojo/public/bindings/lib/buffer.cc

Issue 23913008: C++ bindings (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix windows build error. Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « mojo/public/bindings/lib/buffer.h ('k') | mojo/public/bindings/lib/message.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2013 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/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
17 //-----------------------------------------------------------------------------
18
19 ScratchBuffer::ScratchBuffer()
20 : overflow_(NULL) {
21 fixed_.next = NULL;
22 fixed_.cursor = fixed_data_;
23 fixed_.end = fixed_data_ + kMinSegmentSize;
24 }
25
26 ScratchBuffer::~ScratchBuffer() {
27 while (overflow_) {
28 Segment* doomed = overflow_;
29 overflow_ = overflow_->next;
30 free(doomed);
31 }
32 }
33
34 void* ScratchBuffer::Allocate(size_t delta) {
35 delta = internal::Align(delta);
36
37 void* result =
38 AllocateInSegment((overflow_ != NULL) ? overflow_ : &fixed_, delta);
39 if (result)
40 return result;
41
42 AddOverflowSegment(delta);
43 return Allocate(delta);
44 }
45
46 void* ScratchBuffer::AllocateInSegment(Segment* segment, size_t delta) {
47 void* result;
48 if (static_cast<size_t>(segment->end - segment->cursor) >= delta) {
49 result = segment->cursor;
50 memset(result, 0, delta);
51 segment->cursor += delta;
52 } else {
53 result = NULL;
54 }
55 return result;
56 }
57
58 void ScratchBuffer::AddOverflowSegment(size_t delta) {
59 if (delta < kMinSegmentSize)
60 delta = kMinSegmentSize;
61 Segment* segment = static_cast<Segment*>(malloc(sizeof(Segment) + delta));
62 segment->next = overflow_;
63 segment->cursor = reinterpret_cast<char*>(segment + 1);
64 segment->end = segment->cursor + delta;
65 overflow_ = segment;
66 }
67
68 //-----------------------------------------------------------------------------
69
70 FixedBuffer::FixedBuffer(size_t size)
71 : ptr_(NULL),
72 cursor_(0),
73 size_(internal::Align(size)) {
74 ptr_ = static_cast<char*>(calloc(size_, 1));
75 }
76
77 FixedBuffer::~FixedBuffer() {
78 free(ptr_);
79 }
80
81 void* FixedBuffer::Allocate(size_t delta) {
82 delta = internal::Align(delta);
83
84 // TODO(darin): Using <assert.h> is probably not going to cut it.
85 assert(delta > 0);
86 assert(cursor_ + delta <= size_);
87 if (cursor_ + delta > size_)
88 return NULL;
89
90 char* result = ptr_ + cursor_;
91 cursor_ += delta;
92
93 return result;
94 }
95
96 void* FixedBuffer::Leak() {
97 char* ptr = ptr_;
98 ptr_ = NULL;
99 cursor_ = 0;
100 size_ = 0;
101 return ptr;
102 }
103
104 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/public/bindings/lib/buffer.h ('k') | mojo/public/bindings/lib/message.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698