LCOV - code coverage report
Current view: top level - desktop/unx/source - file_image_unx.c (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 32 44 72.7 %
Date: 2014-04-14 Functions: 3 3 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include "file_image.h"
      21             : 
      22             : #include <unistd.h>
      23             : 
      24             : #include <errno.h>
      25             : #include <fcntl.h>
      26             : 
      27             : #if defined(LINUX)
      28             : #  ifndef __USE_BSD
      29             : #    define __USE_BSD /* madvise, MADV_WILLNEED */
      30             : #  endif
      31             : #endif /* Linux */
      32             : 
      33             : #include <sys/mman.h>
      34             : #include <sys/stat.h>
      35             : 
      36             : #include <string.h>
      37             : 
      38             : /*
      39             :  * file_image_open
      40             :  */
      41     2210260 : int file_image_open (file_image * image, const char * filename)
      42             : {
      43     2210260 :     int         result = 0;
      44             :     int         fd;
      45             :     struct stat st;
      46             :     void *      p;
      47             : 
      48     2210260 :     if (image == 0)
      49           0 :         return (EINVAL);
      50             : 
      51     2210260 :     image->m_base = MAP_FAILED, image->m_size = 0;
      52             : 
      53     2210260 :     if ((fd = open (filename, O_RDONLY)) == -1)
      54       85010 :         return (errno);
      55             : 
      56     2125250 :     if (fstat (fd, &st) == -1)
      57             :     {
      58           0 :         result = errno;
      59           0 :         goto cleanup_and_leave;
      60             :     }
      61             : 
      62     2125250 :     p = mmap (0, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
      63     2125250 :     if (p == MAP_FAILED)
      64             :     {
      65           0 :         result = errno;
      66           0 :         goto cleanup_and_leave;
      67             :     }
      68             : 
      69     2125250 :     image->m_base = p, image->m_size = st.st_size;
      70             : 
      71             : cleanup_and_leave:
      72     2125250 :     close (fd);
      73     2125250 :     return (result);
      74             : }
      75             : 
      76             : /*
      77             :  * file_image_pagein.
      78             :  */
      79     2125250 : int file_image_pagein (file_image * image)
      80             : {
      81             :     file_image    w;
      82             :     long          s;
      83             :     size_t        k;
      84             :     // force touching of each page despite the optimizer
      85     2125250 :     volatile char c = 0;
      86             : 
      87     2125250 :     if (image == 0)
      88           0 :         return (EINVAL);
      89             : 
      90     2125250 :     if ((w.m_base = image->m_base) == 0)
      91           0 :         return (EINVAL);
      92     2125250 :     if ((w.m_size = image->m_size) == 0)
      93           0 :         return (0);
      94             : 
      95     2125250 :     if (madvise (w.m_base, w.m_size, MADV_WILLNEED) == -1)
      96           0 :         return (errno);
      97             : 
      98     2125250 :     if ((s = sysconf (_SC_PAGESIZE)) == -1)
      99           0 :         s = 0x1000;
     100             : 
     101     2125250 :     k = (size_t)(s);
     102  2667358770 :     while (w.m_size > k)
     103             :     {
     104  2663108270 :         c ^= ((char*)(w.m_base))[0];
     105  2663108270 :         w.m_base  = (char*)(w.m_base) + k;
     106  2663108270 :         w.m_size -= k;
     107             :     }
     108     2125250 :     if (w.m_size > 0)
     109             :     {
     110     2125250 :         c ^= ((char*)(w.m_base))[0];
     111             :     }
     112             : 
     113     2125250 :     return (0);
     114             : }
     115             : 
     116             : /*
     117             :  * file_image_close
     118             :  */
     119     2125250 : int file_image_close (file_image * image)
     120             : {
     121     2125250 :     if (image == 0)
     122           0 :         return (EINVAL);
     123             : 
     124     2125250 :     if (munmap (image->m_base, image->m_size) == -1)
     125           0 :         return (errno);
     126             : 
     127     2125250 :     image->m_base = 0, image->m_size = 0;
     128     2125250 :     return (0);
     129             : }
     130             : 
     131             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10