Bug 1857841 - pt 3. Add a new page kind named "fresh" r=glandium
[gecko.git] / mozglue / linker / Mappable.cpp
blob55a7aadfcc0eebf21ca3e8fc0aee717145ba48ef
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/. */
5 #include <fcntl.h>
6 #include <sys/stat.h>
8 #include "Mappable.h"
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;
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;
22 return MemoryRange::mmap(const_cast<void*>(addr), length, prot, flags, *fd,
23 offset);
26 void Mappable::finalize() {
27 /* Close file ; equivalent to close(fd.forget()) */
28 fd.emplace(-1);
31 size_t Mappable::GetLength() const {
32 struct stat st;
33 return fstat(*fd, &st) ? 0 : st.st_size;