/Users/deen/code/yugabyte-db/src/yb/util/faststring.cc
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // |
18 | | // The following only applies to changes made to this file as part of YugaByte development. |
19 | | // |
20 | | // Portions Copyright (c) YugaByte, Inc. |
21 | | // |
22 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
23 | | // in compliance with the License. You may obtain a copy of the License at |
24 | | // |
25 | | // http://www.apache.org/licenses/LICENSE-2.0 |
26 | | // |
27 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
28 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
29 | | // or implied. See the License for the specific language governing permissions and limitations |
30 | | // under the License. |
31 | | // |
32 | | |
33 | | #include "yb/util/faststring.h" |
34 | | |
35 | | #include <memory> |
36 | | |
37 | | #include <glog/logging.h> |
38 | | |
39 | | namespace yb { |
40 | | |
41 | 19.1M | void faststring::GrowByAtLeast(size_t count) { |
42 | | // Not enough space, need to reserve more. |
43 | | // Don't reserve exactly enough space for the new string -- that makes it |
44 | | // too easy to write perf bugs where you get O(n^2) append. |
45 | | // Instead, alwayhs expand by at least 50%. |
46 | | |
47 | 19.1M | size_t to_reserve = len_ + count; |
48 | 19.1M | if (len_ + count < len_ * 3 / 2) { |
49 | 11.6M | to_reserve = len_ * 3 / 2; |
50 | 11.6M | } |
51 | 19.1M | GrowArray(to_reserve); |
52 | 19.1M | } |
53 | | |
54 | 29.5M | void faststring::GrowArray(size_t newcapacity) { |
55 | 29.5M | DCHECK_GE(newcapacity, capacity_); |
56 | 29.5M | std::unique_ptr<uint8_t[]> newdata(new uint8_t[newcapacity]); |
57 | 29.5M | if (len_ > 0) { |
58 | 20.3M | memcpy(&newdata[0], &data_[0], len_); |
59 | 20.3M | } |
60 | 29.5M | capacity_ = newcapacity; |
61 | 29.5M | if (data_ != initial_data_) { |
62 | 10.4M | delete[] data_; |
63 | 19.0M | } else { |
64 | 19.0M | ASAN_POISON_MEMORY_REGION(initial_data_, arraysize(initial_data_)); |
65 | 19.0M | } |
66 | | |
67 | 29.5M | data_ = newdata.release(); |
68 | 29.5M | ASAN_POISON_MEMORY_REGION(data_ + len_, capacity_ - len_); |
69 | 29.5M | } |
70 | | |
71 | | |
72 | | } // namespace yb |