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