Example #1
0
MMap::MMap (const FileDescriptor& fd, bool const sequential)
    :
    size   (fd.size()),
    ptr    (mmap (NULL, size, PROT_READ|PROT_WRITE,
                 MAP_SHARED|MAP_NORESERVE, fd.get(), 0)),
    mapped (ptr != GU_MAP_FAILED)
{
    if (!mapped)
    {
        gu_throw_error(errno) << "mmap() on '" << fd.name()
                              << "' failed";
    }

#if !defined(__sun__) && !defined(__APPLE__) && !defined(__FreeBSD__)
    /* Solaris, Darwin, and FreeBSD do not have MADV_DONTFORK */
    if (posix_madvise (ptr, size, MADV_DONTFORK))
    {
        int const err(errno);
        log_warn << "Failed to set MADV_DONTFORK on " << fd.name()
                 << ": " << err << " (" << strerror(err) << ")";
    }
#endif

    /* benefits are questionable */
    if (sequential && posix_madvise (ptr, size, MADV_SEQUENTIAL))
    {
        int const err(errno);
        log_warn << "Failed to set MADV_SEQUENTIAL on " << fd.name()
                 << ": " << err << " (" << strerror(err) << ")";
    }

    log_debug << "Memory mapped: " << ptr << " (" << size << " bytes)";
}