Update HOWTO document after creating the 2.42 branch
[binutils-gdb.git] / gdbsupport / byte-vector.h
blob54f4cc2edf29310102a666253936315066ed1169
1 /* Copyright (C) 2017-2024 Free Software Foundation, Inc.
3 This file is part of GDB.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 #ifndef COMMON_BYTE_VECTOR_H
19 #define COMMON_BYTE_VECTOR_H
21 #include "gdbsupport/def-vector.h"
23 namespace gdb {
25 /* byte_vector is a gdb_byte std::vector with a custom allocator that
26 unlike std::vector<gdb_byte> does not zero-initialize new elements
27 by default when the vector is created/resized. This is what you
28 usually want when working with byte buffers, since if you're
29 creating or growing a buffer you'll most surely want to fill it in
30 with data, in which case zero-initialization would be a
31 pessimization. For example:
33 gdb::byte_vector buf (some_large_size);
34 fill_with_data (buf.data (), buf.size ());
36 On the odd case you do need zero initialization, then you can still
37 call the overloads that specify an explicit value, like:
39 gdb::byte_vector buf (some_initial_size, 0);
40 buf.resize (a_bigger_size, 0);
42 (Or use std::vector<gdb_byte> instead.)
44 Note that unlike std::vector<gdb_byte>, function local
45 gdb::byte_vector objects constructed with an initial size like:
47 gdb::byte_vector buf (some_size);
48 fill_with_data (buf.data (), buf.size ());
50 usually compile down to the exact same as:
52 std::unique_ptr<byte[]> buf (new gdb_byte[some_size]);
53 fill_with_data (buf.get (), some_size);
55 with the former having the advantage of being a bit more readable,
56 and providing the whole std::vector API, if you end up needing it.
58 using byte_vector = gdb::def_vector<gdb_byte>;
59 using char_vector = gdb::def_vector<char>;
61 } /* namespace gdb */
63 #endif /* COMMON_DEF_VECTOR_H */