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_H_
21 #define LIBDEX_SYSUTIL_H_
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.
35 #define SYSTEM_PAGE_SIZE (1<<PAGE_SHIFT)
37 #define SYSTEM_PAGE_SIZE 4096
41 * Use this to keep track of mapped segments.
44 void* addr; /* start of data */
45 size_t length; /* length of data */
47 void* baseAddr; /* page-aligned base address */
48 size_t baseLength; /* length of mapping */
54 void sysCopyMap(MemMapping* dst, const MemMapping* src);
57 * 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.)
61 * On success, "pMap" is filled in, and zero is returned.
63 int sysMapFileInShmemWritableReadOnly(int fd, MemMapping* pMap);
66 * Map part of a file into a shared, read-only memory segment.
68 * On success, "pMap" is filled in, and zero is returned.
70 int sysMapFileSegmentInShmem(int fd, off_t start, size_t length,
74 * Create a private anonymous mapping, useful for large allocations.
76 * On success, "pMap" is filled in, and zero is returned.
78 int sysCreatePrivateMap(size_t length, MemMapping* pMap);
81 * 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.
84 * Returns 0 on success.
86 int sysChangeMapAccess(void* addr, size_t length, int wantReadWrite,
90 * Release the pages associated with a shared memory segment.
92 * This does not free "pMap"; it just releases the memory.
94 void sysReleaseShmem(MemMapping* pMap);
97 * Write until all bytes have been written.
99 * Returns 0 on success, or an errno value on failure.
101 int sysWriteFully(int fd, const void* buf, size_t count, const char* logMsg);
104 * Copy the given number of bytes from one fd to another. Returns
105 * 0 on success, -1 on failure.
107 int sysCopyFileToFile(int outFd, int inFd, size_t count);
109 #endif // LIBDEX_SYSUTIL_H_