]>
git.saurik.com Git - apple/system_cmds.git/blob - CPPUtil/UtilMappedFile.cpp
acc71bd6929ea70a2262fb625e4973b7111e3e9e
5 // Created by James McIlree on 4/19/13.
6 // Copyright (c) 2013 Apple. All rights reserved.
15 static int open_fd(const char* path
, size_t& file_size
)
17 int fd
= open(path
, O_RDONLY
, 0);
20 if (fstat(fd
, &data
) == 0) {
21 if (S_ISREG(data
.st_mode
)) {
23 if (data
.st_size
> 0) {
24 file_size
= (size_t)data
.st_size
;
35 MappedFile::MappedFile(const char* path
) :
39 ASSERT(path
, "Sanity");
40 int fd
= open_fd(path
, _size
);
42 _address
= (unsigned char*)mmap(NULL
, _size
, PROT_READ
, MAP_FILE
| MAP_SHARED
, fd
, 0);
43 if (_address
== (void*)-1) {
50 MappedFile::~MappedFile()
52 if (_address
!= NULL
) {
53 munmap(_address
, _size
);