blob: fd4d612d38bd35aa5d0b72314ea3ea94464a61c4 [file] [log] [blame]
Erik Klinea41902f2018-05-02 18:07:31 +09001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef NETUTILS_MEMBLOCK_H
18#define NETUTILS_MEMBLOCK_H
19
20#include <memory>
21#include "netdutils/Slice.h"
22
23namespace android {
24namespace netdutils {
25
26// A class to encapsulate self-deleting byte arrays while preserving access
27// to the underlying length (without the length being part of the type, e.g.
28// std::array<>). By design, the only interface to the underlying bytes is
29// via Slice, to encourage safer memory access usage.
30//
31// No thread-safety guarantees whatsoever.
32class MemBlock {
33 public:
34 MemBlock() : MemBlock(0U) {}
Erik Klineb1157dd2018-05-30 20:19:08 +090035 explicit MemBlock(size_t len)
Erik Klinea41902f2018-05-02 18:07:31 +090036 : mData((len > 0U) ? new uint8_t[len]{} : nullptr),
37 mLen(len) {}
38 // Allocate memory of size src.size() and copy src into this MemBlock.
Erik Klineb1157dd2018-05-30 20:19:08 +090039 explicit MemBlock(Slice src) : MemBlock(src.size()) {
Erik Klinea41902f2018-05-02 18:07:31 +090040 copy(get(), src);
41 }
42
43 // No copy construction or assignment.
44 MemBlock(const MemBlock&) = delete;
45 MemBlock& operator=(const MemBlock&) = delete;
46
47 // Move construction and assignment are okay.
48 MemBlock(MemBlock&&) = default;
49 MemBlock& operator=(MemBlock&&) = default;
50
51 // Even though this method is const, the memory wrapped by the
52 // returned Slice is mutable.
53 Slice get() const noexcept { return Slice(mData.get(), mLen); }
54
55 // Implicit cast to Slice.
Bernie Innocentidfbd0ec2018-10-18 21:06:45 +090056 // NOLINTNEXTLINE(google-explicit-constructor)
Stephen Hinesb7fe2db2018-07-11 10:45:05 -070057 operator const Slice() const noexcept { return get(); }
Erik Klinea41902f2018-05-02 18:07:31 +090058
59 private:
60 std::unique_ptr<uint8_t[]> mData;
61 size_t mLen;
62};
63
64} // namespace netdutils
65} // namespace android
66
67#endif /* NETUTILS_MEMBLOCK_H */