xref: /netbsd-src/external/gpl3/gdb.old/dist/gdbsupport/byte-vector.h (revision 6881a4007f077b54e5f51159c52b9b25f57deb0d)
1*6881a400Schristos /* Copyright (C) 2017-2023 Free Software Foundation, Inc.
27d62b00eSchristos 
37d62b00eSchristos    This file is part of GDB.
47d62b00eSchristos 
57d62b00eSchristos    This program is free software; you can redistribute it and/or modify
67d62b00eSchristos    it under the terms of the GNU General Public License as published by
77d62b00eSchristos    the Free Software Foundation; either version 3 of the License, or
87d62b00eSchristos    (at your option) any later version.
97d62b00eSchristos 
107d62b00eSchristos    This program is distributed in the hope that it will be useful,
117d62b00eSchristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
127d62b00eSchristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
137d62b00eSchristos    GNU General Public License for more details.
147d62b00eSchristos 
157d62b00eSchristos    You should have received a copy of the GNU General Public License
167d62b00eSchristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
177d62b00eSchristos 
187d62b00eSchristos #ifndef COMMON_BYTE_VECTOR_H
197d62b00eSchristos #define COMMON_BYTE_VECTOR_H
207d62b00eSchristos 
217d62b00eSchristos #include "gdbsupport/def-vector.h"
227d62b00eSchristos 
237d62b00eSchristos namespace gdb {
247d62b00eSchristos 
257d62b00eSchristos /* byte_vector is a gdb_byte std::vector with a custom allocator that
267d62b00eSchristos    unlike std::vector<gdb_byte> does not zero-initialize new elements
277d62b00eSchristos    by default when the vector is created/resized.  This is what you
287d62b00eSchristos    usually want when working with byte buffers, since if you're
297d62b00eSchristos    creating or growing a buffer you'll most surely want to fill it in
307d62b00eSchristos    with data, in which case zero-initialization would be a
317d62b00eSchristos    pessimization.  For example:
327d62b00eSchristos 
337d62b00eSchristos      gdb::byte_vector buf (some_large_size);
347d62b00eSchristos      fill_with_data (buf.data (), buf.size ());
357d62b00eSchristos 
367d62b00eSchristos    On the odd case you do need zero initialization, then you can still
377d62b00eSchristos    call the overloads that specify an explicit value, like:
387d62b00eSchristos 
397d62b00eSchristos      gdb::byte_vector buf (some_initial_size, 0);
407d62b00eSchristos      buf.resize (a_bigger_size, 0);
417d62b00eSchristos 
427d62b00eSchristos    (Or use std::vector<gdb_byte> instead.)
437d62b00eSchristos 
447d62b00eSchristos    Note that unlike std::vector<gdb_byte>, function local
457d62b00eSchristos    gdb::byte_vector objects constructed with an initial size like:
467d62b00eSchristos 
477d62b00eSchristos      gdb::byte_vector buf (some_size);
487d62b00eSchristos      fill_with_data (buf.data (), buf.size ());
497d62b00eSchristos 
507d62b00eSchristos    usually compile down to the exact same as:
517d62b00eSchristos 
527d62b00eSchristos      std::unique_ptr<byte[]> buf (new gdb_byte[some_size]);
537d62b00eSchristos      fill_with_data (buf.get (), some_size);
547d62b00eSchristos 
557d62b00eSchristos    with the former having the advantage of being a bit more readable,
567d62b00eSchristos    and providing the whole std::vector API, if you end up needing it.
577d62b00eSchristos */
587d62b00eSchristos using byte_vector = gdb::def_vector<gdb_byte>;
597d62b00eSchristos using char_vector = gdb::def_vector<char>;
607d62b00eSchristos 
617d62b00eSchristos } /* namespace gdb */
627d62b00eSchristos 
637d62b00eSchristos #endif /* COMMON_DEF_VECTOR_H */
64