LCOV - code coverage report
Current view: top level - tools/profiler/core - ProfileJSONWriter.h (source / functions) Hit Total Coverage
Test: output.info Lines: 0 34 0.0 %
Date: 2017-07-14 16:53:18 Functions: 0 16 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
       2             : /* This Source Code Form is subject to the terms of the Mozilla Public
       3             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       4             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
       5             : 
       6             : #ifndef PROFILEJSONWRITER_H
       7             : #define PROFILEJSONWRITER_H
       8             : 
       9             : #include <ostream>
      10             : #include <string>
      11             : #include <string.h>
      12             : 
      13             : #include "mozilla/JSONWriter.h"
      14             : #include "mozilla/UniquePtr.h"
      15             : 
      16             : class SpliceableChunkedJSONWriter;
      17             : 
      18             : // On average, profile JSONs are large enough such that we want to avoid
      19             : // reallocating its buffer when expanding. Additionally, the contents of the
      20             : // profile are not accessed until the profile is entirely written. For these
      21             : // reasons we use a chunked writer that keeps an array of chunks, which is
      22             : // concatenated together after writing is finished.
      23           0 : class ChunkedJSONWriteFunc : public mozilla::JSONWriteFunc
      24             : {
      25             : public:
      26             :   friend class SpliceableJSONWriter;
      27             : 
      28           0 :   ChunkedJSONWriteFunc() {
      29           0 :     AllocChunk(kChunkSize);
      30           0 :   }
      31             : 
      32             :   bool IsEmpty() const {
      33             :     MOZ_ASSERT_IF(!mChunkPtr, !mChunkEnd &&
      34             :                               mChunkList.length() == 0 &&
      35             :                               mChunkLengths.length() == 0);
      36             :     return !mChunkPtr;
      37             :   }
      38             : 
      39             :   void Write(const char* aStr) override;
      40             :   mozilla::UniquePtr<char[]> CopyData() const;
      41             :   void Take(ChunkedJSONWriteFunc&& aOther);
      42             : 
      43             : private:
      44             :   void AllocChunk(size_t aChunkSize);
      45             : 
      46             :   static const size_t kChunkSize = 4096 * 512;
      47             : 
      48             :   // Pointer for writing inside the current chunk.
      49             :   //
      50             :   // The current chunk is always at the back of mChunkList, i.e.,
      51             :   // mChunkList.back() <= mChunkPtr <= mChunkEnd.
      52             :   char* mChunkPtr;
      53             : 
      54             :   // Pointer to the end of the current chunk.
      55             :   //
      56             :   // The current chunk is always at the back of mChunkList, i.e.,
      57             :   // mChunkEnd >= mChunkList.back() + mChunkLengths.back().
      58             :   char* mChunkEnd;
      59             : 
      60             :   // List of chunks and their lengths.
      61             :   //
      62             :   // For all i, the length of the string in mChunkList[i] is
      63             :   // mChunkLengths[i].
      64             :   mozilla::Vector<mozilla::UniquePtr<char[]>> mChunkList;
      65             :   mozilla::Vector<size_t> mChunkLengths;
      66             : };
      67             : 
      68           0 : struct OStreamJSONWriteFunc : public mozilla::JSONWriteFunc
      69             : {
      70           0 :   explicit OStreamJSONWriteFunc(std::ostream& aStream)
      71           0 :     : mStream(aStream)
      72           0 :   { }
      73             : 
      74           0 :   void Write(const char* aStr) override {
      75           0 :     mStream << aStr;
      76           0 :   }
      77             : 
      78             :   std::ostream& mStream;
      79             : };
      80             : 
      81           0 : class SpliceableJSONWriter : public mozilla::JSONWriter
      82             : {
      83             : public:
      84           0 :   explicit SpliceableJSONWriter(mozilla::UniquePtr<mozilla::JSONWriteFunc> aWriter)
      85           0 :     : JSONWriter(mozilla::Move(aWriter))
      86           0 :   { }
      87             : 
      88           0 :   void StartBareList(CollectionStyle aStyle = SingleLineStyle) {
      89           0 :     StartCollection(nullptr, "", aStyle);
      90           0 :   }
      91             : 
      92           0 :   void EndBareList() {
      93           0 :     EndCollection("");
      94           0 :   }
      95             : 
      96           0 :   void NullElements(uint32_t aCount) {
      97           0 :     for (uint32_t i = 0; i < aCount; i++) {
      98           0 :       NullElement();
      99             :     }
     100           0 :   }
     101             : 
     102             :   void Splice(const ChunkedJSONWriteFunc* aFunc);
     103             :   void Splice(const char* aStr);
     104             : 
     105             :   // Splice the given JSON directly in, without quoting.
     106           0 :   void SplicedJSONProperty(const char* aMaybePropertyName,
     107             :                            const char* aJsonValue)
     108             :   {
     109           0 :     Scalar(aMaybePropertyName, aJsonValue);
     110           0 :   }
     111             : 
     112             :   // Takes the chunks from aFunc and write them. If move is not possible
     113             :   // (e.g., using OStreamJSONWriteFunc), aFunc's chunks are copied and its
     114             :   // storage cleared.
     115             :   virtual void TakeAndSplice(ChunkedJSONWriteFunc* aFunc);
     116             : };
     117             : 
     118           0 : class SpliceableChunkedJSONWriter : public SpliceableJSONWriter
     119             : {
     120             : public:
     121           0 :   explicit SpliceableChunkedJSONWriter()
     122           0 :     : SpliceableJSONWriter(mozilla::MakeUnique<ChunkedJSONWriteFunc>())
     123           0 :   { }
     124             : 
     125           0 :   ChunkedJSONWriteFunc* WriteFunc() const {
     126           0 :     return static_cast<ChunkedJSONWriteFunc*>(JSONWriter::WriteFunc());
     127             :   }
     128             : 
     129             :   // Adopts the chunks from aFunc without copying.
     130             :   virtual void TakeAndSplice(ChunkedJSONWriteFunc* aFunc) override;
     131             : };
     132             : 
     133             : #endif // PROFILEJSONWRITER_H

Generated by: LCOV version 1.13