blob: c02ec6edae66a5b9c48959484e65c7702a15597e [file] [log] [blame]
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
The Android Open Source Project99409882009-03-18 22:20:24 -070016
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080017/*
18 * System utilities.
19 */
Carl Shapiro375fb112011-06-14 20:31:24 -070020#ifndef LIBDEX_SYSUTIL_H_
21#define LIBDEX_SYSUTIL_H_
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080022
23#include <sys/types.h>
24
25/*
Andy McFadden96516932009-10-28 17:39:02 -070026 * System page size. Normally you're expected to get this from
27 * sysconf(_SC_PAGESIZE) or some system-specific define (usually PAGESIZE
28 * or PAGE_SIZE). If we use a simple #define the compiler can generate
29 * appropriate masks directly, so we define it here and verify it as the
30 * VM is starting up.
31 *
32 * Must be a power of 2.
33 */
Chris Dearman356839b2012-01-30 18:22:27 -080034#ifdef PAGE_SHIFT
35#define SYSTEM_PAGE_SIZE (1<<PAGE_SHIFT)
36#else
Andy McFadden96516932009-10-28 17:39:02 -070037#define SYSTEM_PAGE_SIZE 4096
Chris Dearman356839b2012-01-30 18:22:27 -080038#endif
Andy McFadden96516932009-10-28 17:39:02 -070039
40/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080041 * Use this to keep track of mapped segments.
42 */
Carl Shapirobfc97992011-04-27 14:16:08 -070043struct MemMapping {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080044 void* addr; /* start of data */
45 size_t length; /* length of data */
46
47 void* baseAddr; /* page-aligned base address */
48 size_t baseLength; /* length of mapping */
Carl Shapirobfc97992011-04-27 14:16:08 -070049};
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080050
51/*
52 * Copy a map.
53 */
54void sysCopyMap(MemMapping* dst, const MemMapping* src);
55
56/*
Andy McFaddenb5ebe472009-11-16 16:14:54 -080057 * Map a file (from fd's current offset) into a shared, read-only memory
58 * segment that can be made writable. (In some cases, such as when
59 * mapping a file on a FAT filesystem, the result may be fully writable.)
60 *
61 * On success, "pMap" is filled in, and zero is returned.
62 */
63int sysMapFileInShmemWritableReadOnly(int fd, MemMapping* pMap);
64
65/*
Elliott Hughesbbe8cb52012-12-17 09:21:54 -080066 * Map part of a file into a shared, read-only memory segment.
67 *
68 * On success, "pMap" is filled in, and zero is returned.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080069 */
Andy McFadden80a4e242010-04-23 16:34:52 -070070int sysMapFileSegmentInShmem(int fd, off_t start, size_t length,
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080071 MemMapping* pMap);
72
73/*
The Android Open Source Project99409882009-03-18 22:20:24 -070074 * Create a private anonymous mapping, useful for large allocations.
75 *
76 * On success, "pMap" is filled in, and zero is returned.
77 */
78int sysCreatePrivateMap(size_t length, MemMapping* pMap);
79
80/*
Andy McFadden96516932009-10-28 17:39:02 -070081 * Change the access rights on one or more pages. If "wantReadWrite" is
82 * zero, the pages will be made read-only; otherwise they will be read-write.
83 *
84 * Returns 0 on success.
85 */
86int sysChangeMapAccess(void* addr, size_t length, int wantReadWrite,
87 MemMapping* pmap);
88
89/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080090 * Release the pages associated with a shared memory segment.
91 *
92 * This does not free "pMap"; it just releases the memory.
93 */
94void sysReleaseShmem(MemMapping* pMap);
95
Brian Carlstromfbdcfb92010-05-28 15:42:12 -070096/*
97 * Write until all bytes have been written.
98 *
99 * Returns 0 on success, or an errno value on failure.
100 */
101int sysWriteFully(int fd, const void* buf, size_t count, const char* logMsg);
102
Dan Bornstein650177e2010-12-16 13:17:30 -0800103/*
104 * Copy the given number of bytes from one fd to another. Returns
105 * 0 on success, -1 on failure.
106 */
107int sysCopyFileToFile(int outFd, int inFd, size_t count);
108
Carl Shapiro375fb112011-06-14 20:31:24 -0700109#endif // LIBDEX_SYSUTIL_H_