DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (b6d82b1a6b02)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=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/. */

#include "MemoryBlobImpl.h"
#include "mozilla/IntegerPrintfMacros.h"
#include "mozilla/SHA1.h"
#include "nsPrintfCString.h"

namespace mozilla {
namespace dom {

NS_IMPL_ADDREF(MemoryBlobImpl::DataOwnerAdapter)
NS_IMPL_RELEASE(MemoryBlobImpl::DataOwnerAdapter)

NS_INTERFACE_MAP_BEGIN(MemoryBlobImpl::DataOwnerAdapter)
  NS_INTERFACE_MAP_ENTRY(nsIInputStream)
  NS_INTERFACE_MAP_ENTRY(nsISeekableStream)
  NS_INTERFACE_MAP_ENTRY(nsITellableStream)
  NS_INTERFACE_MAP_ENTRY(nsICloneableInputStream)
  NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIIPCSerializableInputStream,
                                     mSerializableInputStream)
  NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIInputStream)
NS_INTERFACE_MAP_END

nsresult MemoryBlobImpl::DataOwnerAdapter::Create(DataOwner* aDataOwner,
                                                  uint32_t aStart,
                                                  uint32_t aLength,
                                                  nsIInputStream** _retval) {
  nsresult rv;
  MOZ_ASSERT(aDataOwner, "Uh ...");

  nsCOMPtr<nsIInputStream> stream;

  rv = NS_NewByteInputStream(
      getter_AddRefs(stream),
      MakeSpan(static_cast<const char*>(aDataOwner->mData) + aStart, aLength),
      NS_ASSIGNMENT_DEPEND);
  NS_ENSURE_SUCCESS(rv, rv);

  NS_ADDREF(*_retval =
                new MemoryBlobImpl::DataOwnerAdapter(aDataOwner, stream));

  return NS_OK;
}

already_AddRefed<BlobImpl> MemoryBlobImpl::CreateSlice(
    uint64_t aStart, uint64_t aLength, const nsAString& aContentType,
    ErrorResult& aRv) {
  RefPtr<BlobImpl> impl =
      new MemoryBlobImpl(this, aStart, aLength, aContentType);
  return impl.forget();
}

void MemoryBlobImpl::CreateInputStream(nsIInputStream** aStream,
                                       ErrorResult& aRv) {
  if (mLength > INT32_MAX) {
    aRv.Throw(NS_ERROR_FAILURE);
    return;
  }

  aRv = MemoryBlobImpl::DataOwnerAdapter::Create(mDataOwner, mStart, mLength,
                                                 aStream);
}

/* static */
StaticMutex MemoryBlobImpl::DataOwner::sDataOwnerMutex;

/* static */ StaticAutoPtr<LinkedList<MemoryBlobImpl::DataOwner>>
    MemoryBlobImpl::DataOwner::sDataOwners;

/* static */
bool MemoryBlobImpl::DataOwner::sMemoryReporterRegistered = false;

MOZ_DEFINE_MALLOC_SIZE_OF(MemoryFileDataOwnerMallocSizeOf)

class MemoryBlobImplDataOwnerMemoryReporter final : public nsIMemoryReporter {
  ~MemoryBlobImplDataOwnerMemoryReporter() {}

 public:
  NS_DECL_THREADSAFE_ISUPPORTS

  NS_IMETHOD CollectReports(nsIHandleReportCallback* aHandleReport,
                            nsISupports* aData, bool aAnonymize) override {
    typedef MemoryBlobImpl::DataOwner DataOwner;

    StaticMutexAutoLock lock(DataOwner::sDataOwnerMutex);

    if (!DataOwner::sDataOwners) {
      return NS_OK;
    }

    const size_t LARGE_OBJECT_MIN_SIZE = 8 * 1024;
    size_t smallObjectsTotal = 0;

    for (DataOwner* owner = DataOwner::sDataOwners->getFirst(); owner;
         owner = owner->getNext()) {
      size_t size = MemoryFileDataOwnerMallocSizeOf(owner->mData);

      if (size < LARGE_OBJECT_MIN_SIZE) {
        smallObjectsTotal += size;
      } else {
        SHA1Sum sha1;
        sha1.update(owner->mData, owner->mLength);
        uint8_t digest[SHA1Sum::kHashSize];  // SHA1 digests are 20 bytes long.
        sha1.finish(digest);

        nsAutoCString digestString;
        for (size_t i = 0; i < sizeof(digest); i++) {
          digestString.AppendPrintf("%02x", digest[i]);
        }

        aHandleReport->Callback(
            /* process */ NS_LITERAL_CSTRING(""),
            nsPrintfCString(
                "explicit/dom/memory-file-data/large/file(length=%" PRIu64
                ", sha1=%s)",
                owner->mLength,
                aAnonymize ? "<anonymized>" : digestString.get()),
            KIND_HEAP, UNITS_BYTES, size,
            nsPrintfCString(
                "Memory used to back a memory file of length %" PRIu64
                " bytes.  The file "
                "has a sha1 of %s.\n\n"
                "Note that the allocator may round up a memory file's length "
                "-- "
                "that is, an N-byte memory file may take up more than N bytes "
                "of "
                "memory.",
                owner->mLength, digestString.get()),
            aData);
      }
    }

    if (smallObjectsTotal > 0) {
      aHandleReport->Callback(
          /* process */ NS_LITERAL_CSTRING(""),
          NS_LITERAL_CSTRING("explicit/dom/memory-file-data/small"), KIND_HEAP,
          UNITS_BYTES, smallObjectsTotal,
          nsPrintfCString(
              "Memory used to back small memory files (i.e. those taking up "
              "less "
              "than %zu bytes of memory each).\n\n"
              "Note that the allocator may round up a memory file's length -- "
              "that is, an N-byte memory file may take up more than N bytes of "
              "memory.",
              LARGE_OBJECT_MIN_SIZE),
          aData);
    }

    return NS_OK;
  }
};

NS_IMPL_ISUPPORTS(MemoryBlobImplDataOwnerMemoryReporter, nsIMemoryReporter)

/* static */
void MemoryBlobImpl::DataOwner::EnsureMemoryReporterRegistered() {
  sDataOwnerMutex.AssertCurrentThreadOwns();
  if (sMemoryReporterRegistered) {
    return;
  }

  RegisterStrongMemoryReporter(new MemoryBlobImplDataOwnerMemoryReporter());

  sMemoryReporterRegistered = true;
}

}  // namespace dom
}  // namespace mozilla