1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
|
// Copyright Epic Games, Inc. All Rights Reserved.
#include <zencore/iobuffer.h>
#include <zencore/except.h>
#include <zencore/filesystem.h>
#include <zencore/fmtutils.h>
#include <zencore/iohash.h>
#include <zencore/logging.h>
#include <zencore/memory.h>
#include <zencore/testing.h>
#include <zencore/thread.h>
#include <memory.h>
#include <system_error>
#if ZEN_USE_MIMALLOC
ZEN_THIRD_PARTY_INCLUDES_START
# include <mimalloc.h>
ZEN_THIRD_PARTY_INCLUDES_END
#endif
#if ZEN_PLATFORM_WINDOWS
# include <atlfile.h>
#else
# include <sys/stat.h>
# include <sys/mman.h>
#endif
#include <gsl/gsl-lite.hpp>
namespace zen {
//////////////////////////////////////////////////////////////////////////
void
IoBufferCore::AllocateBuffer(size_t InSize, size_t Alignment)
{
#if ZEN_PLATFORM_WINDOWS
if (((InSize & 0xffFF) == 0) && (Alignment == 0x10000))
{
m_Flags.fetch_or(kLowLevelAlloc, std::memory_order_relaxed);
m_DataPtr = VirtualAlloc(nullptr, InSize, MEM_COMMIT, PAGE_READWRITE);
return;
}
#endif // ZEN_PLATFORM_WINDOWS
#if ZEN_USE_MIMALLOC
void* Ptr = mi_aligned_alloc(Alignment, RoundUp(InSize, Alignment));
m_Flags.fetch_or(kIoBufferAlloc, std::memory_order_relaxed);
#else
void* Ptr = Memory::Alloc(InSize, Alignment);
#endif
ZEN_ASSERT(Ptr);
m_DataPtr = Ptr;
}
void
IoBufferCore::FreeBuffer()
{
if (!m_DataPtr)
{
return;
}
const uint32_t LocalFlags = m_Flags.load(std::memory_order_relaxed);
#if ZEN_PLATFORM_WINDOWS
if (LocalFlags & kLowLevelAlloc)
{
VirtualFree(const_cast<void*>(m_DataPtr), 0, MEM_DECOMMIT);
return;
}
#endif // ZEN_PLATFORM_WINDOWS
#if ZEN_USE_MIMALLOC
if (LocalFlags & kIoBufferAlloc)
{
return mi_free(const_cast<void*>(m_DataPtr));
}
#endif
ZEN_UNUSED(LocalFlags);
return Memory::Free(const_cast<void*>(m_DataPtr));
}
//////////////////////////////////////////////////////////////////////////
static_assert(sizeof(IoBufferCore) == 32);
IoBufferCore::IoBufferCore(size_t InSize)
{
ZEN_ASSERT(InSize);
AllocateBuffer(InSize, sizeof(void*));
m_DataBytes = InSize;
SetIsOwnedByThis(true);
}
IoBufferCore::IoBufferCore(size_t InSize, size_t Alignment)
{
ZEN_ASSERT(InSize);
AllocateBuffer(InSize, Alignment);
m_DataBytes = InSize;
SetIsOwnedByThis(true);
}
IoBufferCore::~IoBufferCore()
{
if (IsOwnedByThis() && m_DataPtr)
{
FreeBuffer();
m_DataPtr = nullptr;
}
}
void
IoBufferCore::DeleteThis() const
{
// We do this just to avoid paying for the cost of a vtable
if (const IoBufferExtendedCore* _ = ExtendedCore())
{
delete _;
}
else
{
delete this;
}
}
void
IoBufferCore::Materialize() const
{
if (const IoBufferExtendedCore* _ = ExtendedCore())
{
_->Materialize();
}
}
void
IoBufferCore::MakeOwned(bool Immutable)
{
if (!IsOwned())
{
const void* OldDataPtr = m_DataPtr;
AllocateBuffer(m_DataBytes, sizeof(void*));
memcpy(const_cast<void*>(m_DataPtr), OldDataPtr, m_DataBytes);
SetIsOwnedByThis(true);
}
SetIsImmutable(Immutable);
}
void*
IoBufferCore::MutableDataPointer() const
{
EnsureDataValid();
ZEN_ASSERT(!IsImmutable());
return const_cast<void*>(m_DataPtr);
}
//////////////////////////////////////////////////////////////////////////
IoBufferExtendedCore::IoBufferExtendedCore(void* FileHandle, uint64_t Offset, uint64_t Size, bool TransferHandleOwnership)
: IoBufferCore(nullptr, Size)
, m_FileHandle(FileHandle)
, m_FileOffset(Offset)
{
uint32_t NewFlags = kIsOwnedByThis | kIsExtended;
if (TransferHandleOwnership)
{
NewFlags |= kOwnsFile;
}
m_Flags.fetch_or(NewFlags, std::memory_order_relaxed);
}
IoBufferExtendedCore::IoBufferExtendedCore(const IoBufferExtendedCore* Outer, uint64_t Offset, uint64_t Size)
: IoBufferCore(Outer, nullptr, Size)
, m_FileHandle(Outer->m_FileHandle)
, m_FileOffset(Outer->m_FileOffset + Offset)
{
m_Flags.fetch_or(kIsExtended, std::memory_order_relaxed);
}
IoBufferExtendedCore::~IoBufferExtendedCore()
{
if (m_MappedPointer)
{
#if ZEN_PLATFORM_WINDOWS
UnmapViewOfFile(m_MappedPointer);
#else
uint64_t MapSize = ~uint64_t(uintptr_t(m_MmapHandle));
munmap(m_MappedPointer, MapSize);
#endif
}
const uint32_t LocalFlags = m_Flags.load(std::memory_order_relaxed);
#if ZEN_PLATFORM_WINDOWS
if (LocalFlags & kOwnsMmap)
{
CloseHandle(m_MmapHandle);
}
#endif
if (LocalFlags & kOwnsFile)
{
if (m_DeleteOnClose)
{
#if ZEN_PLATFORM_WINDOWS
// Mark file for deletion when final handle is closed
FILE_DISPOSITION_INFO Fdi{.DeleteFile = TRUE};
SetFileInformationByHandle(m_FileHandle, FileDispositionInfo, &Fdi, sizeof Fdi);
#else
std::filesystem::path FilePath = zen::PathFromHandle(m_FileHandle);
unlink(FilePath.c_str());
#endif
}
#if ZEN_PLATFORM_WINDOWS
BOOL Success = CloseHandle(m_FileHandle);
#else
int Fd = int(uintptr_t(m_FileHandle));
bool Success = (close(Fd) == 0);
#endif
if (!Success)
{
ZEN_WARN("Error reported on file handle close, reason '{}'", GetLastErrorAsString());
}
}
m_DataPtr = nullptr;
}
static constexpr size_t MappingLockCount = 64;
static_assert(IsPow2(MappingLockCount), "MappingLockCount must be power of two");
static RwLock g_MappingLocks[MappingLockCount];
static RwLock&
MappingLockForInstance(const IoBufferExtendedCore* instance)
{
intptr_t base = (intptr_t)instance;
size_t lock_index = ((base >> 8) ^ (base >> 16)) & (MappingLockCount - 1u);
return g_MappingLocks[lock_index];
}
void
IoBufferExtendedCore::Materialize() const
{
// The synchronization scheme here is very primitive, if we end up with
// a lot of contention we can make it more fine-grained
if (m_Flags.load(std::memory_order_acquire) & kIsMaterialized)
return;
RwLock::ExclusiveLockScope _(MappingLockForInstance(this));
// Someone could have gotten here first
// We can use memory_order_relaxed on this load because the mutex has already provided the fence
if (m_Flags.load(std::memory_order_relaxed) & kIsMaterialized)
return;
void* NewMmapHandle;
uint32_t NewFlags = kIsMaterialized;
const uint64_t MapOffset = m_FileOffset & ~0xffffull;
const uint64_t MappedOffsetDisplacement = m_FileOffset - MapOffset;
const uint64_t MapSize = m_DataBytes + MappedOffsetDisplacement;
#if ZEN_PLATFORM_WINDOWS
NewMmapHandle = CreateFileMapping(m_FileHandle,
/* lpFileMappingAttributes */ nullptr,
/* flProtect */ PAGE_READONLY,
/* dwMaximumSizeLow */ 0,
/* dwMaximumSizeHigh */ 0,
/* lpName */ nullptr);
if (NewMmapHandle == nullptr)
{
throw std::system_error(std::error_code(::GetLastError(), std::system_category()),
fmt::format("CreateFileMapping failed on file '{}'", zen::PathFromHandle(m_FileHandle)));
}
NewFlags |= kOwnsMmap;
void* MappedBase = MapViewOfFile(NewMmapHandle,
/* dwDesiredAccess */ FILE_MAP_READ,
/* FileOffsetHigh */ uint32_t(MapOffset >> 32),
/* FileOffsetLow */ uint32_t(MapOffset & 0xffFFffFFu),
/* dwNumberOfBytesToMap */ MapSize);
#else
NewMmapHandle = (void*)uintptr_t(~MapSize); // ~ so it's never null (assuming MapSize >= 0)
NewFlags |= kOwnsMmap;
void* MappedBase = mmap(
/* addr */ nullptr,
/* length */ MapSize,
/* prot */ PROT_READ,
/* flags */ MAP_SHARED | MAP_NORESERVE,
/* fd */ int(uintptr_t(m_FileHandle)),
/* offset */ MapOffset);
#endif // ZEN_PLATFORM_WINDOWS
if (MappedBase == nullptr)
{
#if ZEN_PLATFORM_WINDOWS
CloseHandle(NewMmapHandle);
#endif // ZEN_PLATFORM_WINDOWS
throw std::system_error(std::error_code(zen::GetLastError(), std::system_category()),
fmt::format("MapViewOfFile failed (offset {:#x}, size {:#x}) file: '{}'",
MapOffset,
MapSize,
zen::PathFromHandle(m_FileHandle)));
}
m_MappedPointer = MappedBase;
m_DataPtr = reinterpret_cast<uint8_t*>(MappedBase) + MappedOffsetDisplacement;
m_MmapHandle = NewMmapHandle;
m_Flags.fetch_or(NewFlags, std::memory_order_release);
}
bool
IoBufferExtendedCore::GetFileReference(IoBufferFileReference& OutRef) const
{
if (m_FileHandle == nullptr)
{
return false;
}
OutRef.FileHandle = m_FileHandle;
OutRef.FileChunkOffset = m_FileOffset;
OutRef.FileChunkSize = m_DataBytes;
return true;
}
void
IoBufferExtendedCore::MarkAsDeleteOnClose()
{
m_DeleteOnClose = true;
}
//////////////////////////////////////////////////////////////////////////
IoBuffer::IoBuffer(size_t InSize) : m_Core(new IoBufferCore(InSize))
{
m_Core->SetIsImmutable(false);
}
IoBuffer::IoBuffer(size_t InSize, uint64_t InAlignment) : m_Core(new IoBufferCore(InSize, InAlignment))
{
m_Core->SetIsImmutable(false);
}
IoBuffer::IoBuffer(const IoBuffer& OuterBuffer, size_t Offset, size_t Size)
{
if (Size == ~(0ull))
{
Size = std::clamp<size_t>(Size, 0, OuterBuffer.Size() - Offset);
}
ZEN_ASSERT(Offset <= OuterBuffer.Size());
ZEN_ASSERT((Offset + Size) <= OuterBuffer.Size());
if (IoBufferExtendedCore* Extended = OuterBuffer.m_Core->ExtendedCore())
{
m_Core = new IoBufferExtendedCore(Extended, Offset, Size);
}
else
{
m_Core = new IoBufferCore(OuterBuffer.m_Core, reinterpret_cast<const uint8_t*>(OuterBuffer.Data()) + Offset, Size);
}
}
IoBuffer::IoBuffer(EFileTag, void* FileHandle, uint64_t ChunkFileOffset, uint64_t ChunkSize)
: m_Core(new IoBufferExtendedCore(FileHandle, ChunkFileOffset, ChunkSize, /* owned */ true))
{
}
IoBuffer::IoBuffer(EBorrowedFileTag, void* FileHandle, uint64_t ChunkFileOffset, uint64_t ChunkSize)
: m_Core(new IoBufferExtendedCore(FileHandle, ChunkFileOffset, ChunkSize, /* owned */ false))
{
}
bool
IoBuffer::GetFileReference(IoBufferFileReference& OutRef) const
{
if (IoBufferExtendedCore* ExtCore = m_Core->ExtendedCore())
{
if (ExtCore->GetFileReference(OutRef))
{
return true;
}
}
// Not a file reference
OutRef.FileHandle = 0;
OutRef.FileChunkOffset = ~0ull;
OutRef.FileChunkSize = 0;
return false;
}
void
IoBuffer::MarkAsDeleteOnClose()
{
if (IoBufferExtendedCore* ExtCore = m_Core->ExtendedCore())
{
ExtCore->MarkAsDeleteOnClose();
}
}
//////////////////////////////////////////////////////////////////////////
IoBuffer
IoBufferBuilder::ReadFromFileMaybe(IoBuffer& InBuffer)
{
IoBufferFileReference FileRef;
if (InBuffer.GetFileReference(/* out */ FileRef))
{
IoBuffer OutBuffer(FileRef.FileChunkSize);
#if ZEN_PLATFORM_WINDOWS
OVERLAPPED Ovl{};
const uint64_t NumberOfBytesToRead = FileRef.FileChunkSize;
const uint64_t& FileOffset = FileRef.FileChunkOffset;
Ovl.Offset = DWORD(FileOffset & 0xffff'ffffu);
Ovl.OffsetHigh = DWORD(FileOffset >> 32);
DWORD dwNumberOfBytesRead = 0;
BOOL Success = ::ReadFile(FileRef.FileHandle, OutBuffer.MutableData(), DWORD(NumberOfBytesToRead), &dwNumberOfBytesRead, &Ovl);
#else
int Fd = int(intptr_t(FileRef.FileHandle));
int Result = pread(Fd, OutBuffer.MutableData(), size_t(FileRef.FileChunkSize), off_t(FileRef.FileChunkOffset));
bool Success = (Result < 0);
uint32_t dwNumberOfBytesRead = uint32_t(Result);
#endif
if (!Success)
{
ThrowLastError("ReadFile failed in IoBufferBuilder::ReadFromFileMaybe");
}
ZEN_ASSERT(dwNumberOfBytesRead == FileRef.FileChunkSize);
return OutBuffer;
}
else
{
return InBuffer;
}
}
IoBuffer
IoBufferBuilder::MakeFromFileHandle(void* FileHandle, uint64_t Offset, uint64_t Size)
{
return IoBuffer(IoBuffer::BorrowedFile, FileHandle, Offset, Size);
}
static IoBuffer
MakeFromFileWithOptions(const std::filesystem::path& FileName, uint64_t Offset, uint64_t Size, bool UseShareDelete)
{
uint64_t FileSize;
#if ZEN_PLATFORM_WINDOWS
CAtlFile DataFile;
DWORD ShareOptions = FILE_SHARE_READ;
if (UseShareDelete)
{
ShareOptions |= FILE_SHARE_DELETE;
}
HRESULT hRes = DataFile.Create(FileName.c_str(), GENERIC_READ, FILE_SHARE_READ | ShareOptions, OPEN_EXISTING);
if (FAILED(hRes))
{
return {};
}
DataFile.GetSize((ULONGLONG&)FileSize);
#else
int Flags = O_RDONLY;
if (UseShareDelete)
{
Flags |= O_CLOEXEC;
}
int Fd = open(FileName.c_str(), Flags);
if (Fd < 0)
{
return {};
}
static_assert(sizeof(decltype(stat::st_size)) == sizeof(uint64_t), "fstat() doesn't support large files");
struct stat Stat;
fstat(Fd, &Stat);
FileSize = Stat.st_size;
#endif // ZEN_PLATFORM_WINDOWS
// TODO: should validate that offset is in range
if (Size == ~0ull)
{
Size = FileSize - Offset;
}
else
{
// Clamp size
if ((Offset + Size) > FileSize)
{
Size = FileSize - Offset;
}
}
if (Size)
{
#if ZEN_PLATFORM_WINDOWS
void* Fd = DataFile.Detach();
#endif
return IoBuffer(IoBuffer::File, (void*)uintptr_t(Fd), Offset, Size);
}
#if !ZEN_PLATFORM_WINDOWS
close(Fd);
#endif
// For an empty file, we may as well just return an empty memory IoBuffer
return IoBuffer(IoBuffer::Wrap, "", 0);
}
IoBuffer
IoBufferBuilder::MakeFromFileWithSharedDelete(const std::filesystem::path& FileName)
{
return MakeFromFileWithOptions(FileName, 0, ~0ull, true);
}
IoBuffer
IoBufferBuilder::MakeFromFile(const std::filesystem::path& FileName, uint64_t Offset, uint64_t Size)
{
return MakeFromFileWithOptions(FileName, Offset, Size, false);
}
IoBuffer
IoBufferBuilder::MakeFromTemporaryFile(const std::filesystem::path& FileName)
{
uint64_t FileSize;
void* Handle;
#if ZEN_PLATFORM_WINDOWS
CAtlFile DataFile;
// We need to open with DELETE since this is used for the case
// when a file has been written to a staging directory, and is going
// to be moved in place
HRESULT hRes = DataFile.Create(FileName.native().c_str(), GENERIC_READ | DELETE, FILE_SHARE_READ | FILE_SHARE_DELETE, OPEN_EXISTING);
if (FAILED(hRes))
{
return {};
}
DataFile.GetSize((ULONGLONG&)FileSize);
Handle = DataFile.Detach();
#else
int Fd = open(FileName.native().c_str(), O_RDONLY);
if (Fd < 0)
{
return {};
}
static_assert(sizeof(decltype(stat::st_size)) == sizeof(uint64_t), "fstat() doesn't support large files");
struct stat Stat;
fstat(Fd, &Stat);
FileSize = Stat.st_size;
Handle = (void*)uintptr_t(Fd);
#endif // ZEN_PLATFORM_WINDOWS
IoBuffer Iob(IoBuffer::File, Handle, 0, FileSize);
Iob.m_Core->SetIsWholeFile(true);
return Iob;
}
IoHash
HashBuffer(IoBuffer& Buffer)
{
// TODO: handle disk buffers with special path
return IoHash::HashBuffer(Buffer.Data(), Buffer.Size());
}
//////////////////////////////////////////////////////////////////////////
#if ZEN_WITH_TESTS
void
iobuffer_forcelink()
{
}
TEST_CASE("IoBuffer")
{
zen::IoBuffer buffer1;
zen::IoBuffer buffer2(16384);
zen::IoBuffer buffer3(buffer2, 0, buffer2.Size());
}
#endif
} // namespace zen
|