2 * Copyright 2008, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * NaCl Simple/secure ELF loader (NaCl SEL) misc utilities.
35 #ifndef NATIVE_CLIENT_SERVICE_RUNTIME_SEL_UTIL_H_
36 #define NATIVE_CLIENT_SERVICE_RUNTIME_SEL_UTIL_H_ 1
38 #include "native_client/include/portability.h"
40 #include <sys/types.h>
42 #include "native_client/service_runtime/nacl_config.h"
44 size_t NaClAppPow2Ceil(size_t max_addr
);
47 * NaClRoundPage is a bit of a misnomer -- it always rounds up to a
48 * page size, not the nearest.
50 static INLINE
size_t NaClRoundPage(size_t nbytes
)
52 return (nbytes
+ NACL_PAGESIZE
- 1) & ~(NACL_PAGESIZE
- 1);
55 static INLINE
size_t NaClRoundAllocPage(size_t nbytes
)
57 return (nbytes
+ NACL_MAP_PAGESIZE
- 1) & ~(NACL_MAP_PAGESIZE
- 1);
60 static INLINE
size_t NaClTruncPage(size_t nbytes
)
62 return nbytes
& ~(NACL_PAGESIZE
- 1);
65 static INLINE
size_t NaClTruncAllocPage(size_t nbytes
)
67 return nbytes
& ~(NACL_MAP_PAGESIZE
- 1);
70 static INLINE
size_t NaClBytesToPages(size_t nbytes
)
72 return (nbytes
+ NACL_PAGESIZE
- 1) >> NACL_PAGESHIFT
;
75 static INLINE
int /* bool */ NaClIsPageMultiple(uintptr_t addr_or_size
)
77 return 0 == ((NACL_PAGESIZE
- 1) & addr_or_size
);
80 static INLINE
int /* bool */ NaClIsAllocPageMultiple(uintptr_t addr_or_size
)
82 return 0 == ((NACL_MAP_PAGESIZE
- 1) & addr_or_size
);
86 * True host-OS allocation unit.
88 static INLINE
size_t NaClRoundHostAllocPage(size_t nbytes
)
91 return NaClRoundAllocPage(nbytes
);
92 #else /* NACL_WINDOWS */
93 return NaClRoundPage(nbytes
);
94 #endif /* !NACL_WINDOWS */
97 static INLINE
size_t NaClRoundPageNumUpToMapMultiple(size_t npages
)
99 return (npages
+ NACL_PAGES_PER_MAP
- 1) & ~(NACL_PAGES_PER_MAP
- 1);
102 static INLINE
size_t NaClTruncPageNumDownToMapMultiple(size_t npages
)
104 return npages
& ~(NACL_PAGES_PER_MAP
- 1);
107 #endif /* NATIVE_CLIENT_SERVICE_RUNTIME_SEL_UTIL_H_ */