VirtualBox

source: vbox/trunk/include/iprt/shmem.h@ 81025

Last change on this file since 81025 was 78561, checked in by vboxsync, 6 years ago

Runtime/RTShMem,tstRTShMem: Add API to delete a named shared memory object on POSIX hosts and make use of it in the testcase to cleanup any leftovers from previous runs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.0 KB
Line 
1/** @file
2 * IPRT - Named shared memory.
3 */
4
5/*
6 * Copyright (C) 2018-2019 Oracle Corporation
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
26#ifndef IPRT_INCLUDED_shmem_h
27#define IPRT_INCLUDED_shmem_h
28#ifndef RT_WITHOUT_PRAGMA_ONCE
29# pragma once
30#endif
31
32#include <iprt/types.h>
33
34RT_C_DECLS_BEGIN
35
36/** @defgroup grp_rt_shmem RTShMem - Shared memory.
37 * @ingroup grp_rt
38 */
39
40/** @name Open flags for RTShMemOpen().
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 */
75RTDECL(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.
81 * @retval VERR_INVALID_STATE if there is still a mapping active for the given shared memory object.
82 * @param hShMem The shared memory object handle.
83 *
84 * @note The shared memory object will be deleted if the creator closes it.
85 */
86RTDECL(int) RTShMemClose(RTSHMEM hShMem);
87
88/**
89 * Tries to delete a shared memory object with the given name.
90 *
91 * @returns IPRT status code.
92 * @retval VERR_NOT_SUPPORTED if the platform does not support deleting the shared memory object by name.
93 * @param pszName Name of the shared memory object to delete.
94 */
95RTDECL(int) RTShMemDelete(const char *pszName);
96
97/**
98 * Returns the number of references (i.e. mappings) held for the given shared memory object.
99 *
100 * @returns Reference count or 0 on invalid handle.
101 * @param hShMem The shared memory object handle.
102 */
103RTDECL(uint32_t) RTShMemRefCount(RTSHMEM hShMem);
104
105/**
106 * Sets the size of the given shared memory object.
107 *
108 * @returns IPRT status code.
109 * @retval VERR_INVALID_STATE if there are mappings active for the given shared memory object.
110 * @retval VERR_NOT_SUPPORTED on some hosts which do not support changing the size after creation.
111 * @param hShMem The shared memory object handle.
112 * @param cbMem Size of the memory object handle in bytes.
113 */
114RTDECL(int) RTShMemSetSize(RTSHMEM hShMem, size_t cbMem);
115
116/**
117 * Queries the current size of the shared memory object.
118 *
119 * @returns IPRT status code.
120 * @param hShMem The shared memory object handle.
121 * @param pcbMem Where to store the size of the shared memory object on success.
122 */
123RTDECL(int) RTShMemQuerySize(RTSHMEM hShMem, size_t *pcbMem);
124
125/** @name Region mapping flags for RTShMemMapRegion().
126 * @{
127 */
128/** Read access. */
129#define RTSHMEM_MAP_F_READ RT_BIT_32(0)
130/** Write access. */
131#define RTSHMEM_MAP_F_WRITE RT_BIT_32(1)
132/** Execute access. */
133#define RTSHMEM_MAP_F_EXEC RT_BIT_32(2)
134/** Copy on write, any write creates a new page private to the callers address space and changes
135 * in that area are not shared with other processes using the hsared memory object. */
136#define RTSHMEM_MAP_F_COW RT_BIT_32(3)
137/** Mask of all valid flags. */
138#define RTSHMEM_MAP_F_VALID_MASK UINT32_C(0x0000000f)
139/** @} */
140
141/**
142 * Maps a region of the given shared memory object into the callers address space.
143 *
144 * @returns IPRT status code.
145 * @retval VERR_SHMEM_MAXIMUM_MAPPINGS_REACHED if the maximum number of mappings was reached (host dependent).
146 * @retval VERR_ACCESS_DENIED if the requested memory access rights do not line up with the flags given when opening
147 * the memory object (requesting write access for a readonly shared memory object for example).
148 * @param hShMem The shared memory object handle.
149 * @param offRegion Offset into the shared memory object to start mapping at.
150 * @param cbRegion Size of the region to map.
151 * @param fFlags Desired properties of the mapped region, combination of RTSHMEM_MAP_F_* defines.
152 * @param ppv Where to store the start address of the mapped region on success.
153 */
154RTDECL(int) RTShMemMapRegion(RTSHMEM hShMem, size_t offRegion, size_t cbRegion, uint32_t fFlags, void **ppv);
155
156/**
157 * Unmaps the given region of the shared memory object.
158 *
159 * @returns IPRT status code.
160 * @param hShMem The shared memory object handle.
161 * @param pv Pointer to the mapped region obtained with RTShMemMapRegion() earlier on.
162 */
163RTDECL(int) RTShMemUnmapRegion(RTSHMEM hShMem, void *pv);
164
165/** @} */
166RT_C_DECLS_END
167
168#endif /* !IPRT_INCLUDED_shmem_h */
169
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette