2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #ifndef _LIBDEX_SYSUTIL
21 #define _LIBDEX_SYSUTIL
23 #include <sys/types.h>
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
32 * Must be a power of 2.
34 #define SYSTEM_PAGE_SIZE 4096
37 * Use this to keep track of mapped segments.
39 typedef struct MemMapping {
40 void* addr; /* start of data */
41 size_t length; /* length of data */
43 void* baseAddr; /* page-aligned base address */
44 size_t baseLength; /* length of mapping */
50 void sysCopyMap(MemMapping* dst, const MemMapping* src);
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.
56 * The segment is read-write, allowing VM fixups. (It should be modified
57 * to support .gz/.zip compressed data.)
59 * On success, "pMap" is filled in, and zero is returned.
61 int sysLoadFileInShmem(int fd, MemMapping* pMap);
64 * Map a file (from fd's current offset) into a shared,
65 * read-only memory segment.
67 * On success, "pMap" is filled in, and zero is returned.
69 int sysMapFileInShmemReadOnly(int fd, MemMapping* pMap);
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.)
76 * On success, "pMap" is filled in, and zero is returned.
78 int sysMapFileInShmemWritableReadOnly(int fd, MemMapping* pMap);
81 * Like sysMapFileInShmemReadOnly, but on only part of a file.
83 int sysMapFileSegmentInShmem(int fd, off_t start, size_t length,
87 * Create a private anonymous mapping, useful for large allocations.
89 * On success, "pMap" is filled in, and zero is returned.
91 int sysCreatePrivateMap(size_t length, MemMapping* pMap);
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.
97 * Returns 0 on success.
99 int sysChangeMapAccess(void* addr, size_t length, int wantReadWrite,
103 * Release the pages associated with a shared memory segment.
105 * This does not free "pMap"; it just releases the memory.
107 void sysReleaseShmem(MemMapping* pMap);
110 * Write until all bytes have been written.
112 * Returns 0 on success, or an errno value on failure.
114 int sysWriteFully(int fd, const void* buf, size_t count, const char* logMsg);
117 * Copy the given number of bytes from one fd to another. Returns
118 * 0 on success, -1 on failure.
120 int sysCopyFileToFile(int outFd, int inFd, size_t count);
122 #endif /*_DALVIK_SYSUTIL*/