Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | // CoderMixer2.h |
michael@0 | 2 | |
michael@0 | 3 | #ifndef __CODER_MIXER2_H |
michael@0 | 4 | #define __CODER_MIXER2_H |
michael@0 | 5 | |
michael@0 | 6 | #include "../../../Common/Vector.h" |
michael@0 | 7 | #include "../../../Common/Types.h" |
michael@0 | 8 | #include "../../../Common/MyCom.h" |
michael@0 | 9 | #include "../../ICoder.h" |
michael@0 | 10 | |
michael@0 | 11 | namespace NCoderMixer2 { |
michael@0 | 12 | |
michael@0 | 13 | struct CBindPair |
michael@0 | 14 | { |
michael@0 | 15 | UInt32 InIndex; |
michael@0 | 16 | UInt32 OutIndex; |
michael@0 | 17 | }; |
michael@0 | 18 | |
michael@0 | 19 | struct CCoderStreamsInfo |
michael@0 | 20 | { |
michael@0 | 21 | UInt32 NumInStreams; |
michael@0 | 22 | UInt32 NumOutStreams; |
michael@0 | 23 | }; |
michael@0 | 24 | |
michael@0 | 25 | struct CBindInfo |
michael@0 | 26 | { |
michael@0 | 27 | CRecordVector<CCoderStreamsInfo> Coders; |
michael@0 | 28 | CRecordVector<CBindPair> BindPairs; |
michael@0 | 29 | CRecordVector<UInt32> InStreams; |
michael@0 | 30 | CRecordVector<UInt32> OutStreams; |
michael@0 | 31 | |
michael@0 | 32 | void Clear() |
michael@0 | 33 | { |
michael@0 | 34 | Coders.Clear(); |
michael@0 | 35 | BindPairs.Clear(); |
michael@0 | 36 | InStreams.Clear(); |
michael@0 | 37 | OutStreams.Clear(); |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | /* |
michael@0 | 41 | UInt32 GetCoderStartOutStream(UInt32 coderIndex) const |
michael@0 | 42 | { |
michael@0 | 43 | UInt32 numOutStreams = 0; |
michael@0 | 44 | for (UInt32 i = 0; i < coderIndex; i++) |
michael@0 | 45 | numOutStreams += Coders[i].NumOutStreams; |
michael@0 | 46 | return numOutStreams; |
michael@0 | 47 | } |
michael@0 | 48 | */ |
michael@0 | 49 | |
michael@0 | 50 | |
michael@0 | 51 | void GetNumStreams(UInt32 &numInStreams, UInt32 &numOutStreams) const |
michael@0 | 52 | { |
michael@0 | 53 | numInStreams = 0; |
michael@0 | 54 | numOutStreams = 0; |
michael@0 | 55 | for (int i = 0; i < Coders.Size(); i++) |
michael@0 | 56 | { |
michael@0 | 57 | const CCoderStreamsInfo &coderStreamsInfo = Coders[i]; |
michael@0 | 58 | numInStreams += coderStreamsInfo.NumInStreams; |
michael@0 | 59 | numOutStreams += coderStreamsInfo.NumOutStreams; |
michael@0 | 60 | } |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | int FindBinderForInStream(UInt32 inStream) const |
michael@0 | 64 | { |
michael@0 | 65 | for (int i = 0; i < BindPairs.Size(); i++) |
michael@0 | 66 | if (BindPairs[i].InIndex == inStream) |
michael@0 | 67 | return i; |
michael@0 | 68 | return -1; |
michael@0 | 69 | } |
michael@0 | 70 | int FindBinderForOutStream(UInt32 outStream) const |
michael@0 | 71 | { |
michael@0 | 72 | for (int i = 0; i < BindPairs.Size(); i++) |
michael@0 | 73 | if (BindPairs[i].OutIndex == outStream) |
michael@0 | 74 | return i; |
michael@0 | 75 | return -1; |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | UInt32 GetCoderInStreamIndex(UInt32 coderIndex) const |
michael@0 | 79 | { |
michael@0 | 80 | UInt32 streamIndex = 0; |
michael@0 | 81 | for (UInt32 i = 0; i < coderIndex; i++) |
michael@0 | 82 | streamIndex += Coders[i].NumInStreams; |
michael@0 | 83 | return streamIndex; |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | UInt32 GetCoderOutStreamIndex(UInt32 coderIndex) const |
michael@0 | 87 | { |
michael@0 | 88 | UInt32 streamIndex = 0; |
michael@0 | 89 | for (UInt32 i = 0; i < coderIndex; i++) |
michael@0 | 90 | streamIndex += Coders[i].NumOutStreams; |
michael@0 | 91 | return streamIndex; |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | |
michael@0 | 95 | void FindInStream(UInt32 streamIndex, UInt32 &coderIndex, |
michael@0 | 96 | UInt32 &coderStreamIndex) const |
michael@0 | 97 | { |
michael@0 | 98 | for (coderIndex = 0; coderIndex < (UInt32)Coders.Size(); coderIndex++) |
michael@0 | 99 | { |
michael@0 | 100 | UInt32 curSize = Coders[coderIndex].NumInStreams; |
michael@0 | 101 | if (streamIndex < curSize) |
michael@0 | 102 | { |
michael@0 | 103 | coderStreamIndex = streamIndex; |
michael@0 | 104 | return; |
michael@0 | 105 | } |
michael@0 | 106 | streamIndex -= curSize; |
michael@0 | 107 | } |
michael@0 | 108 | throw 1; |
michael@0 | 109 | } |
michael@0 | 110 | void FindOutStream(UInt32 streamIndex, UInt32 &coderIndex, |
michael@0 | 111 | UInt32 &coderStreamIndex) const |
michael@0 | 112 | { |
michael@0 | 113 | for (coderIndex = 0; coderIndex < (UInt32)Coders.Size(); coderIndex++) |
michael@0 | 114 | { |
michael@0 | 115 | UInt32 curSize = Coders[coderIndex].NumOutStreams; |
michael@0 | 116 | if (streamIndex < curSize) |
michael@0 | 117 | { |
michael@0 | 118 | coderStreamIndex = streamIndex; |
michael@0 | 119 | return; |
michael@0 | 120 | } |
michael@0 | 121 | streamIndex -= curSize; |
michael@0 | 122 | } |
michael@0 | 123 | throw 1; |
michael@0 | 124 | } |
michael@0 | 125 | }; |
michael@0 | 126 | |
michael@0 | 127 | class CBindReverseConverter |
michael@0 | 128 | { |
michael@0 | 129 | UInt32 _numSrcOutStreams; |
michael@0 | 130 | const NCoderMixer2::CBindInfo _srcBindInfo; |
michael@0 | 131 | CRecordVector<UInt32> _srcInToDestOutMap; |
michael@0 | 132 | CRecordVector<UInt32> _srcOutToDestInMap; |
michael@0 | 133 | CRecordVector<UInt32> _destInToSrcOutMap; |
michael@0 | 134 | public: |
michael@0 | 135 | UInt32 NumSrcInStreams; |
michael@0 | 136 | CRecordVector<UInt32> DestOutToSrcInMap; |
michael@0 | 137 | |
michael@0 | 138 | CBindReverseConverter(const NCoderMixer2::CBindInfo &srcBindInfo); |
michael@0 | 139 | void CreateReverseBindInfo(NCoderMixer2::CBindInfo &destBindInfo); |
michael@0 | 140 | }; |
michael@0 | 141 | |
michael@0 | 142 | struct CCoderInfo |
michael@0 | 143 | { |
michael@0 | 144 | CMyComPtr<ICompressCoder> Coder; |
michael@0 | 145 | CMyComPtr<ICompressCoder2> Coder2; |
michael@0 | 146 | UInt32 NumInStreams; |
michael@0 | 147 | UInt32 NumOutStreams; |
michael@0 | 148 | |
michael@0 | 149 | CRecordVector<UInt64> InSizes; |
michael@0 | 150 | CRecordVector<UInt64> OutSizes; |
michael@0 | 151 | CRecordVector<const UInt64 *> InSizePointers; |
michael@0 | 152 | CRecordVector<const UInt64 *> OutSizePointers; |
michael@0 | 153 | |
michael@0 | 154 | CCoderInfo(UInt32 numInStreams, UInt32 numOutStreams); |
michael@0 | 155 | void SetCoderInfo(const UInt64 **inSizes, const UInt64 **outSizes); |
michael@0 | 156 | }; |
michael@0 | 157 | |
michael@0 | 158 | class CCoderMixer2 |
michael@0 | 159 | { |
michael@0 | 160 | public: |
michael@0 | 161 | virtual void SetBindInfo(const CBindInfo &bindInfo) = 0; |
michael@0 | 162 | virtual void ReInit() = 0; |
michael@0 | 163 | virtual void SetCoderInfo(UInt32 coderIndex, const UInt64 **inSizes, const UInt64 **outSizes) = 0; |
michael@0 | 164 | }; |
michael@0 | 165 | |
michael@0 | 166 | } |
michael@0 | 167 | #endif |
michael@0 | 168 |