- add third_party src.
[platform/framework/web/crosswalk.git] / src / native_client / src / trusted / service_runtime / sel_util-inl.h
1 /*
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.
5  */
6
7 /*
8  * NaCl Simple/secure ELF loader (NaCl SEL) misc utilities.  Inlined
9  * functions.  Internal; do not include.
10  */
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
13
14 /*
15  * NaClRoundPage is a bit of a misnomer -- it always rounds up to a
16  * page size, not the nearest.
17  */
18 static INLINE size_t  NaClRoundPage(size_t    nbytes) {
19   return (nbytes + NACL_PAGESIZE - 1) & ~((size_t) NACL_PAGESIZE - 1);
20 }
21
22 static INLINE uint32_t  NaClRoundPage32(uint32_t    nbytes) {
23   return (nbytes + NACL_PAGESIZE - 1) & ~((size_t) NACL_PAGESIZE - 1);
24 }
25
26 static INLINE size_t  NaClRoundAllocPage(size_t    nbytes) {
27   return (nbytes + NACL_MAP_PAGESIZE - 1) & ~((size_t) NACL_MAP_PAGESIZE - 1);
28 }
29
30 static INLINE uint32_t  NaClRoundAllocPage32(uint32_t    nbytes) {
31   return (nbytes + NACL_MAP_PAGESIZE - 1) & ~((uint32_t)NACL_MAP_PAGESIZE - 1);
32 }
33
34 static INLINE size_t NaClTruncPage(size_t  nbytes) {
35   return nbytes & ~((size_t) NACL_PAGESIZE - 1);
36 }
37
38 static INLINE size_t NaClTruncAllocPage(size_t  nbytes) {
39   return nbytes & ~((size_t) NACL_MAP_PAGESIZE - 1);
40 }
41
42 static INLINE size_t  NaClBytesToPages(size_t nbytes) {
43   return (nbytes + NACL_PAGESIZE - 1) >> NACL_PAGESHIFT;
44 }
45
46 static INLINE int /* bool */ NaClIsPageMultiple(uintptr_t addr_or_size) {
47   return 0 == ((NACL_PAGESIZE - 1) & addr_or_size);
48 }
49
50 static INLINE int /* bool */ NaClIsAllocPageMultiple(uintptr_t addr_or_size) {
51   return 0 == ((NACL_MAP_PAGESIZE - 1) & addr_or_size);
52 }
53
54 /*
55  * True host-OS allocation unit.
56  */
57 static INLINE size_t NaClRoundHostAllocPage(size_t  nbytes) {
58 #if NACL_WINDOWS
59   return NaClRoundAllocPage(nbytes);
60 #else   /* NACL_WINDOWS */
61   return NaClRoundPage(nbytes);
62 #endif  /* !NACL_WINDOWS */
63 }
64
65 static INLINE size_t NaClRoundPageNumUpToMapMultiple(size_t npages) {
66   return (npages + NACL_PAGES_PER_MAP - 1) & ~((size_t) NACL_PAGES_PER_MAP - 1);
67 }
68
69 static INLINE size_t NaClTruncPageNumDownToMapMultiple(size_t npages) {
70   return npages & ~((size_t) NACL_PAGES_PER_MAP - 1);
71 }
72
73 #endif  /* NATIVE_CLIENT_SRC_TRUSTED_SERVICE_RUNTIME_SEL_UTIL_INL_H_ */