generic_shared_memory_id.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright 2015 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 UI_GFX_GENERIC_SHARED_MEMORY_ID_H_
  5. #define UI_GFX_GENERIC_SHARED_MEMORY_ID_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <functional>
  9. #include "base/hash/hash.h"
  10. #include "base/trace_event/memory_allocator_dump.h"
  11. #include "ui/gfx/gfx_export.h"
  12. namespace gfx {
  13. // Defines an ID type which is used across all types of shared memory
  14. // allocations in content/. This ID type is in ui/gfx, as components outside
  15. // content/ may need to hold an ID (but should not generate one).
  16. class GFX_EXPORT GenericSharedMemoryId {
  17. public:
  18. int id;
  19. // Invalid ID is -1 to match semantics of base::AtomicSequenceNumber.
  20. constexpr GenericSharedMemoryId() : id(-1) {}
  21. constexpr explicit GenericSharedMemoryId(int id) : id(id) {}
  22. GenericSharedMemoryId(const GenericSharedMemoryId& other) = default;
  23. GenericSharedMemoryId& operator=(const GenericSharedMemoryId& other) =
  24. default;
  25. bool is_valid() const { return id >= 0; }
  26. bool operator==(const GenericSharedMemoryId& other) const {
  27. return id == other.id;
  28. }
  29. bool operator<(const GenericSharedMemoryId& other) const {
  30. return id < other.id;
  31. }
  32. };
  33. // Generates GUID which can be used to trace shared memory using its
  34. // GenericSharedMemoryId.
  35. GFX_EXPORT base::trace_event::MemoryAllocatorDumpGuid
  36. GetGenericSharedGpuMemoryGUIDForTracing(
  37. uint64_t tracing_process_id,
  38. GenericSharedMemoryId generic_shared_memory_id);
  39. } // namespace gfx
  40. namespace std {
  41. template <>
  42. struct hash<gfx::GenericSharedMemoryId> {
  43. size_t operator()(gfx::GenericSharedMemoryId key) const {
  44. return std::hash<int>()(key.id);
  45. }
  46. };
  47. template <typename Second>
  48. struct hash<std::pair<gfx::GenericSharedMemoryId, Second>> {
  49. size_t operator()(
  50. const std::pair<gfx::GenericSharedMemoryId, Second>& pair) const {
  51. return base::HashInts(pair.first.id, pair.second);
  52. }
  53. };
  54. } // namespace std
  55. #endif // UI_GFX_GENERIC_SHARED_MEMORY_ID_H_