1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "file_image.h"
29 # define __USE_BSD /* madvise, MADV_WILLNEED */
41 int file_image_open (file_image
* image
, const char * filename
)
51 image
->m_base
= MAP_FAILED
, image
->m_size
= 0;
53 if ((fd
= open (filename
, O_RDONLY
)) == -1)
56 if (fstat (fd
, &st
) == -1)
59 goto cleanup_and_leave
;
62 p
= mmap (0, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
66 goto cleanup_and_leave
;
69 image
->m_base
= p
, image
->m_size
= st
.st_size
;
79 int file_image_pagein (file_image
* image
)
84 // force touching of each page despite the optimizer
90 if ((w
.m_base
= image
->m_base
) == 0)
92 if ((w
.m_size
= image
->m_size
) == 0)
95 if (madvise (w
.m_base
, w
.m_size
, MADV_WILLNEED
) == -1)
98 if ((s
= sysconf (_SC_PAGESIZE
)) == -1)
104 c
^= ((char*)(w
.m_base
))[0];
105 w
.m_base
= (char*)(w
.m_base
) + k
;
110 c
^= ((char*)(w
.m_base
))[0];
119 int file_image_close (file_image
* image
)
124 if (munmap (image
->m_base
, image
->m_size
) == -1)
127 image
->m_base
= 0, image
->m_size
= 0;
131 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */