|
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
|
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 /* |
|
7 * The storage stream provides an internal buffer that can be filled by a |
|
8 * client using a single output stream. One or more independent input streams |
|
9 * can be created to read the data out non-destructively. The implementation |
|
10 * uses a segmented buffer internally to avoid realloc'ing of large buffers, |
|
11 * with the attendant performance loss and heap fragmentation. |
|
12 */ |
|
13 |
|
14 #ifndef _nsStorageStream_h_ |
|
15 #define _nsStorageStream_h_ |
|
16 |
|
17 #include "nsIStorageStream.h" |
|
18 #include "nsIOutputStream.h" |
|
19 #include "nsMemory.h" |
|
20 #include "mozilla/Attributes.h" |
|
21 |
|
22 #define NS_STORAGESTREAM_CID \ |
|
23 { /* 669a9795-6ff7-4ed4-9150-c34ce2971b63 */ \ |
|
24 0x669a9795, \ |
|
25 0x6ff7, \ |
|
26 0x4ed4, \ |
|
27 {0x91, 0x50, 0xc3, 0x4c, 0xe2, 0x97, 0x1b, 0x63} \ |
|
28 } |
|
29 |
|
30 #define NS_STORAGESTREAM_CONTRACTID "@mozilla.org/storagestream;1" |
|
31 |
|
32 class nsSegmentedBuffer; |
|
33 |
|
34 class nsStorageStream MOZ_FINAL : public nsIStorageStream, |
|
35 public nsIOutputStream |
|
36 { |
|
37 public: |
|
38 nsStorageStream(); |
|
39 |
|
40 NS_DECL_THREADSAFE_ISUPPORTS |
|
41 NS_DECL_NSISTORAGESTREAM |
|
42 NS_DECL_NSIOUTPUTSTREAM |
|
43 |
|
44 friend class nsStorageInputStream; |
|
45 |
|
46 private: |
|
47 ~nsStorageStream(); |
|
48 |
|
49 nsSegmentedBuffer* mSegmentedBuffer; |
|
50 uint32_t mSegmentSize; // All segments, except possibly the last, are of this size |
|
51 // Must be power-of-2 |
|
52 uint32_t mSegmentSizeLog2; // log2(mSegmentSize) |
|
53 bool mWriteInProgress; // true, if an un-Close'ed output stream exists |
|
54 int32_t mLastSegmentNum; // Last segment # in use, -1 initially |
|
55 char* mWriteCursor; // Pointer to next byte to be written |
|
56 char* mSegmentEnd; // Pointer to one byte after end of segment |
|
57 // containing the write cursor |
|
58 uint32_t mLogicalLength; // Number of bytes written to stream |
|
59 |
|
60 NS_METHOD Seek(int32_t aPosition); |
|
61 uint32_t SegNum(uint32_t aPosition) {return aPosition >> mSegmentSizeLog2;} |
|
62 uint32_t SegOffset(uint32_t aPosition) {return aPosition & (mSegmentSize - 1);} |
|
63 }; |
|
64 |
|
65 #endif // _nsStorageStream_h_ |