summaryrefslogtreecommitdiffstats
path: root/ppapi/cpp/dev
diff options
context:
space:
mode:
Diffstat (limited to 'ppapi/cpp/dev')
-rw-r--r--ppapi/cpp/dev/var_array_buffer_dev.cc59
-rw-r--r--ppapi/cpp/dev/var_array_buffer_dev.h54
2 files changed, 76 insertions, 37 deletions
diff --git a/ppapi/cpp/dev/var_array_buffer_dev.cc b/ppapi/cpp/dev/var_array_buffer_dev.cc
index 61f697e..47ad9b3 100644
--- a/ppapi/cpp/dev/var_array_buffer_dev.cc
+++ b/ppapi/cpp/dev/var_array_buffer_dev.cc
@@ -1,9 +1,11 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright (c) 2012 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 "ppapi/cpp/dev/var_array_buffer_dev.h"
+#include <limits>
+
#include "ppapi/c/dev/ppb_var_array_buffer_dev.h"
#include "ppapi/cpp/logging.h"
#include "ppapi/cpp/module_impl.h"
@@ -18,50 +20,59 @@ template <> const char* interface_name<PPB_VarArrayBuffer_Dev>() {
} // namespace
-VarArrayBuffer_Dev::VarArrayBuffer_Dev(const Var& var)
- : Var(var), buffer_(NULL) {
- if (var.is_array_buffer()) {
- buffer_ = Map();
- } else {
+VarArrayBuffer_Dev::VarArrayBuffer_Dev(const Var& var) : Var(var) {
+ if (!var.is_array_buffer()) {
PP_NOTREACHED();
var_ = PP_MakeNull();
}
}
-VarArrayBuffer_Dev::VarArrayBuffer_Dev(uint32_t size_in_bytes)
- : buffer_(NULL) {
+VarArrayBuffer_Dev::VarArrayBuffer_Dev(uint32_t size_in_bytes) {
if (has_interface<PPB_VarArrayBuffer_Dev>()) {
var_ = get_interface<PPB_VarArrayBuffer_Dev>()->Create(size_in_bytes);
needs_release_ = true;
- buffer_ = Map();
} else {
PP_NOTREACHED();
var_ = PP_MakeNull();
}
}
-uint32_t VarArrayBuffer_Dev::ByteLength() const {
- if (has_interface<PPB_VarArrayBuffer_Dev>()) {
- return get_interface<PPB_VarArrayBuffer_Dev>()->ByteLength(var_);
+pp::VarArrayBuffer_Dev& VarArrayBuffer_Dev::operator=(
+ const VarArrayBuffer_Dev& other) {
+ Var::operator=(other);
+ return *this;
+}
+
+pp::Var& VarArrayBuffer_Dev::operator=(const Var& other) {
+ if (other.is_array_buffer()) {
+ return Var::operator=(other);
+ } else {
+ PP_NOTREACHED();
+ return *this;
}
- PP_NOTREACHED();
- return 0;
}
-const void* VarArrayBuffer_Dev::Map() const {
- return const_cast<VarArrayBuffer_Dev*>(this)->Map();
+uint32_t VarArrayBuffer_Dev::ByteLength() const {
+ uint32_t byte_length = std::numeric_limits<uint32_t>::max();
+ if (is_array_buffer() && has_interface<PPB_VarArrayBuffer_Dev>())
+ get_interface<PPB_VarArrayBuffer_Dev>()->ByteLength(var_, &byte_length);
+ else
+ PP_NOTREACHED();
+ return byte_length;
}
void* VarArrayBuffer_Dev::Map() {
- // TODO(dmichael): This is not thread-safe.
- if (buffer_)
- return buffer_;
- if (has_interface<PPB_VarArrayBuffer_Dev>()) {
- buffer_ = get_interface<PPB_VarArrayBuffer_Dev>()->Map(var_);
- return buffer_;
- }
+ if (is_array_buffer() && has_interface<PPB_VarArrayBuffer_Dev>())
+ return get_interface<PPB_VarArrayBuffer_Dev>()->Map(var_);
PP_NOTREACHED();
- return 0;
+ return NULL;
+}
+
+void VarArrayBuffer_Dev::Unmap() {
+ if (is_array_buffer() && has_interface<PPB_VarArrayBuffer_Dev>())
+ get_interface<PPB_VarArrayBuffer_Dev>()->Unmap(var_);
+ else
+ PP_NOTREACHED();
}
} // namespace pp
diff --git a/ppapi/cpp/dev/var_array_buffer_dev.h b/ppapi/cpp/dev/var_array_buffer_dev.h
index 6f8c959..6f7c18a 100644
--- a/ppapi/cpp/dev/var_array_buffer_dev.h
+++ b/ppapi/cpp/dev/var_array_buffer_dev.h
@@ -1,4 +1,4 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright (c) 2012 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.
@@ -20,18 +20,37 @@ namespace pp {
class VarArrayBuffer_Dev : public Var {
public:
/// Contruct a VarArrayBuffer_Dev given a var for which is_array_buffer() is
- /// true. This will refer to the same buffer as var, but allows you to access
- /// methods specific to VarArrayBuffer_Dev.
+ /// true. This will refer to the same ArrayBuffer as var, but allows you to
+ /// access methods specific to VarArrayBuffer_Dev.
///
- /// @param[in] var An array buffer Var.
+ /// @param[in] var An ArrayBuffer Var.
explicit VarArrayBuffer_Dev(const Var& var);
+ /// Copy constructor.
VarArrayBuffer_Dev(const VarArrayBuffer_Dev& buffer) : Var(buffer) {}
+ virtual ~VarArrayBuffer_Dev() {}
+
+ /// This function assigns one VarArrayBuffer to another VarArrayBuffer.
+ ///
+ /// @param[in] other The VarArrayBuffer to be assigned.
+ ///
+ /// @return The resulting VarArrayBuffer.
+ VarArrayBuffer_Dev& operator=(const VarArrayBuffer_Dev& other);
+
+ /// This function assigns one VarArrayBuffer to another VarArrayBuffer. Var's
+ /// assignment operator is overloaded here so that we can check for assigning
+ /// a non-ArrayBuffer var to a VarArrayBuffer_Dev.
+ ///
+ /// @param[in] other The VarArrayBuffer to be assigned.
+ ///
+ /// @return The resulting VarArrayBuffer (as a Var&).
+ virtual Var& operator=(const Var& other);
+
/// Construct a new VarArrayBuffer_Dev which is size_in_bytes bytes long and
/// initialized to zero.
///
- /// @param[in] size_in_bytes The size of the constructed array in bytes.
+ /// @param[in] size_in_bytes The size of the constructed ArrayBuffer in bytes.
VarArrayBuffer_Dev(uint32_t size_in_bytes);
/// Return the length of the VarArrayBuffer_Dev in bytes.
@@ -39,17 +58,26 @@ class VarArrayBuffer_Dev : public Var {
/// @return The length of the VarArrayBuffer_Dev in bytes.
uint32_t ByteLength() const;
- /// Return a pointer to the buffer associated with this VarArrayBuffer_Dev.
+ /// Maps the ArrayBuffer in to the module's address space and returns a
+ /// pointer to the internal buffer for this ArrayBuffer.
+ ///
+ /// Note that calling Map() can be a relatively expensive operation. Use care
+ /// when calling it in performance-critical code. For example, you should call
+ /// it only once when looping over an ArrayBuffer:
///
- /// @return A pointer to the buffer associated with this VarArrayBuffer_Dev.
+ /// <code>
+ /// char* data = static_cast<char*>(array_buffer_var.Map());
+ /// uint32_t byte_length = array_buffer_var.ByteLength();
+ /// for (uint32_t i = 0; i < byte_length; ++i)
+ /// data[i] = 'A';
+ /// </code>
+ ///
+ /// @return A pointer to the internal buffer for this ArrayBuffer.
void* Map();
- const void* Map() const;
-
- virtual ~VarArrayBuffer_Dev() {}
- private:
- // We cache the buffer so that repeated calls to Map() are quick.
- void* buffer_;
+ /// Unmaps this ArrayBuffer var from the module address space. Use this if
+ /// you want to save memory but might want to Map the buffer again later.
+ void Unmap();
};
} // namespace pp