Stxxl  1.4.0
io/mmap_file.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *  io/mmap_file.cpp
00003  *
00004  *  Part of the STXXL. See http://stxxl.sourceforge.net
00005  *
00006  *  Copyright (C) 2002 Roman Dementiev <dementiev@mpi-sb.mpg.de>
00007  *  Copyright (C) 2008 Andreas Beckmann <beckmann@cs.uni-frankfurt.de>
00008  *
00009  *  Distributed under the Boost Software License, Version 1.0.
00010  *  (See accompanying file LICENSE_1_0.txt or copy at
00011  *  http://www.boost.org/LICENSE_1_0.txt)
00012  **************************************************************************/
00013 
00014 #include <stxxl/bits/io/mmap_file.h>
00015 
00016 #if STXXL_HAVE_MMAP_FILE
00017 
00018 #include <stxxl/bits/io/iostats.h>
00019 #include <stxxl/bits/common/error_handling.h>
00020 
00021 
00022 __STXXL_BEGIN_NAMESPACE
00023 
00024 
00025 void mmap_file::serve(const request * req) throw (io_error)
00026 {
00027     scoped_mutex_lock fd_lock(fd_mutex);
00028     assert(req->get_file() == this);
00029     offset_type offset = req->get_offset();
00030     void * buffer = req->get_buffer();
00031     size_type bytes = req->get_size();
00032     request::request_type type = req->get_type();
00033 
00034     stats::scoped_read_write_timer read_write_timer(bytes, type == request::WRITE);
00035 
00036     int prot = (type == request::READ) ? PROT_READ : PROT_WRITE;
00037     void * mem = mmap(NULL, bytes, prot, MAP_SHARED, file_des, offset);
00038     // void *mem = mmap (buffer, bytes, prot , MAP_SHARED|MAP_FIXED , file_des, offset);
00039     // STXXL_MSG("Mmaped to "<<mem<<" , buffer suggested at "<<buffer);
00040     if (mem == MAP_FAILED)
00041     {
00042         STXXL_THROW2(io_error,
00043                      " Mapping failed." <<
00044                      " path=" << filename <<
00045                      " bytes=" << bytes <<
00046                      " Page size: " << sysconf(_SC_PAGESIZE) <<
00047                      " offset modulo page size " << (offset % sysconf(_SC_PAGESIZE)));
00048     }
00049     else if (mem == 0)
00050     {
00051         stxxl_function_error(io_error);
00052     }
00053     else
00054     {
00055         if (type == request::READ)
00056         {
00057             memcpy(buffer, mem, bytes);
00058         }
00059         else
00060         {
00061             memcpy(mem, buffer, bytes);
00062         }
00063         stxxl_check_ge_0(munmap(mem, bytes), io_error);
00064     }
00065 }
00066 
00067 const char * mmap_file::io_type() const
00068 {
00069     return "mmap";
00070 }
00071 
00072 __STXXL_END_NAMESPACE
00073 
00074 #endif  // #if STXXL_HAVE_MMAP_FILE
00075 // vim: et:ts=4:sw=4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines