devtools/shared/heapsnapshot/ZeroCopyNSIOutputStream.cpp
author Nicholas Nethercote <nnethercote@mozilla.com>
Fri, 02 Sep 2016 17:12:24 +1000
changeset 356450 177f41cecedb422d7a11156091ee718ec919c643
parent 309625 f88dfb04a7548936a7f369304bed2df7df3d5adf
child 507432 fb09acfff1684500e989acc798e97fa7f9358dc0
permissions -rw-r--r--
Bug 1299384 - Use MOZ_MUST_USE with NS_warn_if_impl(). r=erahm. This change avoids lots of false positives for Coverity's CHECKED_RETURN warning, caused by NS_WARN_IF's current use in both statement-style and expression-style. In the case where the code within the NS_WARN_IF has side-effects, I made the following change. > NS_WARN_IF(NS_FAILED(FunctionWithSideEffects())); > --> > Unused << NS_WARN_IF(NS_FAILED(FunctionWithSideEffects())); In the case where the code within the NS_WARN_IF lacks side-effects, I made the following change. > NS_WARN_IF(!condWithoutSideEffects); > --> > NS_WARNING_ASSERTION(condWithoutSideEffects, "msg"); This has two improvements. - The condition is not evaluated in non-debug builds. - The sense of the condition is inverted to the familiar "this condition should be true" sense used in assertions. A common variation on the side-effect-free case is the following. > nsresult rv = Fn(); > NS_WARN_IF_(NS_FAILED(rv)); > --> > DebugOnly<nsresult rv> = Fn(); > NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Fn failed");

/* -*-  Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
/* 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/. */

#include "mozilla/devtools/ZeroCopyNSIOutputStream.h"

#include "mozilla/DebugOnly.h"
#include "mozilla/Unused.h"

namespace mozilla {
namespace devtools {

ZeroCopyNSIOutputStream::ZeroCopyNSIOutputStream(nsCOMPtr<nsIOutputStream>& out)
  : out(out)
  , result_(NS_OK)
  , amountUsed(0)
  , writtenCount(0)
{
  DebugOnly<bool> nonBlocking = false;
  MOZ_ASSERT(out->IsNonBlocking(&nonBlocking) == NS_OK);
  MOZ_ASSERT(!nonBlocking);
}

ZeroCopyNSIOutputStream::~ZeroCopyNSIOutputStream()
{
  if (!failed())
    Unused << NS_WARN_IF(NS_FAILED(writeBuffer()));
}

nsresult
ZeroCopyNSIOutputStream::writeBuffer()
{
  if (failed())
    return result_;

  if (amountUsed == 0)
    return NS_OK;

  int32_t amountWritten = 0;
  while (amountWritten < amountUsed) {
    uint32_t justWritten = 0;

    result_ = out->Write(buffer + amountWritten,
                         amountUsed - amountWritten,
                         &justWritten);
    if (NS_WARN_IF(NS_FAILED(result_)))
      return result_;

    amountWritten += justWritten;
  }

  writtenCount += amountUsed;
  amountUsed = 0;
  return NS_OK;
}

// ZeroCopyOutputStream Interface

bool
ZeroCopyNSIOutputStream::Next(void** data, int* size)
{
  MOZ_ASSERT(data != nullptr);
  MOZ_ASSERT(size != nullptr);

  if (failed())
    return false;

  if (amountUsed == BUFFER_SIZE) {
    if (NS_FAILED(writeBuffer()))
      return false;
  }

  *data = buffer + amountUsed;
  *size = BUFFER_SIZE - amountUsed;
  amountUsed = BUFFER_SIZE;
  return true;
}

void
ZeroCopyNSIOutputStream::BackUp(int count)
{
  MOZ_ASSERT(count >= 0,
             "Cannot back up a negative amount of bytes.");
  MOZ_ASSERT(amountUsed == BUFFER_SIZE,
             "Can only call BackUp directly after calling Next.");
  MOZ_ASSERT(count <= amountUsed,
             "Can't back up further than we've given out.");

  amountUsed -= count;
}

::google::protobuf::int64
ZeroCopyNSIOutputStream::ByteCount() const
{
  return writtenCount + amountUsed;
}

} // namespace devtools
} // namespace mozilla