SysUtil.h revision 375fb116bcb817b37509ab579dbd55cdbb765cbf
1/*
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 */
16
17/*
18 * System utilities.
19 */
20#ifndef LIBDEX_SYSUTIL_H_
21#define LIBDEX_SYSUTIL_H_
22
23#include <sys/types.h>
24
25/*
26 * 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 */
34#define SYSTEM_PAGE_SIZE        4096
35
36/*
37 * Use this to keep track of mapped segments.
38 */
39struct MemMapping {
40    void*   addr;           /* start of data */
41    size_t  length;         /* length of data */
42
43    void*   baseAddr;       /* page-aligned base address */
44    size_t  baseLength;     /* length of mapping */
45};
46
47/*
48 * Copy a map.
49 */
50void sysCopyMap(MemMapping* dst, const MemMapping* src);
51
52/*
53 * Load a file into a new shared memory segment.  All data from the current
54 * offset to the end of the file is pulled in.
55 *
56 * The segment is read-write, allowing VM fixups.  (It should be modified
57 * to support .gz/.zip compressed data.)
58 *
59 * On success, "pMap" is filled in, and zero is returned.
60 */
61int sysLoadFileInShmem(int fd, MemMapping* pMap);
62
63/*
64 * Map a file (from fd's current offset) into a shared,
65 * read-only memory segment.
66 *
67 * On success, "pMap" is filled in, and zero is returned.
68 */
69int sysMapFileInShmemReadOnly(int fd, MemMapping* pMap);
70
71/*
72 * Map a file (from fd's current offset) into a shared, read-only memory
73 * segment that can be made writable.  (In some cases, such as when
74 * mapping a file on a FAT filesystem, the result may be fully writable.)
75 *
76 * On success, "pMap" is filled in, and zero is returned.
77 */
78int sysMapFileInShmemWritableReadOnly(int fd, MemMapping* pMap);
79
80/*
81 * Like sysMapFileInShmemReadOnly, but on only part of a file.
82 */
83int sysMapFileSegmentInShmem(int fd, off_t start, size_t length,
84    MemMapping* pMap);
85
86/*
87 * Create a private anonymous mapping, useful for large allocations.
88 *
89 * On success, "pMap" is filled in, and zero is returned.
90 */
91int sysCreatePrivateMap(size_t length, MemMapping* pMap);
92
93/*
94 * Change the access rights on one or more pages.  If "wantReadWrite" is
95 * zero, the pages will be made read-only; otherwise they will be read-write.
96 *
97 * Returns 0 on success.
98 */
99int sysChangeMapAccess(void* addr, size_t length, int wantReadWrite,
100    MemMapping* pmap);
101
102/*
103 * Release the pages associated with a shared memory segment.
104 *
105 * This does not free "pMap"; it just releases the memory.
106 */
107void sysReleaseShmem(MemMapping* pMap);
108
109/*
110 * Write until all bytes have been written.
111 *
112 * Returns 0 on success, or an errno value on failure.
113 */
114int sysWriteFully(int fd, const void* buf, size_t count, const char* logMsg);
115
116/*
117 * Copy the given number of bytes from one fd to another. Returns
118 * 0 on success, -1 on failure.
119 */
120int sysCopyFileToFile(int outFd, int inFd, size_t count);
121
122#endif  // LIBDEX_SYSUTIL_H_
123