VirtualBox

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

Last change on this file since 93941 was 93115, checked in by vboxsync, 3 years ago

scm --update-copyright-year

  • 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-2022 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
41/** @name Open flags for RTShMemOpen().
42 * @{
43 */
44/** Creates a new shared memory object or opens an already existing one. */
45#define RTSHMEM_O_F_CREATE RT_BIT_32(0)
46/** Creates a new shared memory object failing if one with the same name exists already. */
47#define RTSHMEM_O_F_CREATE_EXCL (RTSHMEM_O_F_CREATE | RT_BIT_32(1))
48/** Opens the shared memory object for read access. */
49#define RTSHMEM_O_F_READ RT_BIT_32(2)
50/** Opens the shared memory object for write access. */
51#define RTSHMEM_O_F_WRITE RT_BIT_32(3)
52/** Opens the shared memory object for read and write access. */
53#define RTSHMEM_O_F_READWRITE (RTSHMEM_O_F_READ | RTSHMEM_O_F_WRITE)
54/** Truncates the shared memory object to 0 bytes on open. */
55#define RTSHMEM_O_F_TRUNCATE RT_BIT_32(4)
56/** Mappings may be created with executable access right (required to be known on Windows beforehand). */
57#define RTSHMEM_O_F_MAYBE_EXEC RT_BIT_32(5)
58/** Mask of all valid flags. */
59#define RTSHMEM_O_F_VALID_MASK UINT32_C(0x0000003f)
60/** @} */
61
62/**
63 * Creates or opens a new shared memory object with the given name.
64 *
65 * @returns IPRT status code.
66 * @retval VERR_OUT_OF_RANGE if the mapping hint count is too big.
67 * @param phShMem Where to store the handle to the shared memory object on success.
68 * @param pszName Name of the shared memory object to open or create.
69 * @param fFlags Combination of RTSHMEM_O_F_* flags.
70 * @param cbMax Maximum number of bytes to reserve for the shared memory object.
71 * On some platforms this can be 0 and set to another value using RTShMemSetSize() afterwards.
72 * Giving 0 on Windows results in an error as shared memory objects there do not support
73 * changing the size afterwards.
74 * @param cMappingsHint Hint about the possible number of mappings created later on, set to 0 for a default value.
75 */
76RTDECL(int) RTShMemOpen(PRTSHMEM phShMem, const char *pszName, uint32_t fFlags, size_t cbMax, uint32_t cMappingsHint);
77
78/**
79 * Closes the given shared memory object.
80 *
81 * @returns IPRT status code.
82 * @retval VERR_INVALID_STATE if there is still a mapping active for the given shared memory object.
83 * @param hShMem The shared memory object handle.
84 *
85 * @note The shared memory object will be deleted if the creator closes it.
86 */
87RTDECL(int) RTShMemClose(RTSHMEM hShMem);
88
89/**
90 * Tries to delete a shared memory object with the given name.
91 *
92 * @returns IPRT status code.
93 * @retval VERR_NOT_SUPPORTED if the platform does not support deleting the shared memory object by name.
94 * @param pszName Name of the shared memory object to delete.
95 */
96RTDECL(int) RTShMemDelete(const char *pszName);
97
98/**
99 * Returns the number of references (i.e. mappings) held for the given shared memory object.
100 *
101 * @returns Reference count or 0 on invalid handle.
102 * @param hShMem The shared memory object handle.
103 */
104RTDECL(uint32_t) RTShMemRefCount(RTSHMEM hShMem);
105
106/**
107 * Sets the size of the given shared memory object.
108 *
109 * @returns IPRT status code.
110 * @retval VERR_INVALID_STATE if there are mappings active for the given shared memory object.
111 * @retval VERR_NOT_SUPPORTED on some hosts which do not support changing the size after creation.
112 * @param hShMem The shared memory object handle.
113 * @param cbMem Size of the memory object handle in bytes.
114 */
115RTDECL(int) RTShMemSetSize(RTSHMEM hShMem, size_t cbMem);
116
117/**
118 * Queries the current size of the shared memory object.
119 *
120 * @returns IPRT status code.
121 * @param hShMem The shared memory object handle.
122 * @param pcbMem Where to store the size of the shared memory object on success.
123 */
124RTDECL(int) RTShMemQuerySize(RTSHMEM hShMem, size_t *pcbMem);
125
126/** @name Region mapping flags for RTShMemMapRegion().
127 * @{
128 */
129/** Read access. */
130#define RTSHMEM_MAP_F_READ RT_BIT_32(0)
131/** Write access. */
132#define RTSHMEM_MAP_F_WRITE RT_BIT_32(1)
133/** Execute access. */
134#define RTSHMEM_MAP_F_EXEC RT_BIT_32(2)
135/** Copy on write, any write creates a new page private to the callers address space and changes
136 * in that area are not shared with other processes using the hsared memory object. */
137#define RTSHMEM_MAP_F_COW RT_BIT_32(3)
138/** Mask of all valid flags. */
139#define RTSHMEM_MAP_F_VALID_MASK UINT32_C(0x0000000f)
140/** @} */
141
142/**
143 * Maps a region of the given shared memory object into the callers address space.
144 *
145 * @returns IPRT status code.
146 * @retval VERR_SHMEM_MAXIMUM_MAPPINGS_REACHED if the maximum number of mappings was reached (host dependent).
147 * @retval VERR_ACCESS_DENIED if the requested memory access rights do not line up with the flags given when opening
148 * the memory object (requesting write access for a readonly shared memory object for example).
149 * @param hShMem The shared memory object handle.
150 * @param offRegion Offset into the shared memory object to start mapping at.
151 * @param cbRegion Size of the region to map.
152 * @param fFlags Desired properties of the mapped region, combination of RTSHMEM_MAP_F_* defines.
153 * @param ppv Where to store the start address of the mapped region on success.
154 */
155RTDECL(int) RTShMemMapRegion(RTSHMEM hShMem, size_t offRegion, size_t cbRegion, uint32_t fFlags, void **ppv);
156
157/**
158 * Unmaps the given region of the shared memory object.
159 *
160 * @returns IPRT status code.
161 * @param hShMem The shared memory object handle.
162 * @param pv Pointer to the mapped region obtained with RTShMemMapRegion() earlier on.
163 */
164RTDECL(int) RTShMemUnmapRegion(RTSHMEM hShMem, void *pv);
165
166/** @} */
167RT_C_DECLS_END
168
169#endif /* !IPRT_INCLUDED_shmem_h */
170
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