From 5f8de423f190bbb79a62f804151bc24824fa32d8 Mon Sep 17 00:00:00 2001 From: "Matt A. Tobin" Date: Fri, 2 Feb 2018 04:16:08 -0500 Subject: Add m-esr52 at 52.6.0 --- media/mtransport/databuffer.h | 74 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 media/mtransport/databuffer.h (limited to 'media/mtransport/databuffer.h') diff --git a/media/mtransport/databuffer.h b/media/mtransport/databuffer.h new file mode 100644 index 000000000..fb60ecb98 --- /dev/null +++ b/media/mtransport/databuffer.h @@ -0,0 +1,74 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +// Original author: ekr@rtfm.com + +#ifndef databuffer_h__ +#define databuffer_h__ +#include +#include +#include +#include + +namespace mozilla { + +class DataBuffer { + public: + DataBuffer() : data_(nullptr), len_(0), capacity_(0) {} + DataBuffer(const uint8_t *data, size_t len) { + Assign(data, len, len); + } + DataBuffer(const uint8_t *data, size_t len, size_t capacity) { + Assign(data, len, capacity); + } + + // to ensure extra space for expansion + void Assign(const uint8_t *data, size_t len, size_t capacity) { + MOZ_RELEASE_ASSERT(len <= capacity); + Allocate(capacity); // sets len_ = capacity + memcpy(static_cast(data_.get()), + static_cast(data), len); + len_ = len; + } + + void Allocate(size_t capacity) { + data_.reset(new uint8_t[capacity ? capacity : 1]); // Don't depend on new [0]. + len_ = capacity_ = capacity; + } + + void EnsureCapacity(size_t capacity) { + if (capacity_ < capacity) { + uint8_t *new_data = new uint8_t[ capacity ? capacity : 1]; + memcpy(static_cast(new_data), + static_cast(data_.get()), len_); + data_.reset(new_data); // after copying! Deletes old data + capacity_ = capacity; + } + } + + // used when something writes to the buffer (having checked + // capacity() or used EnsureCapacity()) and increased the length. + void SetLength(size_t len) { + MOZ_RELEASE_ASSERT(len <= capacity_); + len_ = len; + } + + const uint8_t *data() const { return data_.get(); } + uint8_t *data() { return data_.get(); } + size_t len() const { return len_; } + size_t capacity() const { return capacity_; } + +private: + UniquePtr data_; + size_t len_; + size_t capacity_; + + DISALLOW_COPY_ASSIGN(DataBuffer); +}; + +} + +#endif -- cgit v1.2.3