snappy-sinksource.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // Copyright 2011 Google Inc. All Rights Reserved.
  2. //
  3. // Redistribution and use in source and binary forms, with or without
  4. // modification, are permitted provided that the following conditions are
  5. // met:
  6. //
  7. // * Redistributions of source code must retain the above copyright
  8. // notice, this list of conditions and the following disclaimer.
  9. // * Redistributions in binary form must reproduce the above
  10. // copyright notice, this list of conditions and the following disclaimer
  11. // in the documentation and/or other materials provided with the
  12. // distribution.
  13. // * Neither the name of Google Inc. nor the names of its
  14. // contributors may be used to endorse or promote products derived from
  15. // this software without specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  21. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. #ifndef THIRD_PARTY_SNAPPY_SNAPPY_SINKSOURCE_H_
  29. #define THIRD_PARTY_SNAPPY_SNAPPY_SINKSOURCE_H_
  30. #include <stddef.h>
  31. namespace snappy {
  32. // A Sink is an interface that consumes a sequence of bytes.
  33. class Sink {
  34. public:
  35. Sink() { }
  36. virtual ~Sink();
  37. // Append "bytes[0,n-1]" to this.
  38. virtual void Append(const char* bytes, size_t n) = 0;
  39. // Returns a writable buffer of the specified length for appending.
  40. // May return a pointer to the caller-owned scratch buffer which
  41. // must have at least the indicated length. The returned buffer is
  42. // only valid until the next operation on this Sink.
  43. //
  44. // After writing at most "length" bytes, call Append() with the
  45. // pointer returned from this function and the number of bytes
  46. // written. Many Append() implementations will avoid copying
  47. // bytes if this function returned an internal buffer.
  48. //
  49. // If a non-scratch buffer is returned, the caller may only pass a
  50. // prefix of it to Append(). That is, it is not correct to pass an
  51. // interior pointer of the returned array to Append().
  52. //
  53. // The default implementation always returns the scratch buffer.
  54. virtual char* GetAppendBuffer(size_t length, char* scratch);
  55. // For higher performance, Sink implementations can provide custom
  56. // AppendAndTakeOwnership() and GetAppendBufferVariable() methods.
  57. // These methods can reduce the number of copies done during
  58. // compression/decompression.
  59. // Append "bytes[0,n-1] to the sink. Takes ownership of "bytes"
  60. // and calls the deleter function as (*deleter)(deleter_arg, bytes, n)
  61. // to free the buffer. deleter function must be non NULL.
  62. //
  63. // The default implementation just calls Append and frees "bytes".
  64. // Other implementations may avoid a copy while appending the buffer.
  65. virtual void AppendAndTakeOwnership(
  66. char* bytes, size_t n, void (*deleter)(void*, const char*, size_t),
  67. void *deleter_arg);
  68. // Returns a writable buffer for appending and writes the buffer's capacity to
  69. // *allocated_size. Guarantees *allocated_size >= min_size.
  70. // May return a pointer to the caller-owned scratch buffer which must have
  71. // scratch_size >= min_size.
  72. //
  73. // The returned buffer is only valid until the next operation
  74. // on this ByteSink.
  75. //
  76. // After writing at most *allocated_size bytes, call Append() with the
  77. // pointer returned from this function and the number of bytes written.
  78. // Many Append() implementations will avoid copying bytes if this function
  79. // returned an internal buffer.
  80. //
  81. // If the sink implementation allocates or reallocates an internal buffer,
  82. // it should use the desired_size_hint if appropriate. If a caller cannot
  83. // provide a reasonable guess at the desired capacity, it should set
  84. // desired_size_hint = 0.
  85. //
  86. // If a non-scratch buffer is returned, the caller may only pass
  87. // a prefix to it to Append(). That is, it is not correct to pass an
  88. // interior pointer to Append().
  89. //
  90. // The default implementation always returns the scratch buffer.
  91. virtual char* GetAppendBufferVariable(
  92. size_t min_size, size_t desired_size_hint, char* scratch,
  93. size_t scratch_size, size_t* allocated_size);
  94. private:
  95. // No copying
  96. Sink(const Sink&);
  97. void operator=(const Sink&);
  98. };
  99. // A Source is an interface that yields a sequence of bytes
  100. class Source {
  101. public:
  102. Source() { }
  103. virtual ~Source();
  104. // Return the number of bytes left to read from the source
  105. virtual size_t Available() const = 0;
  106. // Peek at the next flat region of the source. Does not reposition
  107. // the source. The returned region is empty iff Available()==0.
  108. //
  109. // Returns a pointer to the beginning of the region and store its
  110. // length in *len.
  111. //
  112. // The returned region is valid until the next call to Skip() or
  113. // until this object is destroyed, whichever occurs first.
  114. //
  115. // The returned region may be larger than Available() (for example
  116. // if this ByteSource is a view on a substring of a larger source).
  117. // The caller is responsible for ensuring that it only reads the
  118. // Available() bytes.
  119. virtual const char* Peek(size_t* len) = 0;
  120. // Skip the next n bytes. Invalidates any buffer returned by
  121. // a previous call to Peek().
  122. // REQUIRES: Available() >= n
  123. virtual void Skip(size_t n) = 0;
  124. private:
  125. // No copying
  126. Source(const Source&);
  127. void operator=(const Source&);
  128. };
  129. // A Source implementation that yields the contents of a flat array
  130. class ByteArraySource : public Source {
  131. public:
  132. ByteArraySource(const char* p, size_t n) : ptr_(p), left_(n) { }
  133. ~ByteArraySource() override;
  134. size_t Available() const override;
  135. const char* Peek(size_t* len) override;
  136. void Skip(size_t n) override;
  137. private:
  138. const char* ptr_;
  139. size_t left_;
  140. };
  141. // A Sink implementation that writes to a flat array without any bound checks.
  142. class UncheckedByteArraySink : public Sink {
  143. public:
  144. explicit UncheckedByteArraySink(char* dest) : dest_(dest) { }
  145. ~UncheckedByteArraySink() override;
  146. void Append(const char* data, size_t n) override;
  147. char* GetAppendBuffer(size_t len, char* scratch) override;
  148. char* GetAppendBufferVariable(
  149. size_t min_size, size_t desired_size_hint, char* scratch,
  150. size_t scratch_size, size_t* allocated_size) override;
  151. void AppendAndTakeOwnership(
  152. char* bytes, size_t n, void (*deleter)(void*, const char*, size_t),
  153. void *deleter_arg) override;
  154. // Return the current output pointer so that a caller can see how
  155. // many bytes were produced.
  156. // Note: this is not a Sink method.
  157. char* CurrentDestination() const { return dest_; }
  158. private:
  159. char* dest_;
  160. };
  161. } // namespace snappy
  162. #endif // THIRD_PARTY_SNAPPY_SNAPPY_SINKSOURCE_H_