-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapping.cc
70 lines (63 loc) · 1.93 KB
/
mapping.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
* This is port of OpenHttpStreamer for win32
* copyright (c) 2011 andrew.kondratovich@gmail.com
*
* Originally:
* copyright (c) 2010 ZAO Inventos (inventos.ru)
* copyright (c) 2010 jk@inventos.ru
* copyright (c) 2010 kuzalex@inventos.ru
* copyright (c) 2010 artint@inventos.ru
*
* This file is part of mp4frag.
*
* mp4grag is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* mp4frag is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
#include "mapping.hh"
#include "mman.h"
#include <boost/system/system_error.hpp>
#include <boost/system/error_code.hpp>
#include <memory.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
#include <errno.h>
using namespace boost::system;
void Mapping::make_mapping(int fd) {
struct stat st;
memset(&st, 0, sizeof(st));
if ( fstat(fd, &st) == -1 ) {
throw system_error(errno, get_system_category(), "fstat");
}
_size = st.st_size;
_data = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
if ( _data == (void *)(-1) ) {
throw system_error(errno, get_system_category(), "mmap");
}
}
void Mapping::make_mapping(const char *name) {
int fd = _open(name, O_RDONLY);
if ( fd == -1 ) {
throw system_error(errno, get_system_category(), std::string("opening ") + name);
}
struct fd_guard {
int _fd;
fd_guard(int f) : _fd(f) {}
~fd_guard() { _close(_fd); }
} guard(fd);
make_mapping(fd);
}
Mapping::~Mapping() {
if ( _data != (void*)-1 ) {
munmap((void *)_data, _size);
}
}