@@ -190,7 +190,14 @@ private static SafeFileHandle CreateSharedBackingObject(Interop.Sys.MemoryMapped
190
190
do
191
191
{
192
192
mapName = GenerateMapName ( ) ;
193
- fd = Interop . Sys . ShmOpen ( mapName , flags , ( int ) perms ) ; // Create the shared memory object.
193
+ if ( Interop . Sys . MemfdSupported )
194
+ {
195
+ fd = Interop . Sys . MemfdCreate ( mapName ) ;
196
+ }
197
+ else
198
+ {
199
+ fd = Interop . Sys . ShmOpen ( mapName , flags , ( int ) perms ) ; // Create the shared memory object.
200
+ }
194
201
195
202
if ( fd . IsInvalid )
196
203
{
@@ -204,7 +211,7 @@ private static SafeFileHandle CreateSharedBackingObject(Interop.Sys.MemoryMapped
204
211
// the result of native shm_open does not work well with our subsequent call to mmap.
205
212
return null ;
206
213
}
207
- else if ( errorInfo . Error == Interop . Error . ENAMETOOLONG )
214
+ else if ( ! Interop . Sys . MemfdSupported && errorInfo . Error == Interop . Error . ENAMETOOLONG )
208
215
{
209
216
Debug . Fail ( $ "shm_open failed with ENAMETOOLONG for { Encoding . UTF8 . GetByteCount ( mapName ) } byte long name.") ;
210
217
// in theory it should not happen anymore, but just to be extra safe we use the fallback
@@ -219,10 +226,13 @@ private static SafeFileHandle CreateSharedBackingObject(Interop.Sys.MemoryMapped
219
226
220
227
try
221
228
{
222
- // Unlink the shared memory object immediately so that it'll go away once all handles
223
- // to it are closed (as with opened then unlinked files, it'll remain usable via
224
- // the open handles even though it's unlinked and can't be opened anew via its name).
225
- Interop . CheckIo ( Interop . Sys . ShmUnlink ( mapName ) ) ;
229
+ if ( ! Interop . Sys . MemfdSupported )
230
+ {
231
+ // Unlink the shared memory object immediately so that it'll go away once all handles
232
+ // to it are closed (as with opened then unlinked files, it'll remain usable via
233
+ // the open handles even though it's unlinked and can't be opened anew via its name).
234
+ Interop . CheckIo ( Interop . Sys . ShmUnlink ( mapName ) ) ;
235
+ }
226
236
227
237
// Give it the right capacity. We do this directly with ftruncate rather
228
238
// than via FileStream.SetLength after the FileStream is created because, on some systems,
0 commit comments