unsafe_shared_memory_pool.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright 2021 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_MEMORY_UNSAFE_SHARED_MEMORY_POOL_H_
  5. #define BASE_MEMORY_UNSAFE_SHARED_MEMORY_POOL_H_
  6. #include <memory>
  7. #include <utility>
  8. #include <vector>
  9. #include "base/base_export.h"
  10. #include "base/memory/ref_counted.h"
  11. #include "base/memory/unsafe_shared_memory_region.h"
  12. #include "base/synchronization/lock.h"
  13. #include "base/types/pass_key.h"
  14. namespace base {
  15. // UnsafeSharedMemoryPool manages allocation and pooling of
  16. // UnsafeSharedMemoryRegions. Using pool saves cost of repeated shared memory
  17. // allocations. Up-to 32 regions would be pooled. It is thread-safe. May return
  18. // bigger regions than requested. If a requested size is increased, all stored
  19. // regions are purged. Regions are returned to the buffer on destruction of
  20. // |SharedMemoryHandle| if they are of a correct size.
  21. class BASE_EXPORT UnsafeSharedMemoryPool
  22. : public RefCountedThreadSafe<UnsafeSharedMemoryPool> {
  23. public:
  24. // Used to store the allocation result.
  25. // This class returns memory to the pool upon destruction.
  26. class BASE_EXPORT Handle {
  27. public:
  28. Handle(PassKey<UnsafeSharedMemoryPool>,
  29. UnsafeSharedMemoryRegion region,
  30. WritableSharedMemoryMapping mapping,
  31. scoped_refptr<UnsafeSharedMemoryPool> pool);
  32. ~Handle();
  33. // Disallow copy and assign.
  34. Handle(const Handle&) = delete;
  35. Handle& operator=(const Handle&) = delete;
  36. const UnsafeSharedMemoryRegion& GetRegion() const;
  37. const WritableSharedMemoryMapping& GetMapping() const;
  38. private:
  39. UnsafeSharedMemoryRegion region_;
  40. WritableSharedMemoryMapping mapping_;
  41. scoped_refptr<UnsafeSharedMemoryPool> pool_;
  42. };
  43. UnsafeSharedMemoryPool();
  44. // Disallow copy and assign.
  45. UnsafeSharedMemoryPool(const UnsafeSharedMemoryPool&) = delete;
  46. UnsafeSharedMemoryPool& operator=(const UnsafeSharedMemoryPool&) = delete;
  47. // Allocates a region of the given |size| or reuses a previous allocation if
  48. // possible.
  49. std::unique_ptr<Handle> MaybeAllocateBuffer(size_t size);
  50. // Shuts down the pool, freeing all currently unused allocations and freeing
  51. // outstanding ones as they are returned.
  52. void Shutdown();
  53. private:
  54. friend class RefCountedThreadSafe<UnsafeSharedMemoryPool>;
  55. ~UnsafeSharedMemoryPool();
  56. void ReleaseBuffer(UnsafeSharedMemoryRegion region,
  57. WritableSharedMemoryMapping mapping);
  58. Lock lock_;
  59. // All shared memory regions cached internally are guaranteed to be
  60. // at least `region_size_` bytes in size.
  61. size_t region_size_ GUARDED_BY(lock_) = 0u;
  62. // Cached unused regions and their mappings.
  63. std::vector<std::pair<UnsafeSharedMemoryRegion, WritableSharedMemoryMapping>>
  64. regions_ GUARDED_BY(lock_);
  65. bool is_shutdown_ GUARDED_BY(lock_) = false;
  66. };
  67. } // namespace base
  68. #endif // BASE_MEMORY_UNSAFE_SHARED_MEMORY_POOL_H_