1 | /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
2 | #ident "$Id$" |
3 | /*====== |
4 | This file is part of PerconaFT. |
5 | |
6 | |
7 | Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved. |
8 | |
9 | PerconaFT is free software: you can redistribute it and/or modify |
10 | it under the terms of the GNU General Public License, version 2, |
11 | as published by the Free Software Foundation. |
12 | |
13 | PerconaFT is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | GNU General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU General Public License |
19 | along with PerconaFT. If not, see <http://www.gnu.org/licenses/>. |
20 | |
21 | ---------------------------------------- |
22 | |
23 | PerconaFT is free software: you can redistribute it and/or modify |
24 | it under the terms of the GNU Affero General Public License, version 3, |
25 | as published by the Free Software Foundation. |
26 | |
27 | PerconaFT is distributed in the hope that it will be useful, |
28 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
29 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
30 | GNU Affero General Public License for more details. |
31 | |
32 | You should have received a copy of the GNU Affero General Public License |
33 | along with PerconaFT. If not, see <http://www.gnu.org/licenses/>. |
34 | ======= */ |
35 | |
36 | #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved." |
37 | |
38 | #include <algorithm> |
39 | #include <cassert> |
40 | #include <cstdlib> |
41 | #include <memory> |
42 | |
43 | #include "buffer.hpp" |
44 | #include "malloc_utils.hpp" |
45 | |
46 | namespace mu = malloc_utils; |
47 | |
48 | namespace ftcxx { |
49 | |
50 | const size_t Buffer::INITIAL_CAPACITY = 1<<10; |
51 | const size_t Buffer::MAXIMUM_CAPACITY = 1<<18; |
52 | const double Buffer::FULLNESS_RATIO = 0.9; |
53 | |
54 | Buffer::Buffer() |
55 | : _cur(0), |
56 | _end(0), |
57 | _capacity(INITIAL_CAPACITY), |
58 | _buf(nullptr, &std::free) |
59 | { |
60 | init(); |
61 | } |
62 | |
63 | Buffer::Buffer(size_t capacity) |
64 | : _end(0), |
65 | _capacity(capacity), |
66 | _buf(nullptr, &std::free) |
67 | { |
68 | init(); |
69 | } |
70 | |
71 | char *Buffer::alloc(size_t sz) { |
72 | grow(sz); |
73 | char *p = raw(_end); |
74 | _end += sz; |
75 | return p; |
76 | } |
77 | |
78 | bool Buffer::full() const { |
79 | return _end > MAXIMUM_CAPACITY * FULLNESS_RATIO; |
80 | } |
81 | |
82 | bool Buffer::more() const { |
83 | return _cur < _end; |
84 | } |
85 | |
86 | char *Buffer::current() const { |
87 | return raw(_cur); |
88 | } |
89 | |
90 | void Buffer::advance(size_t sz) { |
91 | _cur += sz; |
92 | } |
93 | |
94 | void Buffer::clear() { |
95 | _cur = 0; |
96 | _end = 0; |
97 | } |
98 | |
99 | void Buffer::init() { |
100 | _buf.reset(static_cast<char *>(mu::checkedMalloc(_capacity))); |
101 | } |
102 | |
103 | /** |
104 | * Implements our growth strategy. Currently we double until we get |
105 | * up to 4kB so that we can quickly reach the point where jemalloc can |
106 | * help us resize in-place, but after that point we grow by a factor |
107 | * of 1.5x. |
108 | * |
109 | * FBVector doubles once it is bigger than 128kB, but I don't think we |
110 | * actually want to because that's about when we want to stop growing. |
111 | */ |
112 | size_t Buffer::next_alloc_size(size_t sz) { |
113 | if (sz < mu::jemallocMinInPlaceExpandable) { |
114 | return sz * 2; |
115 | } |
116 | #if 0 |
117 | else if (sz > (128<<10)) { |
118 | return sz * 2; |
119 | } |
120 | #endif |
121 | else { |
122 | return (sz * 3 + 1) / 2; |
123 | } |
124 | } |
125 | |
126 | void Buffer::grow(size_t sz) { |
127 | size_t new_capacity = _capacity; |
128 | while (new_capacity < _end + sz) { |
129 | new_capacity = next_alloc_size(new_capacity); |
130 | } |
131 | assert(new_capacity >= _capacity); // overflow? |
132 | if (new_capacity > _capacity) { |
133 | // This section isn't exception-safe, but smartRealloc already |
134 | // isn't. The only thing we can throw in here is |
135 | // std::bad_alloc, in which case we're kind of screwed anyway. |
136 | new_capacity = mu::goodMallocSize(new_capacity); |
137 | _buf.reset(static_cast<char *>(mu::smartRealloc(_buf.release(), _end, _capacity, new_capacity, _capacity))); |
138 | } |
139 | } |
140 | |
141 | } // namespace ftcxx |
142 | |