blob: c64bea7a52bebd5c7332203e1e3a78bfa69b84c3 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Arnd Bergmann5b17e1c2009-05-13 22:56:30 +00002#ifndef __ASM_GENERIC_GETORDER_H
3#define __ASM_GENERIC_GETORDER_H
4
5#ifndef __ASSEMBLY__
6
7#include <linux/compiler.h>
David Howellsd66acc32012-02-20 22:39:29 +00008#include <linux/log2.h>
9
10/*
11 * Runtime evaluation of get_order()
12 */
13static inline __attribute_const__
14int __get_order(unsigned long size)
15{
16 int order;
17
18 size--;
19 size >>= PAGE_SHIFT;
20#if BITS_PER_LONG == 32
21 order = fls(size);
22#else
23 order = fls64(size);
24#endif
25 return order;
26}
Arnd Bergmann5b17e1c2009-05-13 22:56:30 +000027
David Howellse0891a92012-02-20 22:39:18 +000028/**
29 * get_order - Determine the allocation order of a memory size
30 * @size: The size for which to get the order
31 *
32 * Determine the allocation order of a particular sized block of memory. This
33 * is on a logarithmic scale, where:
34 *
35 * 0 -> 2^0 * PAGE_SIZE and below
36 * 1 -> 2^1 * PAGE_SIZE to 2^0 * PAGE_SIZE + 1
37 * 2 -> 2^2 * PAGE_SIZE to 2^1 * PAGE_SIZE + 1
38 * 3 -> 2^3 * PAGE_SIZE to 2^2 * PAGE_SIZE + 1
39 * 4 -> 2^4 * PAGE_SIZE to 2^3 * PAGE_SIZE + 1
40 * ...
41 *
42 * The order returned is used to find the smallest allocation granule required
43 * to hold an object of the specified size.
44 *
45 * The result is undefined if the size is 0.
46 *
47 * This function may be used to initialise variables with compile time
48 * evaluations of constants.
49 */
David Howellsd66acc32012-02-20 22:39:29 +000050#define get_order(n) \
51( \
52 __builtin_constant_p(n) ? ( \
Joerg Roedelb8934852012-02-24 13:58:15 +010053 ((n) == 0UL) ? BITS_PER_LONG - PAGE_SHIFT : \
54 (((n) < (1UL << PAGE_SHIFT)) ? 0 : \
David Howellsd66acc32012-02-20 22:39:29 +000055 ilog2((n) - 1) - PAGE_SHIFT + 1) \
56 ) : \
57 __get_order(n) \
58)
Arnd Bergmann5b17e1c2009-05-13 22:56:30 +000059
60#endif /* __ASSEMBLY__ */
61
62#endif /* __ASM_GENERIC_GETORDER_H */