/src/trafficserver/include/tsutil/LocalBuffer.h
Line | Count | Source |
1 | | /** @file |
2 | | |
3 | | LocalBuffer |
4 | | |
5 | | @section license License |
6 | | |
7 | | Licensed to the Apache Software Foundation (ASF) under one |
8 | | or more contributor license agreements. See the NOTICE file |
9 | | distributed with this work for additional information |
10 | | regarding copyright ownership. The ASF licenses this file |
11 | | to you under the Apache License, Version 2.0 (the |
12 | | "License"); you may not use this file except in compliance |
13 | | with the License. You may obtain a copy of the License at |
14 | | |
15 | | http://www.apache.org/licenses/LICENSE-2.0 |
16 | | |
17 | | Unless required by applicable law or agreed to in writing, software |
18 | | distributed under the License is distributed on an "AS IS" BASIS, |
19 | | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
20 | | See the License for the specific language governing permissions and |
21 | | limitations under the License. |
22 | | */ |
23 | | |
24 | | #pragma once |
25 | | |
26 | | #include <cstdint> |
27 | | namespace ts |
28 | | { |
29 | | template <class T = uint8_t, std::size_t EstSizeBound = 1024> class LocalBuffer |
30 | | { |
31 | | public: |
32 | | LocalBuffer(std::size_t size) |
33 | 0 | : _ptr(size == 0 ? nullptr : (size > EstSizeBound ? new T[size] : _buf)), |
34 | 0 | _size((0 < size && size <= EstSizeBound) ? EstSizeBound : size) |
35 | 0 | { |
36 | 0 | } Unexecuted instantiation: ts::LocalBuffer<char, 1024ul>::LocalBuffer(unsigned long) Unexecuted instantiation: ts::LocalBuffer<unsigned char, 4096ul>::LocalBuffer(unsigned long) Unexecuted instantiation: ts::LocalBuffer<SRV*, 16ul>::LocalBuffer(unsigned long) Unexecuted instantiation: ts::LocalBuffer<HostDBInfo*, 16ul>::LocalBuffer(unsigned long) Unexecuted instantiation: ts::LocalBuffer<bool, 64ul>::LocalBuffer(unsigned long) Unexecuted instantiation: ts::LocalBuffer<char, 8192ul>::LocalBuffer(unsigned long) Unexecuted instantiation: ts::LocalBuffer<unsigned char, 1024ul>::LocalBuffer(unsigned long) |
37 | | ~LocalBuffer(); |
38 | | |
39 | | // Don't allocate on heap |
40 | | void *operator new(std::size_t) = delete; |
41 | | void *operator new[](std::size_t) = delete; |
42 | | |
43 | | T *data() const; |
44 | | std::size_t size() const; |
45 | | |
46 | | private: |
47 | | T _buf[EstSizeBound]; |
48 | | T *const _ptr; |
49 | | const std::size_t _size; |
50 | | }; |
51 | | |
52 | | template <class T, std::size_t S> LocalBuffer<T, S>::~LocalBuffer() |
53 | 0 | { |
54 | 0 | if (_ptr && _ptr != _buf) { |
55 | 0 | delete[] _ptr; |
56 | 0 | } |
57 | 0 | } Unexecuted instantiation: ts::LocalBuffer<char, 1024ul>::~LocalBuffer() Unexecuted instantiation: ts::LocalBuffer<unsigned char, 4096ul>::~LocalBuffer() Unexecuted instantiation: ts::LocalBuffer<SRV*, 16ul>::~LocalBuffer() Unexecuted instantiation: ts::LocalBuffer<HostDBInfo*, 16ul>::~LocalBuffer() Unexecuted instantiation: ts::LocalBuffer<bool, 64ul>::~LocalBuffer() Unexecuted instantiation: ts::LocalBuffer<char, 8192ul>::~LocalBuffer() Unexecuted instantiation: ts::LocalBuffer<unsigned char, 1024ul>::~LocalBuffer() |
58 | | |
59 | | template <class T, std::size_t S> |
60 | | inline T * |
61 | | LocalBuffer<T, S>::data() const |
62 | 0 | { |
63 | 0 | return _ptr; |
64 | 0 | } Unexecuted instantiation: ts::LocalBuffer<char, 1024ul>::data() const Unexecuted instantiation: ts::LocalBuffer<unsigned char, 4096ul>::data() const Unexecuted instantiation: ts::LocalBuffer<SRV*, 16ul>::data() const Unexecuted instantiation: ts::LocalBuffer<HostDBInfo*, 16ul>::data() const Unexecuted instantiation: ts::LocalBuffer<bool, 64ul>::data() const Unexecuted instantiation: ts::LocalBuffer<char, 8192ul>::data() const Unexecuted instantiation: ts::LocalBuffer<unsigned char, 1024ul>::data() const |
65 | | |
66 | | template <class T, std::size_t S> |
67 | | inline std::size_t |
68 | | LocalBuffer<T, S>::size() const |
69 | | { |
70 | | return _size; |
71 | | } |
72 | | |
73 | | } // namespace ts |