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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « mojo/public/bindings/lib/buffer.h ('k') | mojo/public/bindings/lib/message.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/public/bindings/lib/buffer.cc
diff --git a/mojo/public/bindings/lib/buffer.cc b/mojo/public/bindings/lib/buffer.cc
new file mode 100644
index 0000000000000000000000000000000000000000..1aa98a9b9d1175a14d9c7b4026a27ba4426332cf
--- /dev/null
+++ b/mojo/public/bindings/lib/buffer.cc
@@ -0,0 +1,104 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "mojo/public/bindings/lib/buffer.h"
+
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <algorithm>
+
+#include "mojo/public/bindings/lib/bindings_serialization.h"
+
+namespace mojo {
+
+//-----------------------------------------------------------------------------
+
+ScratchBuffer::ScratchBuffer()
+ : overflow_(NULL) {
+ fixed_.next = NULL;
+ fixed_.cursor = fixed_data_;
+ fixed_.end = fixed_data_ + kMinSegmentSize;
+}
+
+ScratchBuffer::~ScratchBuffer() {
+ while (overflow_) {
+ Segment* doomed = overflow_;
+ overflow_ = overflow_->next;
+ free(doomed);
+ }
+}
+
+void* ScratchBuffer::Allocate(size_t delta) {
+ delta = internal::Align(delta);
+
+ void* result =
+ AllocateInSegment((overflow_ != NULL) ? overflow_ : &fixed_, delta);
+ if (result)
+ return result;
+
+ AddOverflowSegment(delta);
+ return Allocate(delta);
+}
+
+void* ScratchBuffer::AllocateInSegment(Segment* segment, size_t delta) {
+ void* result;
+ if (static_cast<size_t>(segment->end - segment->cursor) >= delta) {
+ result = segment->cursor;
+ memset(result, 0, delta);
+ segment->cursor += delta;
+ } else {
+ result = NULL;
+ }
+ return result;
+}
+
+void ScratchBuffer::AddOverflowSegment(size_t delta) {
+ if (delta < kMinSegmentSize)
+ delta = kMinSegmentSize;
+ Segment* segment = static_cast<Segment*>(malloc(sizeof(Segment) + delta));
+ segment->next = overflow_;
+ segment->cursor = reinterpret_cast<char*>(segment + 1);
+ segment->end = segment->cursor + delta;
+ overflow_ = segment;
+}
+
+//-----------------------------------------------------------------------------
+
+FixedBuffer::FixedBuffer(size_t size)
+ : ptr_(NULL),
+ cursor_(0),
+ size_(internal::Align(size)) {
+ ptr_ = static_cast<char*>(calloc(size_, 1));
+}
+
+FixedBuffer::~FixedBuffer() {
+ free(ptr_);
+}
+
+void* FixedBuffer::Allocate(size_t delta) {
+ delta = internal::Align(delta);
+
+ // TODO(darin): Using <assert.h> is probably not going to cut it.
+ assert(delta > 0);
+ assert(cursor_ + delta <= size_);
+ if (cursor_ + delta > size_)
+ return NULL;
+
+ char* result = ptr_ + cursor_;
+ cursor_ += delta;
+
+ return result;
+}
+
+void* FixedBuffer::Leak() {
+ char* ptr = ptr_;
+ ptr_ = NULL;
+ cursor_ = 0;
+ size_ = 0;
+ return ptr;
+}
+
+} // namespace mojo
« 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