summaryrefslogtreecommitdiff
path: root/media/base/byte_queue.cc
diff options
context:
space:
mode:
authorTorne (Richard Coles) <torne@google.com>2012-11-14 11:43:16 +0000
committerTorne (Richard Coles) <torne@google.com>2012-11-14 11:43:16 +0000
commit5821806d5e7f356e8fa4b058a389a808ea183019 (patch)
treee19f4793aac92e2c0d9a01087019a60d6657d838 /media/base/byte_queue.cc
parent8e79a8efe247f109aafd917a69e8a392961b3687 (diff)
downloadchromium_org-5821806d5e7f356e8fa4b058a389a808ea183019.tar.gz
Merge from Chromium at DEPS revision r167172
This commit was generated by merge_to_master.py. Change-Id: Ib8d56fd5ae39a2d7e8c91dcd76cc6d13f25f2aab
Diffstat (limited to 'media/base/byte_queue.cc')
-rw-r--r--media/base/byte_queue.cc84
1 files changed, 84 insertions, 0 deletions
diff --git a/media/base/byte_queue.cc b/media/base/byte_queue.cc
new file mode 100644
index 0000000000..e91bfb71fc
--- /dev/null
+++ b/media/base/byte_queue.cc
@@ -0,0 +1,84 @@
+// Copyright (c) 2011 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 "media/base/byte_queue.h"
+
+#include "base/logging.h"
+
+namespace media {
+
+// Default starting size for the queue.
+enum { kDefaultQueueSize = 1024 };
+
+ByteQueue::ByteQueue()
+ : buffer_(new uint8[kDefaultQueueSize]),
+ size_(kDefaultQueueSize),
+ offset_(0),
+ used_(0) {
+}
+
+ByteQueue::~ByteQueue() {}
+
+void ByteQueue::Reset() {
+ offset_ = 0;
+ used_ = 0;
+}
+
+void ByteQueue::Push(const uint8* data, int size) {
+ DCHECK(data);
+ DCHECK_GT(size, 0);
+
+ size_t size_needed = used_ + size;
+
+ // Check to see if we need a bigger buffer.
+ if (size_needed > size_) {
+ size_t new_size = 2 * size_;
+ while (size_needed > new_size && new_size > size_)
+ new_size *= 2;
+
+ // Sanity check to make sure we didn't overflow.
+ CHECK_GT(new_size, size_);
+
+ scoped_array<uint8> new_buffer(new uint8[new_size]);
+
+ // Copy the data from the old buffer to the start of the new one.
+ if (used_ > 0)
+ memcpy(new_buffer.get(), front(), used_);
+
+ buffer_.reset(new_buffer.release());
+ size_ = new_size;
+ offset_ = 0;
+ } else if ((offset_ + used_ + size) > size_) {
+ // The buffer is big enough, but we need to move the data in the queue.
+ memmove(buffer_.get(), front(), used_);
+ offset_ = 0;
+ }
+
+ memcpy(front() + used_, data, size);
+ used_ += size;
+}
+
+void ByteQueue::Peek(const uint8** data, int* size) const {
+ DCHECK(data);
+ DCHECK(size);
+ *data = front();
+ *size = used_;
+}
+
+void ByteQueue::Pop(int count) {
+ DCHECK_LE(count, used_);
+
+ offset_ += count;
+ used_ -= count;
+
+ // Move the offset back to 0 if we have reached the end of the buffer.
+ if (offset_ == size_) {
+ DCHECK_EQ(used_, 0);
+ offset_ = 0;
+ }
+}
+
+uint8* ByteQueue::front() const { return buffer_.get() + offset_; }
+
+} // namespace media