xref: /OK3568_Linux_fs/kernel/include/asm-generic/getorder.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef __ASM_GENERIC_GETORDER_H
3*4882a593Smuzhiyun #define __ASM_GENERIC_GETORDER_H
4*4882a593Smuzhiyun 
5*4882a593Smuzhiyun #ifndef __ASSEMBLY__
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #include <linux/compiler.h>
8*4882a593Smuzhiyun #include <linux/log2.h>
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun /**
11*4882a593Smuzhiyun  * get_order - Determine the allocation order of a memory size
12*4882a593Smuzhiyun  * @size: The size for which to get the order
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * Determine the allocation order of a particular sized block of memory.  This
15*4882a593Smuzhiyun  * is on a logarithmic scale, where:
16*4882a593Smuzhiyun  *
17*4882a593Smuzhiyun  *	0 -> 2^0 * PAGE_SIZE and below
18*4882a593Smuzhiyun  *	1 -> 2^1 * PAGE_SIZE to 2^0 * PAGE_SIZE + 1
19*4882a593Smuzhiyun  *	2 -> 2^2 * PAGE_SIZE to 2^1 * PAGE_SIZE + 1
20*4882a593Smuzhiyun  *	3 -> 2^3 * PAGE_SIZE to 2^2 * PAGE_SIZE + 1
21*4882a593Smuzhiyun  *	4 -> 2^4 * PAGE_SIZE to 2^3 * PAGE_SIZE + 1
22*4882a593Smuzhiyun  *	...
23*4882a593Smuzhiyun  *
24*4882a593Smuzhiyun  * The order returned is used to find the smallest allocation granule required
25*4882a593Smuzhiyun  * to hold an object of the specified size.
26*4882a593Smuzhiyun  *
27*4882a593Smuzhiyun  * The result is undefined if the size is 0.
28*4882a593Smuzhiyun  */
get_order(unsigned long size)29*4882a593Smuzhiyun static inline __attribute_const__ int get_order(unsigned long size)
30*4882a593Smuzhiyun {
31*4882a593Smuzhiyun 	if (__builtin_constant_p(size)) {
32*4882a593Smuzhiyun 		if (!size)
33*4882a593Smuzhiyun 			return BITS_PER_LONG - PAGE_SHIFT;
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun 		if (size < (1UL << PAGE_SHIFT))
36*4882a593Smuzhiyun 			return 0;
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun 		return ilog2((size) - 1) - PAGE_SHIFT + 1;
39*4882a593Smuzhiyun 	}
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun 	size--;
42*4882a593Smuzhiyun 	size >>= PAGE_SHIFT;
43*4882a593Smuzhiyun #if BITS_PER_LONG == 32
44*4882a593Smuzhiyun 	return fls(size);
45*4882a593Smuzhiyun #else
46*4882a593Smuzhiyun 	return fls64(size);
47*4882a593Smuzhiyun #endif
48*4882a593Smuzhiyun }
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun #endif	/* __ASSEMBLY__ */
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun #endif	/* __ASM_GENERIC_GETORDER_H */
53