[go: up one dir, main page]

Menu

[b40542]: / io / mmap_file.cpp  Maximize  Restore  History

Download this file

72 lines (59 with data), 2.0 kB

 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
71
/***************************************************************************
* io/mmap_file.cpp
*
* Part of the STXXL. See http://stxxl.sourceforge.net
*
* Copyright (C) 2002 Roman Dementiev <dementiev@mpi-sb.mpg.de>
*
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
**************************************************************************/
#include <stxxl/bits/io/mmap_file.h>
#if STXXL_HAVE_MMAP_FILE
#include <stxxl/bits/io/request_impl_basic.h>
__STXXL_BEGIN_NAMESPACE
void mmap_file::serve(const request * req) throw (io_error)
{
scoped_mutex_lock fd_lock(fd_mutex);
assert(req->get_file() == this);
offset_type offset = req->get_offset();
void * buffer = req->get_buffer();
size_type bytes = req->get_size();
request::request_type type = req->get_type();
stats::scoped_read_write_timer read_write_timer(bytes, type == request::WRITE);
int prot = (type == request::READ) ? PROT_READ : PROT_WRITE;
void * mem = mmap(NULL, bytes, prot, MAP_SHARED, file_des, offset);
// void *mem = mmap (buffer, bytes, prot , MAP_SHARED|MAP_FIXED , file_des, offset);
// STXXL_MSG("Mmaped to "<<mem<<" , buffer suggested at "<<buffer);
if (mem == MAP_FAILED)
{
STXXL_THROW2(io_error,
" Mapping failed." <<
" Page size: " << sysconf(_SC_PAGESIZE) <<
" offset modulo page size " << (offset % sysconf(_SC_PAGESIZE)));
}
else if (mem == 0)
{
stxxl_function_error(io_error);
}
else
{
if (type == request::READ)
{
memcpy(buffer, mem, bytes);
}
else
{
memcpy(mem, buffer, bytes);
}
stxxl_check_ge_0(munmap(mem, bytes), io_error);
}
}
const char * mmap_file::io_type() const
{
return "mmap";
}
__STXXL_END_NAMESPACE
#endif // #if STXXL_HAVE_MMAP_FILE
// vim: et:ts=4:sw=4