Update ooo320-m1
[ooovba.git] / desktop / source / pagein / file_image_unx.c
blobf82ff1d179613817b83814fb5598b352b2f0583c
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: file_image_unx.c,v $
10 * $Revision: 1.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "file_image.h"
33 #include <unistd.h>
35 #include <errno.h>
36 #include <fcntl.h>
38 #if defined(LINUX)
39 # ifndef __USE_BSD
40 # define __USE_BSD /* madvise, MADV_WILLNEED */
41 # endif
42 #endif /* Linux */
44 #include <sys/mman.h>
45 #include <sys/stat.h>
47 #include <string.h>
50 * file_image_open
52 int file_image_open (file_image * image, const char * filename)
54 int result = 0;
55 int fd;
56 struct stat st;
57 void * p;
59 if (image == 0)
60 return (EINVAL);
62 image->m_base = MAP_FAILED, image->m_size = 0;
64 if ((fd = open (filename, O_RDONLY)) == -1)
65 return (errno);
67 if (fstat (fd, &st) == -1)
69 result = errno;
70 goto cleanup_and_leave;
73 p = mmap (0, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
74 if (p == MAP_FAILED)
76 result = errno;
77 goto cleanup_and_leave;
80 image->m_base = p, image->m_size = st.st_size;
82 cleanup_and_leave:
83 close (fd);
84 return (result);
88 * file_image_pagein.
90 int file_image_pagein (file_image * image)
92 file_image w;
93 long s;
94 size_t k;
95 volatile char c = 0;
97 if (image == 0)
98 return (EINVAL);
100 if ((w.m_base = image->m_base) == 0)
101 return (EINVAL);
102 if ((w.m_size = image->m_size) == 0)
103 return (0);
105 if (madvise (w.m_base, w.m_size, MADV_WILLNEED) == -1)
107 #ifndef MACOSX
108 return (errno);
109 #else
110 /* madvise MADV_WILLNEED need not succeed here */
111 /* but that is fine */
112 #endif
116 #ifndef MACOSX
117 if ((s = sysconf (_SC_PAGESIZE)) == -1)
118 s = 0x1000;
119 #else
120 s = getpagesize();
121 #endif
123 k = (size_t)(s);
124 while (w.m_size > k)
126 c ^= ((char*)(w.m_base))[0];
127 w.m_base = (char*)(w.m_base) + k;
128 w.m_size -= k;
130 if (w.m_size > 0)
132 c ^= ((char*)(w.m_base))[0];
133 w.m_base = (char*)(w.m_base) + w.m_size;
134 w.m_size -= w.m_size;
137 return (0);
141 * file_image_close
143 int file_image_close (file_image * image)
145 if (image == 0)
146 return (EINVAL);
148 if (munmap (image->m_base, image->m_size) == -1)
149 return (errno);
151 image->m_base = 0, image->m_size = 0;
152 return (0);