1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: file_image_unx.c,v $
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"
40 # define __USE_BSD /* madvise, MADV_WILLNEED */
52 int file_image_open (file_image
* image
, const char * filename
)
62 image
->m_base
= MAP_FAILED
, image
->m_size
= 0;
64 if ((fd
= open (filename
, O_RDONLY
)) == -1)
67 if (fstat (fd
, &st
) == -1)
70 goto cleanup_and_leave
;
73 p
= mmap (0, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
77 goto cleanup_and_leave
;
80 image
->m_base
= p
, image
->m_size
= st
.st_size
;
90 int file_image_pagein (file_image
* image
)
100 if ((w
.m_base
= image
->m_base
) == 0)
102 if ((w
.m_size
= image
->m_size
) == 0)
105 if (madvise (w
.m_base
, w
.m_size
, MADV_WILLNEED
) == -1)
110 /* madvise MADV_WILLNEED need not succeed here */
111 /* but that is fine */
117 if ((s
= sysconf (_SC_PAGESIZE
)) == -1)
126 c
^= ((char*)(w
.m_base
))[0];
127 w
.m_base
= (char*)(w
.m_base
) + k
;
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
;
143 int file_image_close (file_image
* image
)
148 if (munmap (image
->m_base
, image
->m_size
) == -1)
151 image
->m_base
= 0, image
->m_size
= 0;