/src/s2geometry/src/s2/util/coding/coder.cc
Line | Count | Source |
1 | | // Copyright 2000 Google Inc. All Rights Reserved. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS-IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | // |
15 | | |
16 | | // |
17 | | // |
18 | | |
19 | | #include "s2/util/coding/coder.h" |
20 | | |
21 | | #include <algorithm> |
22 | | #include <cassert> |
23 | | #include <cstdint> |
24 | | #include <tuple> |
25 | | #include <utility> |
26 | | |
27 | | #include "absl/log/absl_check.h" |
28 | | |
29 | | #include "s2/base/malloc_extension.h" |
30 | | |
31 | | Encoder::Encoder(Encoder&& other) |
32 | 0 | : buf_(std::exchange(other.buf_, nullptr)), |
33 | 0 | limit_(std::exchange(other.limit_, nullptr)), |
34 | 0 | underlying_buffer_(std::exchange(other.underlying_buffer_, nullptr)), |
35 | 0 | orig_(std::exchange(other.orig_, nullptr)) {} |
36 | | |
37 | 0 | Encoder& Encoder::operator=(Encoder&& other) { |
38 | 0 | if (this == &other) return *this; |
39 | 0 | if (ensure_allowed()) DeleteBuffer(underlying_buffer_, capacity()); |
40 | 0 | buf_ = std::exchange(other.buf_, nullptr); |
41 | 0 | limit_ = std::exchange(other.limit_, nullptr); |
42 | 0 | underlying_buffer_ = std::exchange(other.underlying_buffer_, nullptr); |
43 | 0 | orig_ = std::exchange(other.orig_, nullptr); |
44 | 0 | return *this; |
45 | 0 | } |
46 | | |
47 | 0 | Encoder::~Encoder() { |
48 | 0 | ABSL_CHECK_LE(buf_, limit_); // Catch the buffer overflow. |
49 | 0 | if (ensure_allowed()) DeleteBuffer(underlying_buffer_, capacity()); |
50 | 0 | } |
51 | | |
52 | 0 | int Encoder::varint32_length(uint32_t v) { return Varint::Length32(v); } |
53 | | |
54 | 0 | int Encoder::varint64_length(uint64_t v) { return Varint::Length64(v); } |
55 | | |
56 | 0 | std::pair<unsigned char*, size_t> Encoder::NewBuffer(size_t size) { |
57 | 0 | auto res = __size_returning_new(size); |
58 | 0 | return {static_cast<unsigned char*>(res.p), res.n}; |
59 | 0 | } |
60 | | |
61 | 0 | void Encoder::DeleteBuffer(unsigned char* buf, size_t size) { |
62 | 0 | ::operator delete(buf, size); |
63 | 0 | } |
64 | | |
65 | 0 | void Encoder::EnsureSlowPath(size_t N) { |
66 | 0 | ABSL_CHECK(ensure_allowed()); |
67 | 0 | assert(avail() < N); |
68 | 0 | assert(length() == 0 || orig_ == underlying_buffer_); |
69 | | |
70 | | // Double buffer size, but make sure we always have at least N extra bytes |
71 | 0 | const size_t current_len = length(); |
72 | 0 | const auto [new_buffer, new_capacity] = |
73 | 0 | NewBuffer(std::max(current_len + N, 2 * current_len)); |
74 | |
|
75 | 0 | if (underlying_buffer_) { |
76 | 0 | memcpy(new_buffer, underlying_buffer_, current_len); |
77 | 0 | DeleteBuffer(underlying_buffer_, capacity()); |
78 | 0 | } |
79 | 0 | underlying_buffer_ = new_buffer; |
80 | |
|
81 | 0 | orig_ = new_buffer; |
82 | 0 | limit_ = new_buffer + new_capacity; |
83 | 0 | buf_ = orig_ + current_len; |
84 | 0 | ABSL_CHECK(avail() >= N); |
85 | 0 | } |
86 | | |
87 | 0 | void Encoder::Resize(size_t N) { |
88 | 0 | ABSL_CHECK(length() >= N); |
89 | 0 | buf_ = orig_ + N; |
90 | | assert(length() == N); |
91 | 0 | } |