byte-vector.h 2.2 KB

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