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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
|
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/util/windows_compatibility.h" // IWYU pragma: keep
// sys/mman.h not present in Visual Studio or Cygwin
#ifdef _WIN32
# ifndef NOMINMAX
# define NOMINMAX
# endif
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/io/mman.h"
# undef Realloc
# undef Free
#else
# include <fcntl.h>
# include <sys/mman.h>
# include <unistd.h> // IWYU pragma: keep
#endif
#include <algorithm>
#include <atomic>
#include <cerrno>
#include <cstdint>
#include <cstring>
#include <limits>
#include <memory>
#include <mutex>
#include <sstream>
#include <string>
#include <utility>
// ----------------------------------------------------------------------
// Other Arrow includes
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/io/file.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/io/interfaces.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/io/util_internal.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/buffer.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/memory_pool.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/status.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/util/future.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/util/io_util.h"
#include "contrib/libs/apache/arrow_next/cpp/src/arrow/util/logging.h"
namespace arrow20 {
using internal::FileDescriptor;
using internal::IOErrorFromErrno;
namespace io {
class OSFile {
public:
// Note: only one of the Open* methods below may be called on a given instance
Status OpenWritable(const std::string& path, bool truncate, bool append,
bool write_only) {
RETURN_NOT_OK(SetFileName(path));
ARROW_ASSIGN_OR_RAISE(fd_, ::arrow20::internal::FileOpenWritable(file_name_, write_only,
truncate, append));
mode_ = write_only ? FileMode::WRITE : FileMode::READWRITE;
if (!truncate) {
ARROW_ASSIGN_OR_RAISE(size_, ::arrow20::internal::FileGetSize(fd_.fd()));
} else {
size_ = 0;
}
return Status::OK();
}
// This is different from OpenWritable(string, ...) in that it doesn't
// truncate nor mandate a seekable file
Status OpenWritable(int fd) {
auto result = ::arrow20::internal::FileGetSize(fd);
if (result.ok()) {
size_ = *result;
} else {
// Non-seekable file
size_ = -1;
}
RETURN_NOT_OK(SetFileName(fd));
mode_ = FileMode::WRITE;
fd_ = FileDescriptor(fd);
return Status::OK();
}
Status OpenReadable(const std::string& path) {
RETURN_NOT_OK(SetFileName(path));
ARROW_ASSIGN_OR_RAISE(fd_, ::arrow20::internal::FileOpenReadable(file_name_));
ARROW_ASSIGN_OR_RAISE(size_, ::arrow20::internal::FileGetSize(fd_.fd()));
mode_ = FileMode::READ;
return Status::OK();
}
Status OpenReadable(int fd) {
ARROW_ASSIGN_OR_RAISE(size_, ::arrow20::internal::FileGetSize(fd));
RETURN_NOT_OK(SetFileName(fd));
mode_ = FileMode::READ;
fd_ = FileDescriptor(fd);
return Status::OK();
}
Status CheckClosed() const {
if (fd_.closed()) {
return Status::Invalid("Invalid operation on closed file");
}
return Status::OK();
}
Status Close() { return fd_.Close(); }
Result<int64_t> Read(int64_t nbytes, void* out) {
RETURN_NOT_OK(CheckClosed());
RETURN_NOT_OK(CheckPositioned());
return ::arrow20::internal::FileRead(fd_.fd(), reinterpret_cast<uint8_t*>(out), nbytes);
}
Result<int64_t> ReadAt(int64_t position, int64_t nbytes, void* out) {
RETURN_NOT_OK(CheckClosed());
RETURN_NOT_OK(internal::ValidateRange(position, nbytes));
// ReadAt() leaves the file position undefined, so require that we seek
// before calling Read() or Write().
need_seeking_.store(true);
return ::arrow20::internal::FileReadAt(fd_.fd(), reinterpret_cast<uint8_t*>(out),
position, nbytes);
}
Status Seek(int64_t pos) {
RETURN_NOT_OK(CheckClosed());
if (pos < 0) {
return Status::Invalid("Invalid position");
}
Status st = ::arrow20::internal::FileSeek(fd_.fd(), pos);
if (st.ok()) {
need_seeking_.store(false);
}
return st;
}
Result<int64_t> Tell() const {
RETURN_NOT_OK(CheckClosed());
return ::arrow20::internal::FileTell(fd_.fd());
}
Status Write(const void* data, int64_t length) {
RETURN_NOT_OK(CheckClosed());
std::lock_guard<std::mutex> guard(lock_);
RETURN_NOT_OK(CheckPositioned());
if (length < 0) {
return Status::IOError("Length must be non-negative");
}
return ::arrow20::internal::FileWrite(fd_.fd(), reinterpret_cast<const uint8_t*>(data),
length);
}
int fd() const { return fd_.fd(); }
bool is_open() const { return !fd_.closed(); }
int64_t size() const { return size_; }
FileMode::type mode() const { return mode_; }
std::mutex& lock() { return lock_; }
protected:
Status SetFileName(const std::string& file_name) {
return ::arrow20::internal::PlatformFilename::FromString(file_name).Value(&file_name_);
}
Status SetFileName(int fd) {
std::stringstream ss;
ss << "<fd " << fd << ">";
return SetFileName(ss.str());
}
Status CheckPositioned() {
if (need_seeking_.load()) {
return Status::Invalid(
"Need seeking after ReadAt() before "
"calling implicitly-positioned operation");
}
return Status::OK();
}
::arrow20::internal::PlatformFilename file_name_;
std::mutex lock_;
FileDescriptor fd_;
FileMode::type mode_;
int64_t size_{-1};
// Whether ReadAt made the file position non-deterministic.
std::atomic<bool> need_seeking_{false};
};
// ----------------------------------------------------------------------
// ReadableFile implementation
class ReadableFile::ReadableFileImpl : public OSFile {
public:
explicit ReadableFileImpl(MemoryPool* pool) : OSFile(), pool_(pool) {}
Status Open(const std::string& path) { return OpenReadable(path); }
Status Open(int fd) { return OpenReadable(fd); }
Result<std::shared_ptr<Buffer>> ReadBuffer(int64_t nbytes) {
ARROW_ASSIGN_OR_RAISE(auto buffer, AllocateResizableBuffer(nbytes, pool_));
ARROW_ASSIGN_OR_RAISE(int64_t bytes_read, Read(nbytes, buffer->mutable_data()));
if (bytes_read < nbytes) {
RETURN_NOT_OK(buffer->Resize(bytes_read));
buffer->ZeroPadding();
}
// R build with openSUSE155 requires an explicit shared_ptr construction
return std::shared_ptr<Buffer>(std::move(buffer));
}
Result<std::shared_ptr<Buffer>> ReadBufferAt(int64_t position, int64_t nbytes) {
ARROW_ASSIGN_OR_RAISE(auto buffer, AllocateResizableBuffer(nbytes, pool_));
ARROW_ASSIGN_OR_RAISE(int64_t bytes_read,
ReadAt(position, nbytes, buffer->mutable_data()));
if (bytes_read < nbytes) {
RETURN_NOT_OK(buffer->Resize(bytes_read));
buffer->ZeroPadding();
}
// R build with openSUSE155 requires an explicit shared_ptr construction
return std::shared_ptr<Buffer>(std::move(buffer));
}
Status WillNeed(const std::vector<ReadRange>& ranges) {
auto report_error = [](int errnum, const char* msg) -> Status {
if (errnum == EBADF || errnum == EINVAL) {
// These are logic errors, so raise them
return IOErrorFromErrno(errnum, msg);
}
#ifndef NDEBUG
// Other errors may be encountered if the target device or filesystem
// does not support fadvise advisory (for example, macOS can return
// ENOTTY on macOS: ARROW-13983). Log the error for diagnosis
// on debug builds, but avoid bothering the user otherwise.
ARROW_LOG(WARNING) << IOErrorFromErrno(errnum, msg).ToString();
#else
ARROW_UNUSED(msg);
#endif
return Status::OK();
};
RETURN_NOT_OK(CheckClosed());
for (const auto& range : ranges) {
RETURN_NOT_OK(internal::ValidateRange(range.offset, range.length));
#if defined(POSIX_FADV_WILLNEED)
int ret = posix_fadvise(fd_.fd(), range.offset, range.length, POSIX_FADV_WILLNEED);
if (ret) {
RETURN_NOT_OK(report_error(ret, "posix_fadvise failed"));
}
#elif defined(F_RDADVISE) // macOS, BSD?
struct {
off_t ra_offset;
int ra_count;
} radvisory{range.offset, static_cast<int>(range.length)};
if (radvisory.ra_count > 0 && fcntl(fd_.fd(), F_RDADVISE, &radvisory) == -1) {
RETURN_NOT_OK(report_error(errno, "fcntl(fd, F_RDADVISE, ...) failed"));
}
#else
ARROW_UNUSED(report_error);
#endif
}
return Status::OK();
}
private:
MemoryPool* pool_;
};
ReadableFile::ReadableFile(MemoryPool* pool) { impl_.reset(new ReadableFileImpl(pool)); }
ReadableFile::~ReadableFile() { internal::CloseFromDestructor(this); }
Result<std::shared_ptr<ReadableFile>> ReadableFile::Open(const std::string& path,
MemoryPool* pool) {
auto file = std::shared_ptr<ReadableFile>(new ReadableFile(pool));
RETURN_NOT_OK(file->impl_->Open(path));
return file;
}
Result<std::shared_ptr<ReadableFile>> ReadableFile::Open(int fd, MemoryPool* pool) {
auto file = std::shared_ptr<ReadableFile>(new ReadableFile(pool));
RETURN_NOT_OK(file->impl_->Open(fd));
return file;
}
Status ReadableFile::DoClose() { return impl_->Close(); }
bool ReadableFile::closed() const { return !impl_->is_open(); }
Status ReadableFile::WillNeed(const std::vector<ReadRange>& ranges) {
return impl_->WillNeed(ranges);
}
Result<int64_t> ReadableFile::DoTell() const { return impl_->Tell(); }
Result<int64_t> ReadableFile::DoRead(int64_t nbytes, void* out) {
return impl_->Read(nbytes, out);
}
Result<int64_t> ReadableFile::DoReadAt(int64_t position, int64_t nbytes, void* out) {
return impl_->ReadAt(position, nbytes, out);
}
Result<std::shared_ptr<Buffer>> ReadableFile::DoReadAt(int64_t position, int64_t nbytes) {
return impl_->ReadBufferAt(position, nbytes);
}
Result<std::shared_ptr<Buffer>> ReadableFile::DoRead(int64_t nbytes) {
return impl_->ReadBuffer(nbytes);
}
Result<int64_t> ReadableFile::DoGetSize() { return impl_->size(); }
Status ReadableFile::DoSeek(int64_t pos) { return impl_->Seek(pos); }
int ReadableFile::file_descriptor() const { return impl_->fd(); }
// ----------------------------------------------------------------------
// FileOutputStream
class FileOutputStream::FileOutputStreamImpl : public OSFile {
public:
Status Open(const std::string& path, bool append) {
const bool truncate = !append;
return OpenWritable(path, truncate, append, true /* write_only */);
}
Status Open(int fd) { return OpenWritable(fd); }
};
FileOutputStream::FileOutputStream() { impl_.reset(new FileOutputStreamImpl()); }
FileOutputStream::~FileOutputStream() { internal::CloseFromDestructor(this); }
Result<std::shared_ptr<FileOutputStream>> FileOutputStream::Open(const std::string& path,
bool append) {
auto stream = std::shared_ptr<FileOutputStream>(new FileOutputStream());
RETURN_NOT_OK(stream->impl_->Open(path, append));
return stream;
}
Result<std::shared_ptr<FileOutputStream>> FileOutputStream::Open(int fd) {
auto stream = std::shared_ptr<FileOutputStream>(new FileOutputStream());
RETURN_NOT_OK(stream->impl_->Open(fd));
return stream;
}
Status FileOutputStream::Close() { return impl_->Close(); }
bool FileOutputStream::closed() const { return !impl_->is_open(); }
Result<int64_t> FileOutputStream::Tell() const { return impl_->Tell(); }
Status FileOutputStream::Write(const void* data, int64_t length) {
return impl_->Write(data, length);
}
int FileOutputStream::file_descriptor() const { return impl_->fd(); }
// ----------------------------------------------------------------------
// Implement MemoryMappedFile
class MemoryMappedFile::MemoryMap
: public std::enable_shared_from_this<MemoryMappedFile::MemoryMap> {
public:
// An object representing the entire memory-mapped region.
// It can be sliced in order to return individual subregions, which
// will then keep the original region alive as long as necessary.
class Region : public Buffer {
public:
Region(std::shared_ptr<MemoryMappedFile::MemoryMap> memory_map, uint8_t* data,
int64_t size)
: Buffer(data, size) {
is_mutable_ = memory_map->writable();
}
~Region() {
if (data_ != nullptr) {
#ifndef __EMSCRIPTEN__
int result = munmap(data(), static_cast<size_t>(size_));
// emscripten erroneously reports failures in munmap
// https://github.com/emscripten-core/emscripten/issues/20459
ARROW_CHECK_EQ(result, 0) << "munmap failed";
#else
munmap(data(), static_cast<size_t>(size_));
#endif
}
}
// For convenience
uint8_t* data() { return const_cast<uint8_t*>(data_); }
void Detach() { data_ = nullptr; }
};
MemoryMap() : file_size_(0), map_len_(0) {}
~MemoryMap() { ARROW_CHECK_OK(Close()); }
Status Close() {
if (file_->is_open()) {
// Lose our reference to the MemoryMappedRegion, so that munmap()
// is called as soon as all buffer exports are released.
region_.reset();
return file_->Close();
} else {
return Status::OK();
}
}
bool closed() const { return !file_->is_open(); }
Status CheckClosed() const {
if (closed()) {
return Status::Invalid("Invalid operation on closed file");
}
return Status::OK();
}
Status Open(const std::string& path, FileMode::type mode, const int64_t offset = 0,
const int64_t length = -1) {
file_ = std::make_unique<OSFile>();
if (mode != FileMode::READ) {
// Memory mapping has permission failures if PROT_READ not set
prot_flags_ = PROT_READ | PROT_WRITE;
map_mode_ = MAP_SHARED;
constexpr bool append = false;
constexpr bool truncate = false;
constexpr bool write_only = false;
RETURN_NOT_OK(file_->OpenWritable(path, truncate, append, write_only));
} else {
prot_flags_ = PROT_READ;
map_mode_ = MAP_PRIVATE; // Changes are not to be committed back to the file
RETURN_NOT_OK(file_->OpenReadable(path));
}
map_len_ = offset_ = 0;
// Memory mapping fails when file size is 0
// delay it until the first resize
if (file_->size() > 0) {
RETURN_NOT_OK(InitMMap(file_->size(), false, offset, length));
}
position_ = 0;
return Status::OK();
}
// Resize the mmap and file to the specified size.
// Resize on memory mapped file region is not supported.
Status Resize(const int64_t new_size) {
if (!writable()) {
return Status::IOError("Cannot resize a readonly memory map");
}
if (map_len_ != file_size_) {
return Status::IOError("Cannot resize a partial memory map");
}
if (region_.use_count() > 1) {
// There are buffer exports currently, the MemoryMapRemap() call
// would make the buffers invalid
return Status::IOError("Cannot resize memory map while there are active readers");
}
if (new_size == 0) {
if (map_len_ > 0) {
// Just unmap the mmap and truncate the file to 0 size
region_.reset();
RETURN_NOT_OK(::arrow20::internal::FileTruncate(file_->fd(), 0));
map_len_ = offset_ = file_size_ = 0;
}
position_ = 0;
return Status::OK();
}
if (map_len_ > 0) {
void* result;
auto data = region_->data();
RETURN_NOT_OK(::arrow20::internal::MemoryMapRemap(data, map_len_, new_size,
file_->fd(), &result));
region_->Detach(); // avoid munmap() on destruction
region_ = std::make_shared<Region>(shared_from_this(),
static_cast<uint8_t*>(result), new_size);
map_len_ = file_size_ = new_size;
offset_ = 0;
if (position_ > map_len_) {
position_ = map_len_;
}
} else {
DCHECK_EQ(position_, 0);
// the mmap is not yet initialized, resize the underlying
// file, since it might have been 0-sized
RETURN_NOT_OK(InitMMap(new_size, /*resize_file*/ true));
}
return Status::OK();
}
Status Seek(int64_t position) {
if (position < 0) {
return Status::Invalid("position is out of bounds");
}
position_ = position;
return Status::OK();
}
Result<std::shared_ptr<Buffer>> Slice(int64_t offset, int64_t length) {
length = std::max<int64_t>(0, std::min(length, map_len_ - offset));
if (length > 0) {
DCHECK_NE(region_, nullptr);
return SliceBuffer(region_, offset, length);
} else {
return std::make_shared<Buffer>(nullptr, 0);
}
}
// map_len_ == file_size_ if memory mapping on the whole file
int64_t size() const { return map_len_; }
int64_t position() { return position_; }
void advance(int64_t nbytes) { position_ = position_ + nbytes; }
uint8_t* data() { return region_ ? region_->data() : nullptr; }
uint8_t* head() { return data() + position_; }
bool writable() { return file_->mode() != FileMode::READ; }
bool opened() { return file_->is_open(); }
int fd() const { return file_->fd(); }
std::mutex& write_lock() { return file_->lock(); }
std::mutex& resize_lock() { return resize_lock_; }
private:
// Initialize the mmap and set size, capacity and the data pointers
Status InitMMap(int64_t initial_size, bool resize_file = false,
const int64_t offset = 0, const int64_t length = -1) {
DCHECK(!region_);
if (resize_file) {
RETURN_NOT_OK(::arrow20::internal::FileTruncate(file_->fd(), initial_size));
}
int64_t mmap_length = initial_size;
if (length >= 0) {
// memory mapping a file region
if (length > initial_size) {
return Status::Invalid("mapping length is beyond file size");
}
mmap_length = length;
}
if (static_cast<int64_t>(static_cast<size_t>(mmap_length)) != mmap_length) {
return Status::CapacityError("Requested memory map length ", mmap_length,
" does not fit in a C size_t "
"(are you using a 32-bit build of Arrow?)");
}
void* result = mmap(nullptr, static_cast<size_t>(mmap_length), prot_flags_, map_mode_,
file_->fd(), static_cast<off_t>(offset));
if (result == MAP_FAILED) {
return Status::IOError("Memory mapping file failed: ",
::arrow20::internal::ErrnoMessage(errno));
}
map_len_ = mmap_length;
offset_ = offset;
region_ = std::make_shared<Region>(shared_from_this(), static_cast<uint8_t*>(result),
map_len_);
file_size_ = initial_size;
return Status::OK();
}
std::unique_ptr<OSFile> file_;
int prot_flags_;
int map_mode_;
std::shared_ptr<Region> region_;
int64_t file_size_;
int64_t position_;
int64_t offset_;
int64_t map_len_;
std::mutex resize_lock_;
};
MemoryMappedFile::MemoryMappedFile() {}
MemoryMappedFile::~MemoryMappedFile() { internal::CloseFromDestructor(this); }
Result<std::shared_ptr<MemoryMappedFile>> MemoryMappedFile::Create(
const std::string& path, int64_t size) {
ARROW_ASSIGN_OR_RAISE(auto file, FileOutputStream::Open(path));
RETURN_NOT_OK(::arrow20::internal::FileTruncate(file->file_descriptor(), size));
RETURN_NOT_OK(file->Close());
return MemoryMappedFile::Open(path, FileMode::READWRITE);
}
Result<std::shared_ptr<MemoryMappedFile>> MemoryMappedFile::Open(const std::string& path,
FileMode::type mode) {
std::shared_ptr<MemoryMappedFile> result(new MemoryMappedFile());
result->memory_map_.reset(new MemoryMap());
RETURN_NOT_OK(result->memory_map_->Open(path, mode));
return result;
}
Result<std::shared_ptr<MemoryMappedFile>> MemoryMappedFile::Open(const std::string& path,
FileMode::type mode,
const int64_t offset,
const int64_t length) {
std::shared_ptr<MemoryMappedFile> result(new MemoryMappedFile());
result->memory_map_.reset(new MemoryMap());
RETURN_NOT_OK(result->memory_map_->Open(path, mode, offset, length));
return result;
}
Result<int64_t> MemoryMappedFile::GetSize() {
RETURN_NOT_OK(memory_map_->CheckClosed());
return memory_map_->size();
}
Result<int64_t> MemoryMappedFile::Tell() const {
RETURN_NOT_OK(memory_map_->CheckClosed());
return memory_map_->position();
}
Status MemoryMappedFile::Seek(int64_t position) {
RETURN_NOT_OK(memory_map_->CheckClosed());
return memory_map_->Seek(position);
}
Status MemoryMappedFile::Close() { return memory_map_->Close(); }
bool MemoryMappedFile::closed() const { return memory_map_->closed(); }
Result<std::shared_ptr<Buffer>> MemoryMappedFile::ReadAt(int64_t position,
int64_t nbytes) {
RETURN_NOT_OK(memory_map_->CheckClosed());
// if the file is writable, we acquire the lock before creating any slices
// in case a resize is triggered concurrently, otherwise we wouldn't detect
// a change in the use count
auto guard_resize = memory_map_->writable()
? std::unique_lock<std::mutex>(memory_map_->resize_lock())
: std::unique_lock<std::mutex>();
ARROW_ASSIGN_OR_RAISE(
nbytes, internal::ValidateReadRange(position, nbytes, memory_map_->size()));
// Arrange to page data in
RETURN_NOT_OK(::arrow20::internal::MemoryAdviseWillNeed(
{{memory_map_->data() + position, static_cast<size_t>(nbytes)}}));
return memory_map_->Slice(position, nbytes);
}
Result<int64_t> MemoryMappedFile::ReadAt(int64_t position, int64_t nbytes, void* out) {
RETURN_NOT_OK(memory_map_->CheckClosed());
auto guard_resize = memory_map_->writable()
? std::unique_lock<std::mutex>(memory_map_->resize_lock())
: std::unique_lock<std::mutex>();
ARROW_ASSIGN_OR_RAISE(
nbytes, internal::ValidateReadRange(position, nbytes, memory_map_->size()));
if (nbytes > 0) {
memcpy(out, memory_map_->data() + position, static_cast<size_t>(nbytes));
}
return nbytes;
}
Result<int64_t> MemoryMappedFile::Read(int64_t nbytes, void* out) {
RETURN_NOT_OK(memory_map_->CheckClosed());
ARROW_ASSIGN_OR_RAISE(int64_t bytes_read, ReadAt(memory_map_->position(), nbytes, out));
memory_map_->advance(bytes_read);
return bytes_read;
}
Result<std::shared_ptr<Buffer>> MemoryMappedFile::Read(int64_t nbytes) {
RETURN_NOT_OK(memory_map_->CheckClosed());
ARROW_ASSIGN_OR_RAISE(auto buffer, ReadAt(memory_map_->position(), nbytes));
memory_map_->advance(buffer->size());
return buffer;
}
Future<std::shared_ptr<Buffer>> MemoryMappedFile::ReadAsync(const IOContext&,
int64_t position,
int64_t nbytes) {
return Future<std::shared_ptr<Buffer>>::MakeFinished(ReadAt(position, nbytes));
}
Status MemoryMappedFile::WillNeed(const std::vector<ReadRange>& ranges) {
using ::arrow20::internal::MemoryRegion;
RETURN_NOT_OK(memory_map_->CheckClosed());
auto guard_resize = memory_map_->writable()
? std::unique_lock<std::mutex>(memory_map_->resize_lock())
: std::unique_lock<std::mutex>();
std::vector<MemoryRegion> regions(ranges.size());
for (size_t i = 0; i < ranges.size(); ++i) {
const auto& range = ranges[i];
ARROW_ASSIGN_OR_RAISE(
auto size,
internal::ValidateReadRange(range.offset, range.length, memory_map_->size()));
DCHECK_NE(memory_map_->data(), nullptr);
regions[i] = {const_cast<uint8_t*>(memory_map_->data() + range.offset),
static_cast<size_t>(size)};
}
return ::arrow20::internal::MemoryAdviseWillNeed(regions);
}
bool MemoryMappedFile::supports_zero_copy() const { return true; }
Status MemoryMappedFile::WriteAt(int64_t position, const void* data, int64_t nbytes) {
RETURN_NOT_OK(memory_map_->CheckClosed());
std::lock_guard<std::mutex> guard(memory_map_->write_lock());
if (!memory_map_->opened() || !memory_map_->writable()) {
return Status::IOError("Unable to write");
}
RETURN_NOT_OK(internal::ValidateWriteRange(position, nbytes, memory_map_->size()));
RETURN_NOT_OK(memory_map_->Seek(position));
return WriteInternal(data, nbytes);
}
Status MemoryMappedFile::Write(const void* data, int64_t nbytes) {
RETURN_NOT_OK(memory_map_->CheckClosed());
std::lock_guard<std::mutex> guard(memory_map_->write_lock());
if (!memory_map_->opened() || !memory_map_->writable()) {
return Status::IOError("Unable to write");
}
RETURN_NOT_OK(
internal::ValidateWriteRange(memory_map_->position(), nbytes, memory_map_->size()));
return WriteInternal(data, nbytes);
}
Status MemoryMappedFile::WriteInternal(const void* data, int64_t nbytes) {
memcpy(memory_map_->head(), data, static_cast<size_t>(nbytes));
memory_map_->advance(nbytes);
return Status::OK();
}
Status MemoryMappedFile::Resize(int64_t new_size) {
RETURN_NOT_OK(memory_map_->CheckClosed());
std::unique_lock<std::mutex> write_guard(memory_map_->write_lock(), std::defer_lock);
std::unique_lock<std::mutex> resize_guard(memory_map_->resize_lock(), std::defer_lock);
std::lock(write_guard, resize_guard);
RETURN_NOT_OK(memory_map_->Resize(new_size));
return Status::OK();
}
int MemoryMappedFile::file_descriptor() const { return memory_map_->fd(); }
} // namespace io
} // namespace arrow20
|