xref: /netbsd-src/external/gpl3/gdb/dist/gdbsupport/byte-vector.h (revision 7d62b00eb9ad855ffcd7da46b41e23feb5476fac)
1 /* Copyright (C) 2017-2020 Free Software Foundation, Inc.
2 
3    This file is part of GDB.
4 
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.
9 
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.
14 
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/>.  */
17 
18 #ifndef COMMON_BYTE_VECTOR_H
19 #define COMMON_BYTE_VECTOR_H
20 
21 #include "gdbsupport/def-vector.h"
22 
23 namespace gdb {
24 
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:
32 
33      gdb::byte_vector buf (some_large_size);
34      fill_with_data (buf.data (), buf.size ());
35 
36    On the odd case you do need zero initialization, then you can still
37    call the overloads that specify an explicit value, like:
38 
39      gdb::byte_vector buf (some_initial_size, 0);
40      buf.resize (a_bigger_size, 0);
41 
42    (Or use std::vector<gdb_byte> instead.)
43 
44    Note that unlike std::vector<gdb_byte>, function local
45    gdb::byte_vector objects constructed with an initial size like:
46 
47      gdb::byte_vector buf (some_size);
48      fill_with_data (buf.data (), buf.size ());
49 
50    usually compile down to the exact same as:
51 
52      std::unique_ptr<byte[]> buf (new gdb_byte[some_size]);
53      fill_with_data (buf.get (), some_size);
54 
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.
57 */
58 using byte_vector = gdb::def_vector<gdb_byte>;
59 using char_vector = gdb::def_vector<char>;
60 
61 } /* namespace gdb */
62 
63 #endif /* COMMON_DEF_VECTOR_H */
64