| 25 | namespace tflite { |
| 26 | |
| 27 | MMAPAllocation::MMAPAllocation(const char* filename, |
| 28 | ErrorReporter* error_reporter) |
| 29 | : Allocation(error_reporter, Allocation::Type::kMMap), |
| 30 | mmapped_buffer_(MAP_FAILED) { |
| 31 | mmap_fd_ = open(filename, O_RDONLY); |
| 32 | if (mmap_fd_ == -1) { |
| 33 | error_reporter_->Report("Could not open '%s'.", filename); |
| 34 | return; |
| 35 | } |
| 36 | struct stat sb; |
| 37 | fstat(mmap_fd_, &sb); |
| 38 | buffer_size_bytes_ = sb.st_size; |
| 39 | mmapped_buffer_ = |
| 40 | mmap(nullptr, buffer_size_bytes_, PROT_READ, MAP_SHARED, mmap_fd_, 0); |
| 41 | if (mmapped_buffer_ == MAP_FAILED) { |
| 42 | error_reporter_->Report("Mmap of '%s' failed.", filename); |
| 43 | return; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | MMAPAllocation::~MMAPAllocation() { |
| 48 | if (valid()) { |