Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #include "file_image.h"
30 : :
31 : : #include <unistd.h>
32 : :
33 : : #include <errno.h>
34 : : #include <fcntl.h>
35 : :
36 : : #if defined(LINUX)
37 : : # ifndef __USE_BSD
38 : : # define __USE_BSD /* madvise, MADV_WILLNEED */
39 : : # endif
40 : : #endif /* Linux */
41 : :
42 : : #include <sys/mman.h>
43 : : #include <sys/stat.h>
44 : :
45 : : #include <string.h>
46 : :
47 : : /*
48 : : * file_image_open
49 : : */
50 : 0 : int file_image_open (file_image * image, const char * filename)
51 : : {
52 : 0 : int result = 0;
53 : : int fd;
54 : : struct stat st;
55 : : void * p;
56 : :
57 [ # # ]: 0 : if (image == 0)
58 : 0 : return (EINVAL);
59 : :
60 : 0 : image->m_base = MAP_FAILED, image->m_size = 0;
61 : :
62 [ # # ]: 0 : if ((fd = open (filename, O_RDONLY)) == -1)
63 : 0 : return (errno);
64 : :
65 [ # # ]: 0 : if (fstat (fd, &st) == -1)
66 : : {
67 : 0 : result = errno;
68 : 0 : goto cleanup_and_leave;
69 : : }
70 : :
71 : 0 : p = mmap (0, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
72 [ # # ]: 0 : if (p == MAP_FAILED)
73 : : {
74 : 0 : result = errno;
75 : 0 : goto cleanup_and_leave;
76 : : }
77 : :
78 : 0 : image->m_base = p, image->m_size = st.st_size;
79 : :
80 : : cleanup_and_leave:
81 : 0 : close (fd);
82 : 0 : return (result);
83 : : }
84 : :
85 : : /*
86 : : * file_image_pagein.
87 : : */
88 : 0 : int file_image_pagein (file_image * image)
89 : : {
90 : : file_image w;
91 : : long s;
92 : : size_t k;
93 : : // force touching of each page despite the optimizer
94 : 0 : volatile char c = 0;
95 : :
96 [ # # ]: 0 : if (image == 0)
97 : 0 : return (EINVAL);
98 : :
99 [ # # ]: 0 : if ((w.m_base = image->m_base) == 0)
100 : 0 : return (EINVAL);
101 [ # # ]: 0 : if ((w.m_size = image->m_size) == 0)
102 : 0 : return (0);
103 : :
104 [ # # ]: 0 : if (madvise (w.m_base, w.m_size, MADV_WILLNEED) == -1)
105 : 0 : return (errno);
106 : :
107 [ # # ]: 0 : if ((s = sysconf (_SC_PAGESIZE)) == -1)
108 : 0 : s = 0x1000;
109 : :
110 : 0 : k = (size_t)(s);
111 [ # # ]: 0 : while (w.m_size > k)
112 : : {
113 : 0 : c ^= ((char*)(w.m_base))[0];
114 : 0 : w.m_base = (char*)(w.m_base) + k;
115 : 0 : w.m_size -= k;
116 : : }
117 [ # # ]: 0 : if (w.m_size > 0)
118 : : {
119 : 0 : c ^= ((char*)(w.m_base))[0];
120 : : }
121 : :
122 : 0 : return (0);
123 : : }
124 : :
125 : : /*
126 : : * file_image_close
127 : : */
128 : 0 : int file_image_close (file_image * image)
129 : : {
130 [ # # ]: 0 : if (image == 0)
131 : 0 : return (EINVAL);
132 : :
133 [ # # ]: 0 : if (munmap (image->m_base, image->m_size) == -1)
134 : 0 : return (errno);
135 : :
136 : 0 : image->m_base = 0, image->m_size = 0;
137 : 0 : return (0);
138 : : }
139 : :
140 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|