other-licenses/snappy/src/snappy-internal.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

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 // Copyright 2008 Google Inc. All Rights Reserved.
michael@0 2 //
michael@0 3 // Redistribution and use in source and binary forms, with or without
michael@0 4 // modification, are permitted provided that the following conditions are
michael@0 5 // met:
michael@0 6 //
michael@0 7 // * Redistributions of source code must retain the above copyright
michael@0 8 // notice, this list of conditions and the following disclaimer.
michael@0 9 // * Redistributions in binary form must reproduce the above
michael@0 10 // copyright notice, this list of conditions and the following disclaimer
michael@0 11 // in the documentation and/or other materials provided with the
michael@0 12 // distribution.
michael@0 13 // * Neither the name of Google Inc. nor the names of its
michael@0 14 // contributors may be used to endorse or promote products derived from
michael@0 15 // this software without specific prior written permission.
michael@0 16 //
michael@0 17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0 21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0 23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 28 //
michael@0 29 // Internals shared between the Snappy implementation and its unittest.
michael@0 30
michael@0 31 #ifndef UTIL_SNAPPY_SNAPPY_INTERNAL_H_
michael@0 32 #define UTIL_SNAPPY_SNAPPY_INTERNAL_H_
michael@0 33
michael@0 34 #include "snappy-stubs-internal.h"
michael@0 35
michael@0 36 namespace snappy {
michael@0 37 namespace internal {
michael@0 38
michael@0 39 class WorkingMemory {
michael@0 40 public:
michael@0 41 WorkingMemory() : large_table_(NULL) { }
michael@0 42 ~WorkingMemory() { delete[] large_table_; }
michael@0 43
michael@0 44 // Allocates and clears a hash table using memory in "*this",
michael@0 45 // stores the number of buckets in "*table_size" and returns a pointer to
michael@0 46 // the base of the hash table.
michael@0 47 uint16* GetHashTable(size_t input_size, int* table_size);
michael@0 48
michael@0 49 private:
michael@0 50 uint16 small_table_[1<<10]; // 2KB
michael@0 51 uint16* large_table_; // Allocated only when needed
michael@0 52
michael@0 53 DISALLOW_COPY_AND_ASSIGN(WorkingMemory);
michael@0 54 };
michael@0 55
michael@0 56 // Flat array compression that does not emit the "uncompressed length"
michael@0 57 // prefix. Compresses "input" string to the "*op" buffer.
michael@0 58 //
michael@0 59 // REQUIRES: "input_length <= kBlockSize"
michael@0 60 // REQUIRES: "op" points to an array of memory that is at least
michael@0 61 // "MaxCompressedLength(input_length)" in size.
michael@0 62 // REQUIRES: All elements in "table[0..table_size-1]" are initialized to zero.
michael@0 63 // REQUIRES: "table_size" is a power of two
michael@0 64 //
michael@0 65 // Returns an "end" pointer into "op" buffer.
michael@0 66 // "end - op" is the compressed size of "input".
michael@0 67 char* CompressFragment(const char* input,
michael@0 68 size_t input_length,
michael@0 69 char* op,
michael@0 70 uint16* table,
michael@0 71 const int table_size);
michael@0 72
michael@0 73 // Return the largest n such that
michael@0 74 //
michael@0 75 // s1[0,n-1] == s2[0,n-1]
michael@0 76 // and n <= (s2_limit - s2).
michael@0 77 //
michael@0 78 // Does not read *s2_limit or beyond.
michael@0 79 // Does not read *(s1 + (s2_limit - s2)) or beyond.
michael@0 80 // Requires that s2_limit >= s2.
michael@0 81 //
michael@0 82 // Separate implementation for x86_64, for speed. Uses the fact that
michael@0 83 // x86_64 is little endian.
michael@0 84 #if defined(ARCH_K8)
michael@0 85 static inline int FindMatchLength(const char* s1,
michael@0 86 const char* s2,
michael@0 87 const char* s2_limit) {
michael@0 88 DCHECK_GE(s2_limit, s2);
michael@0 89 int matched = 0;
michael@0 90
michael@0 91 // Find out how long the match is. We loop over the data 64 bits at a
michael@0 92 // time until we find a 64-bit block that doesn't match; then we find
michael@0 93 // the first non-matching bit and use that to calculate the total
michael@0 94 // length of the match.
michael@0 95 while (PREDICT_TRUE(s2 <= s2_limit - 8)) {
michael@0 96 if (PREDICT_FALSE(UNALIGNED_LOAD64(s2) == UNALIGNED_LOAD64(s1 + matched))) {
michael@0 97 s2 += 8;
michael@0 98 matched += 8;
michael@0 99 } else {
michael@0 100 // On current (mid-2008) Opteron models there is a 3% more
michael@0 101 // efficient code sequence to find the first non-matching byte.
michael@0 102 // However, what follows is ~10% better on Intel Core 2 and newer,
michael@0 103 // and we expect AMD's bsf instruction to improve.
michael@0 104 uint64 x = UNALIGNED_LOAD64(s2) ^ UNALIGNED_LOAD64(s1 + matched);
michael@0 105 int matching_bits = Bits::FindLSBSetNonZero64(x);
michael@0 106 matched += matching_bits >> 3;
michael@0 107 return matched;
michael@0 108 }
michael@0 109 }
michael@0 110 while (PREDICT_TRUE(s2 < s2_limit)) {
michael@0 111 if (PREDICT_TRUE(s1[matched] == *s2)) {
michael@0 112 ++s2;
michael@0 113 ++matched;
michael@0 114 } else {
michael@0 115 return matched;
michael@0 116 }
michael@0 117 }
michael@0 118 return matched;
michael@0 119 }
michael@0 120 #else
michael@0 121 static inline int FindMatchLength(const char* s1,
michael@0 122 const char* s2,
michael@0 123 const char* s2_limit) {
michael@0 124 // Implementation based on the x86-64 version, above.
michael@0 125 DCHECK_GE(s2_limit, s2);
michael@0 126 int matched = 0;
michael@0 127
michael@0 128 while (s2 <= s2_limit - 4 &&
michael@0 129 UNALIGNED_LOAD32(s2) == UNALIGNED_LOAD32(s1 + matched)) {
michael@0 130 s2 += 4;
michael@0 131 matched += 4;
michael@0 132 }
michael@0 133 if (LittleEndian::IsLittleEndian() && s2 <= s2_limit - 4) {
michael@0 134 uint32 x = UNALIGNED_LOAD32(s2) ^ UNALIGNED_LOAD32(s1 + matched);
michael@0 135 int matching_bits = Bits::FindLSBSetNonZero(x);
michael@0 136 matched += matching_bits >> 3;
michael@0 137 } else {
michael@0 138 while ((s2 < s2_limit) && (s1[matched] == *s2)) {
michael@0 139 ++s2;
michael@0 140 ++matched;
michael@0 141 }
michael@0 142 }
michael@0 143 return matched;
michael@0 144 }
michael@0 145 #endif
michael@0 146
michael@0 147 } // end namespace internal
michael@0 148 } // end namespace snappy
michael@0 149
michael@0 150 #endif // UTIL_SNAPPY_SNAPPY_INTERNAL_H_

mercurial