mem_buffer_util.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_FUCHSIA_MEM_BUFFER_UTIL_H_
  5. #define BASE_FUCHSIA_MEM_BUFFER_UTIL_H_
  6. #include <fuchsia/mem/cpp/fidl.h>
  7. #include <string>
  8. #include "base/base_export.h"
  9. #include "base/files/file.h"
  10. #include "base/strings/string_piece_forward.h"
  11. #include "third_party/abseil-cpp/absl/types/optional.h"
  12. namespace base {
  13. // Returns the contents of `buffer` (which must be a valid UTF-8 string), or
  14. // null in case of a conversion error.
  15. BASE_EXPORT absl::optional<std::u16string> ReadUTF8FromVMOAsUTF16(
  16. const fuchsia::mem::Buffer& buffer);
  17. // Creates a Fuchsia VMO from `data`. The size of the resulting virtual memory
  18. // object will be set to the size of the string, and it will be given the name
  19. // `name`.
  20. BASE_EXPORT zx::vmo VmoFromString(StringPiece data, StringPiece name);
  21. // Creates a Fuchsia memory buffer from `data`. The resulting virtual memory
  22. // object will be given the name `name`.
  23. // `fuchsia::mem::Buffer` is deprecated: for new interfaces, prefer using
  24. // a VMO object directly (see `VmoFromString`).
  25. BASE_EXPORT fuchsia::mem::Buffer MemBufferFromString(StringPiece data,
  26. StringPiece name);
  27. // Creates a Fuchsia memory buffer from the UTF-16 string `data`. The resulting
  28. // virtual memory object will be given the name `name`.
  29. BASE_EXPORT fuchsia::mem::Buffer MemBufferFromString16(StringPiece16 data,
  30. StringPiece name);
  31. // Returns the contents of `data`, or null if the read operation fails.
  32. BASE_EXPORT absl::optional<std::string> StringFromVmo(const zx::vmo& vmo);
  33. // Returns the contents of `buffer`, or null if the read operation fails.
  34. // `fuchsia::mem::Buffer` is deprecated: for new interfaces, prefer using
  35. // a VMO object directly (see `StringFromVmo`).
  36. BASE_EXPORT absl::optional<std::string> StringFromMemBuffer(
  37. const fuchsia::mem::Buffer& buffer);
  38. // Returns the contents of `data`, or null if the read operation fails.
  39. BASE_EXPORT absl::optional<std::string> StringFromMemData(
  40. const fuchsia::mem::Data& data);
  41. // Creates a memory-mapped, read-only Buffer with the contents of `file`. Will
  42. // return an empty Buffer if the file could not be opened.
  43. BASE_EXPORT fuchsia::mem::Buffer MemBufferFromFile(File file);
  44. // Creates a non-resizeable, copy-on-write shared memory clone of `buffer`. The
  45. // resulting virtual memory object will be given the name `name`.
  46. BASE_EXPORT fuchsia::mem::Buffer CloneBuffer(const fuchsia::mem::Buffer& buffer,
  47. StringPiece name);
  48. } // namespace base
  49. #endif // BASE_FUCHSIA_MEM_BUFFER_UTIL_H_