Mappable.cpp (950B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 #include <fcntl.h> 6 #include <sys/stat.h> 7 8 #include "Mappable.h" 9 10 Mappable* Mappable::Create(const char* path) { 11 int fd = open(path, O_RDONLY); 12 if (fd != -1) return new Mappable(fd); 13 return nullptr; 14 } 15 16 MemoryRange Mappable::mmap(const void* addr, size_t length, int prot, int flags, 17 off_t offset) { 18 MOZ_ASSERT(fd && *fd != -1); 19 MOZ_ASSERT(!(flags & MAP_SHARED)); 20 flags |= MAP_PRIVATE; 21 22 return MemoryRange::mmap(const_cast<void*>(addr), length, prot, flags, *fd, 23 offset); 24 } 25 26 void Mappable::finalize() { 27 /* Close file ; equivalent to close(fd.forget()) */ 28 fd.emplace(-1); 29 } 30 31 size_t Mappable::GetLength() const { 32 struct stat st; 33 return fstat(*fd, &st) ? 0 : st.st_size; 34 }