2 * Copyright 2009 The Native Client Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can
4 * be found in the LICENSE file.
8 * NaCl Simple/secure ELF loader (NaCl SEL) misc utilities. Inlined
9 * functions. Internal; do not include.
11 #ifndef NATIVE_CLIENT_SRC_TRUSTED_SERVICE_RUNTIME_SEL_UTIL_INL_H_
12 #define NATIVE_CLIENT_SRC_TRUSTED_SERVICE_RUNTIME_SEL_UTIL_INL_H_ 1
15 * NaClRoundPage is a bit of a misnomer -- it always rounds up to a
16 * page size, not the nearest.
18 static INLINE size_t NaClRoundPage(size_t nbytes) {
19 return (nbytes + NACL_PAGESIZE - 1) & ~((size_t) NACL_PAGESIZE - 1);
22 static INLINE uint32_t NaClRoundPage32(uint32_t nbytes) {
23 return (nbytes + NACL_PAGESIZE - 1) & ~((size_t) NACL_PAGESIZE - 1);
26 static INLINE size_t NaClRoundAllocPage(size_t nbytes) {
27 return (nbytes + NACL_MAP_PAGESIZE - 1) & ~((size_t) NACL_MAP_PAGESIZE - 1);
30 static INLINE uint32_t NaClRoundAllocPage32(uint32_t nbytes) {
31 return (nbytes + NACL_MAP_PAGESIZE - 1) & ~((uint32_t)NACL_MAP_PAGESIZE - 1);
34 static INLINE size_t NaClTruncPage(size_t nbytes) {
35 return nbytes & ~((size_t) NACL_PAGESIZE - 1);
38 static INLINE size_t NaClTruncAllocPage(size_t nbytes) {
39 return nbytes & ~((size_t) NACL_MAP_PAGESIZE - 1);
42 static INLINE size_t NaClBytesToPages(size_t nbytes) {
43 return (nbytes + NACL_PAGESIZE - 1) >> NACL_PAGESHIFT;
46 static INLINE int /* bool */ NaClIsPageMultiple(uintptr_t addr_or_size) {
47 return 0 == ((NACL_PAGESIZE - 1) & addr_or_size);
50 static INLINE int /* bool */ NaClIsAllocPageMultiple(uintptr_t addr_or_size) {
51 return 0 == ((NACL_MAP_PAGESIZE - 1) & addr_or_size);
55 * True host-OS allocation unit.
57 static INLINE size_t NaClRoundHostAllocPage(size_t nbytes) {
59 return NaClRoundAllocPage(nbytes);
60 #else /* NACL_WINDOWS */
61 return NaClRoundPage(nbytes);
62 #endif /* !NACL_WINDOWS */
65 static INLINE size_t NaClRoundPageNumUpToMapMultiple(size_t npages) {
66 return (npages + NACL_PAGES_PER_MAP - 1) & ~((size_t) NACL_PAGES_PER_MAP - 1);
69 static INLINE size_t NaClTruncPageNumDownToMapMultiple(size_t npages) {
70 return npages & ~((size_t) NACL_PAGES_PER_MAP - 1);
73 #endif /* NATIVE_CLIENT_SRC_TRUSTED_SERVICE_RUNTIME_SEL_UTIL_INL_H_ */