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"
35 int file_image_open (file_image
* image
, const char * filename
)
45 image
->m_base
= MAP_FAILED
;
48 if ((fd
= open (filename
, O_RDONLY
)) == -1)
51 if (fstat (fd
, &st
) == -1)
54 goto cleanup_and_leave
;
57 p
= mmap (NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
61 goto cleanup_and_leave
;
65 image
->m_size
= st
.st_size
;
75 int file_image_pagein (file_image
* image
)
83 if (image
->m_base
== NULL
)
85 if (image
->m_size
== 0)
88 if (madvise (image
->m_base
, image
->m_size
, MADV_WILLNEED
) == -1)
91 s
= sysconf (_SC_PAGESIZE
);
94 // force touching of each page despite the optimizer
95 for(idx
= 0; idx
< image
->m_size
; idx
+= (size_t)s
)
97 c
^= ((volatile const char*)(image
->m_base
))[idx
];
99 c
^= ((volatile const char*)(image
->m_base
))[image
->m_size
-1];
107 int file_image_close (file_image
* image
)
112 if (munmap (image
->m_base
, image
->m_size
) == -1)
115 image
->m_base
= NULL
;
120 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */