X-Git-Url: https://git.saurik.com/apple/system_cmds.git/blobdiff_plain/1a7e3f61d38d679bba59130891c2031b5a0092b6..bd6521f0fc816ab056bc71376f9706a69b3b52c1:/CPPUtil/UtilMappedFile.cpp diff --git a/CPPUtil/UtilMappedFile.cpp b/CPPUtil/UtilMappedFile.cpp new file mode 100644 index 0000000..acc71bd --- /dev/null +++ b/CPPUtil/UtilMappedFile.cpp @@ -0,0 +1,57 @@ +// +// UtilMappedFile.cpp +// CPPUtil +// +// Created by James McIlree on 4/19/13. +// Copyright (c) 2013 Apple. All rights reserved. +// + +#include "CPPUtil.h" + +#include + +BEGIN_UTIL_NAMESPACE + +static int open_fd(const char* path, size_t& file_size) +{ + int fd = open(path, O_RDONLY, 0); + if(fd >= 0) { + struct stat data; + if (fstat(fd, &data) == 0) { + if (S_ISREG(data.st_mode)) { + // Is it zero sized? + if (data.st_size > 0) { + file_size = (size_t)data.st_size; + return fd; + } + } + } + close(fd); + } + + return -1; +} + +MappedFile::MappedFile(const char* path) : + _address(NULL), + _size(0) +{ + ASSERT(path, "Sanity"); + int fd = open_fd(path, _size); + if (fd >= 0) { + _address = (unsigned char*)mmap(NULL, _size, PROT_READ, MAP_FILE | MAP_SHARED, fd, 0); + if (_address == (void*)-1) { + _address = NULL; + } + close(fd); + } +} + +MappedFile::~MappedFile() +{ + if (_address != NULL) { + munmap(_address, _size); + } +} + +END_UTIL_NAMESPACE