[75879] | 1 | /** @file
|
---|
| 2 | * IPRT - Named shared memory.
|
---|
| 3 | */
|
---|
| 4 |
|
---|
| 5 | /*
|
---|
[76553] | 6 | * Copyright (C) 2018-2019 Oracle Corporation
|
---|
[75879] | 7 | *
|
---|
| 8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
| 9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
| 10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
| 11 | * General Public License (GPL) as published by the Free Software
|
---|
| 12 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
| 13 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
| 14 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
| 15 | *
|
---|
| 16 | * The contents of this file may alternatively be used under the terms
|
---|
| 17 | * of the Common Development and Distribution License Version 1.0
|
---|
| 18 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
| 19 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
| 20 | * CDDL are applicable instead of those of the GPL.
|
---|
| 21 | *
|
---|
| 22 | * You may elect to license modified versions of this file under the
|
---|
| 23 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
| 24 | */
|
---|
| 25 |
|
---|
[76557] | 26 | #ifndef IPRT_INCLUDED_shmem_h
|
---|
| 27 | #define IPRT_INCLUDED_shmem_h
|
---|
[76507] | 28 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
| 29 | # pragma once
|
---|
| 30 | #endif
|
---|
[75879] | 31 |
|
---|
| 32 | #include <iprt/types.h>
|
---|
| 33 |
|
---|
| 34 | RT_C_DECLS_BEGIN
|
---|
| 35 |
|
---|
[75883] | 36 | /** @defgroup grp_rt_shmem RTShMem - Shared memory.
|
---|
| 37 | * @ingroup grp_rt
|
---|
| 38 | */
|
---|
| 39 |
|
---|
| 40 | /** @name Open flags for RTShMemOpen().
|
---|
[75879] | 41 | * @{
|
---|
| 42 | */
|
---|
| 43 | /** Creates a new shared memory object or opens an already existing one. */
|
---|
| 44 | #define RTSHMEM_O_F_CREATE RT_BIT_32(0)
|
---|
| 45 | /** Creates a new shared memory object failing if one with the same name exists already. */
|
---|
| 46 | #define RTSHMEM_O_F_CREATE_EXCL (RTSHMEM_O_F_CREATE | RT_BIT_32(1))
|
---|
| 47 | /** Opens the shared memory object for read access. */
|
---|
| 48 | #define RTSHMEM_O_F_READ RT_BIT_32(2)
|
---|
| 49 | /** Opens the shared memory object for write access. */
|
---|
| 50 | #define RTSHMEM_O_F_WRITE RT_BIT_32(3)
|
---|
| 51 | /** Opens the shared memory object for read and write access. */
|
---|
| 52 | #define RTSHMEM_O_F_READWRITE (RTSHMEM_O_F_READ | RTSHMEM_O_F_WRITE)
|
---|
| 53 | /** Truncates the shared memory object to 0 bytes on open. */
|
---|
| 54 | #define RTSHMEM_O_F_TRUNCATE RT_BIT_32(4)
|
---|
| 55 | /** Mappings may be created with executable access right (required to be known on Windows beforehand). */
|
---|
| 56 | #define RTSHMEM_O_F_MAYBE_EXEC RT_BIT_32(5)
|
---|
| 57 | /** Mask of all valid flags. */
|
---|
| 58 | #define RTSHMEM_O_F_VALID_MASK UINT32_C(0x0000003f)
|
---|
| 59 | /** @} */
|
---|
| 60 |
|
---|
| 61 | /**
|
---|
| 62 | * Creates or opens a new shared memory object with the given name.
|
---|
| 63 | *
|
---|
| 64 | * @returns IPRT status code.
|
---|
| 65 | * @retval VERR_OUT_OF_RANGE if the mapping hint count is too big.
|
---|
| 66 | * @param phShMem Where to store the handle to the shared memory object on success.
|
---|
| 67 | * @param pszName Name of the shared memory object to open or create.
|
---|
| 68 | * @param fFlags Combination of RTSHMEM_O_F_* flags.
|
---|
| 69 | * @param cbMax Maximum number of bytes to reserve for the shared memory object.
|
---|
| 70 | * On some platforms this can be 0 and set to another value using RTShMemSetSize() afterwards.
|
---|
| 71 | * Giving 0 on Windows results in an error as shared memory objects there do not support
|
---|
| 72 | * changing the size afterwards.
|
---|
| 73 | * @param cMappingsHint Hint about the possible number of mappings created later on, set to 0 for a default value.
|
---|
| 74 | */
|
---|
| 75 | RTDECL(int) RTShMemOpen(PRTSHMEM phShMem, const char *pszName, uint32_t fFlags, size_t cbMax, uint32_t cMappingsHint);
|
---|
| 76 |
|
---|
| 77 | /**
|
---|
| 78 | * Closes the given shared memory object.
|
---|
| 79 | *
|
---|
| 80 | * @returns IPRT status code.
|
---|
[75880] | 81 | * @retval VERR_INVALID_STATE if there is still a mapping active for the given shared memory object.
|
---|
[75879] | 82 | * @param hShMem The shared memory object handle.
|
---|
| 83 | *
|
---|
| 84 | * @note The shared memory object will be deleted if the creator closes it.
|
---|
| 85 | */
|
---|
| 86 | RTDECL(int) RTShMemClose(RTSHMEM hShMem);
|
---|
| 87 |
|
---|
| 88 | /**
|
---|
| 89 | * Returns the number of references (i.e. mappings) held for the given shared memory object.
|
---|
| 90 | *
|
---|
| 91 | * @returns Reference count or 0 on invalid handle.
|
---|
| 92 | * @param hShMem The shared memory object handle.
|
---|
| 93 | */
|
---|
| 94 | RTDECL(uint32_t) RTShMemRefCount(RTSHMEM hShMem);
|
---|
| 95 |
|
---|
| 96 | /**
|
---|
| 97 | * Sets the size of the given shared memory object.
|
---|
| 98 | *
|
---|
| 99 | * @returns IPRT status code.
|
---|
| 100 | * @retval VERR_INVALID_STATE if there are mappings active for the given shared memory object.
|
---|
| 101 | * @retval VERR_NOT_SUPPORTED on some hosts which do not support changing the size after creation.
|
---|
| 102 | * @param hShMem The shared memory object handle.
|
---|
| 103 | * @param cbMem Size of the memory object handle in bytes.
|
---|
| 104 | */
|
---|
| 105 | RTDECL(int) RTShMemSetSize(RTSHMEM hShMem, size_t cbMem);
|
---|
| 106 |
|
---|
| 107 | /**
|
---|
| 108 | * Queries the current size of the shared memory object.
|
---|
| 109 | *
|
---|
| 110 | * @returns IPRT status code.
|
---|
| 111 | * @param hShMem The shared memory object handle.
|
---|
| 112 | * @param pcbMem Where to store the size of the shared memory object on success.
|
---|
| 113 | */
|
---|
| 114 | RTDECL(int) RTShMemQuerySize(RTSHMEM hShMem, size_t *pcbMem);
|
---|
| 115 |
|
---|
[75883] | 116 | /** @name Region mapping flags for RTShMemMapRegion().
|
---|
[75879] | 117 | * @{
|
---|
| 118 | */
|
---|
| 119 | /** Read access. */
|
---|
| 120 | #define RTSHMEM_MAP_F_READ RT_BIT_32(0)
|
---|
| 121 | /** Write access. */
|
---|
| 122 | #define RTSHMEM_MAP_F_WRITE RT_BIT_32(1)
|
---|
| 123 | /** Execute access. */
|
---|
| 124 | #define RTSHMEM_MAP_F_EXEC RT_BIT_32(2)
|
---|
| 125 | /** Copy on write, any write creates a new page private to the callers address space and changes
|
---|
| 126 | * in that area are not shared with other processes using the hsared memory object. */
|
---|
| 127 | #define RTSHMEM_MAP_F_COW RT_BIT_32(3)
|
---|
| 128 | /** Mask of all valid flags. */
|
---|
| 129 | #define RTSHMEM_MAP_F_VALID_MASK UINT32_C(0x0000000f)
|
---|
| 130 | /** @} */
|
---|
| 131 |
|
---|
| 132 | /**
|
---|
| 133 | * Maps a region of the given shared memory object into the callers address space.
|
---|
| 134 | *
|
---|
| 135 | * @returns IPRT status code.
|
---|
| 136 | * @retval VERR_SHMEM_MAXIMUM_MAPPINGS_REACHED if the maximum number of mappings was reached (host dependent).
|
---|
| 137 | * @retval VERR_ACCESS_DENIED if the requested memory access rights do not line up with the flags given when opening
|
---|
| 138 | * the memory object (requesting write access for a readonly shared memory object for example).
|
---|
| 139 | * @param hShMem The shared memory object handle.
|
---|
| 140 | * @param offRegion Offset into the shared memory object to start mapping at.
|
---|
| 141 | * @param cbRegion Size of the region to map.
|
---|
| 142 | * @param fFlags Desired properties of the mapped region, combination of RTSHMEM_MAP_F_* defines.
|
---|
| 143 | * @param ppv Where to store the start address of the mapped region on success.
|
---|
| 144 | */
|
---|
| 145 | RTDECL(int) RTShMemMapRegion(RTSHMEM hShMem, size_t offRegion, size_t cbRegion, uint32_t fFlags, void **ppv);
|
---|
| 146 |
|
---|
| 147 | /**
|
---|
| 148 | * Unmaps the given region of the shared memory object.
|
---|
| 149 | *
|
---|
| 150 | * @returns IPRT status code.
|
---|
| 151 | * @param hShMem The shared memory object handle.
|
---|
| 152 | * @param pv Pointer to the mapped region obtained with RTShMemMapRegion() earlier on.
|
---|
| 153 | */
|
---|
| 154 | RTDECL(int) RTShMemUnmapRegion(RTSHMEM hShMem, void *pv);
|
---|
| 155 |
|
---|
[75883] | 156 | /** @} */
|
---|
[75879] | 157 | RT_C_DECLS_END
|
---|
| 158 |
|
---|
[76585] | 159 | #endif /* !IPRT_INCLUDED_shmem_h */
|
---|
[75879] | 160 |
|
---|