Blame xpcom/io/nsStorageStream.h

Packit f0b94e
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
Packit f0b94e
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
Packit f0b94e
/* This Source Code Form is subject to the terms of the Mozilla Public
Packit f0b94e
 * License, v. 2.0. If a copy of the MPL was not distributed with this
Packit f0b94e
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
Packit f0b94e
Packit f0b94e
/*
Packit f0b94e
 * The storage stream provides an internal buffer that can be filled by a
Packit f0b94e
 * client using a single output stream.  One or more independent input streams
Packit f0b94e
 * can be created to read the data out non-destructively.  The implementation
Packit f0b94e
 * uses a segmented buffer internally to avoid realloc'ing of large buffers,
Packit f0b94e
 * with the attendant performance loss and heap fragmentation.
Packit f0b94e
 */
Packit f0b94e
Packit f0b94e
#ifndef _nsStorageStream_h_
Packit f0b94e
#define _nsStorageStream_h_
Packit f0b94e
Packit f0b94e
#include "nsIStorageStream.h"
Packit f0b94e
#include "nsIOutputStream.h"
Packit f0b94e
#include "nsMemory.h"
Packit f0b94e
#include "mozilla/Attributes.h"
Packit f0b94e
Packit f0b94e
#define NS_STORAGESTREAM_CID                         \
Packit f0b94e
  { /* 669a9795-6ff7-4ed4-9150-c34ce2971b63 */       \
Packit f0b94e
    0x669a9795, 0x6ff7, 0x4ed4, {                    \
Packit f0b94e
      0x91, 0x50, 0xc3, 0x4c, 0xe2, 0x97, 0x1b, 0x63 \
Packit f0b94e
    }                                                \
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
#define NS_STORAGESTREAM_CONTRACTID "@mozilla.org/storagestream;1"
Packit f0b94e
Packit f0b94e
class nsSegmentedBuffer;
Packit f0b94e
Packit f0b94e
class nsStorageStream final : public nsIStorageStream, public nsIOutputStream {
Packit f0b94e
 public:
Packit f0b94e
  nsStorageStream();
Packit f0b94e
Packit f0b94e
  NS_DECL_THREADSAFE_ISUPPORTS
Packit f0b94e
  NS_DECL_NSISTORAGESTREAM
Packit f0b94e
  NS_DECL_NSIOUTPUTSTREAM
Packit f0b94e
Packit f0b94e
  friend class nsStorageInputStream;
Packit f0b94e
Packit f0b94e
 private:
Packit f0b94e
  ~nsStorageStream();
Packit f0b94e
Packit f0b94e
  nsSegmentedBuffer* mSegmentedBuffer;
Packit f0b94e
  uint32_t
Packit f0b94e
      mSegmentSize;  // All segments, except possibly the last, are of this size
Packit f0b94e
                     //   Must be power-of-2
Packit f0b94e
  uint32_t mSegmentSizeLog2;  // log2(mSegmentSize)
Packit f0b94e
  bool mWriteInProgress;      // true, if an un-Close'ed output stream exists
Packit f0b94e
  int32_t mLastSegmentNum;    // Last segment # in use, -1 initially
Packit f0b94e
  char* mWriteCursor;         // Pointer to next byte to be written
Packit f0b94e
  char* mSegmentEnd;          // Pointer to one byte after end of segment
Packit f0b94e
                              //   containing the write cursor
Packit f0b94e
  uint32_t mLogicalLength;    // Number of bytes written to stream
Packit f0b94e
Packit f0b94e
  nsresult Seek(int32_t aPosition);
Packit f0b94e
  uint32_t SegNum(uint32_t aPosition) { return aPosition >> mSegmentSizeLog2; }
Packit f0b94e
  uint32_t SegOffset(uint32_t aPosition) {
Packit f0b94e
    return aPosition & (mSegmentSize - 1);
Packit f0b94e
  }
Packit f0b94e
};
Packit f0b94e
Packit f0b94e
#endif  //  _nsStorageStream_h_