MCPcopy Create free account
hub / github.com/google/snappy / SnappyArrayWriter

Class SnappyArrayWriter

snappy.cc:2292–2372  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2290// Note that this is not a "ByteSink", but a type that matches the
2291// Writer template argument to SnappyDecompressor::DecompressAllTags().
2292class SnappyArrayWriter {
2293 private:
2294 char* base_;
2295 char* op_;
2296 char* op_limit_;
2297 // If op < op_limit_min_slop_ then it's safe to unconditionally write
2298 // kSlopBytes starting at op.
2299 char* op_limit_min_slop_;
2300
2301 public:
2302 inline explicit SnappyArrayWriter(char* dst)
2303 : base_(dst),
2304 op_(dst),
2305 op_limit_(dst),
2306 op_limit_min_slop_(dst) {} // Safe default see invariant.
2307
2308 inline void SetExpectedLength(size_t len) {
2309 op_limit_ = op_ + len;
2310 // Prevent pointer from being past the buffer.
2311 op_limit_min_slop_ = op_limit_ - std::min<size_t>(kSlopBytes - 1, len);
2312 }
2313
2314 inline bool CheckLength() const { return op_ == op_limit_; }
2315
2316 char* GetOutputPtr() { return op_; }
2317 char* GetBase(ptrdiff_t* op_limit_min_slop) {
2318 *op_limit_min_slop = op_limit_min_slop_ - base_;
2319 return base_;
2320 }
2321 void SetOutputPtr(char* op) { op_ = op; }
2322
2323 inline bool Append(const char* ip, size_t len, char** op_p) {
2324 char* op = *op_p;
2325 const size_t space_left = op_limit_ - op;
2326 if (space_left < len) return false;
2327 std::memcpy(op, ip, len);
2328 *op_p = op + len;
2329 return true;
2330 }
2331
2332 inline bool TryFastAppend(const char* ip, size_t available, size_t len,
2333 char** op_p) {
2334 char* op = *op_p;
2335 const size_t space_left = op_limit_ - op;
2336 if (len <= 16 && available >= 16 + kMaximumTagLength && space_left >= 16) {
2337 // Fast path, used for the majority (about 95%) of invocations.
2338 UnalignedCopy128(ip, op);
2339 *op_p = op + len;
2340 return true;
2341 } else {
2342 return false;
2343 }
2344 }
2345
2346 SNAPPY_ATTRIBUTE_ALWAYS_INLINE
2347 inline bool AppendFromSelf(size_t offset, size_t len, char** op_p) {
2348 assert(len > 0);
2349 char* const op = *op_p;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected