SharedMemoryPlatform.h (4119B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_ipc_SharedMemoryPlatform_h 8 #define mozilla_ipc_SharedMemoryPlatform_h 9 10 #include "mozilla/ipc/SharedMemoryHandle.h" 11 #include "mozilla/ipc/SharedMemoryMapping.h" 12 #include "mozilla/Logging.h" 13 #include "mozilla/Maybe.h" 14 15 namespace mozilla::ipc::shared_memory { 16 17 /// The shared memory logger. 18 // The definition resides in `SharedMemoryHandle.cpp`. 19 extern LazyLogModule gSharedMemoryLog; 20 21 /** 22 * Functions that need to be implemented for each platform. 23 * 24 * These are static methods of a class to simplify access (the class can be 25 * made a friend to give access to platform implementations). 26 */ 27 class Platform { 28 public: 29 /** 30 * Create a new shared memory handle. 31 * 32 * @param aHandle The handle to populate. 33 * @param aSize The size of the handle. 34 * 35 * @returns Whether the handle was successfully created. 36 */ 37 static bool Create(MutableHandle& aHandle, size_t aSize); 38 39 /** 40 * Create a new freezable shared memory handle. 41 * 42 * @param aHandle The handle to populate. 43 * @param aSize The size of the handle. 44 * 45 * @returns Whether the handle was successfully created. 46 */ 47 static bool CreateFreezable(FreezableHandle& aHandle, size_t aSize); 48 49 /** 50 * Return whether a platform handle is safe to map. 51 * 52 * This is used when handles are read from IPC. 53 * 54 * @param aHandle The handle to check. 55 * 56 * @returns Whether the handle is safe to map. 57 */ 58 static bool IsSafeToMap(const PlatformHandle& aHandle); 59 60 /** 61 * Clone a handle. 62 * 63 * @param aHandle The handle to clone. 64 * 65 * @returns The cloned handle, or nullptr if not successful. 66 */ 67 static PlatformHandle CloneHandle(const PlatformHandle& aHandle); 68 69 /** 70 * Freeze a handle, returning the frozen handle. 71 * 72 * @param aHandle The handle to freeze. 73 * 74 * The inner `PlatformHandle mHandle` should be the frozen handle upon 75 * successful return. `mSize` must not change. 76 * 77 * @return Whether freezing the handle was successful. 78 */ 79 static bool Freeze(FreezableHandle& aHandle); 80 81 /** 82 * Map the given handle with the size ane fixed address. 83 * 84 * @param aHandle The handle to map. 85 * @param aOffset Offset into the shared memory region to map. 86 * @param aSize Size of the shared memory region to map. 87 * @param aFixedAddress The address at which to map the memory, or nullptr to 88 * map anywhere. 89 * @param aReadOnly Whether the mapping should be read-only. 90 * 91 * @returns The location of the mapping. 92 */ 93 static Maybe<void*> Map(const HandleBase& aHandle, uint64_t aOffset, 94 size_t aSize, void* aFixedAddress, bool aReadOnly); 95 96 /** 97 * Unmap previously mapped memory. 98 * 99 * @param aMemory The memory location to unmap. 100 * @param aSize The size of the mapping. 101 */ 102 static void Unmap(void* aMemory, size_t aSize); 103 104 /** 105 * Protect the given memory region. 106 * 107 * @param aAddr The address at the beginning of the memory region. 108 * @param aSize The size of the region to protect. 109 * @param aAccess The access level to allow. 110 * 111 * @returns Whether protection was successful. 112 */ 113 static bool Protect(char* aAddr, size_t aSize, Access aAccess); 114 115 /** 116 * Find a region of free memory. 117 * 118 * @param aSize The size of the region to locate. 119 * 120 * @returns The start of the memory region, or nullptr on error. 121 */ 122 static void* FindFreeAddressSpace(size_t aSize); 123 124 /** 125 * Return the page size of the system. 126 */ 127 static size_t PageSize(); 128 129 /** 130 * Return the allocation granularity of the system. 131 * This may be distinct from the page size, and controls the required 132 * alignment for fixed mapping addresses and shared memory offsets. 133 */ 134 static size_t AllocationGranularity(); 135 }; 136 137 } // namespace mozilla::ipc::shared_memory 138 139 #endif