Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef __ASM_GENERIC_GETORDER_H
0003 #define __ASM_GENERIC_GETORDER_H
0004 
0005 #ifndef __ASSEMBLY__
0006 
0007 #include <linux/compiler.h>
0008 #include <linux/log2.h>
0009 
0010 /**
0011  * get_order - Determine the allocation order of a memory size
0012  * @size: The size for which to get the order
0013  *
0014  * Determine the allocation order of a particular sized block of memory.  This
0015  * is on a logarithmic scale, where:
0016  *
0017  *  0 -> 2^0 * PAGE_SIZE and below
0018  *  1 -> 2^1 * PAGE_SIZE to 2^0 * PAGE_SIZE + 1
0019  *  2 -> 2^2 * PAGE_SIZE to 2^1 * PAGE_SIZE + 1
0020  *  3 -> 2^3 * PAGE_SIZE to 2^2 * PAGE_SIZE + 1
0021  *  4 -> 2^4 * PAGE_SIZE to 2^3 * PAGE_SIZE + 1
0022  *  ...
0023  *
0024  * The order returned is used to find the smallest allocation granule required
0025  * to hold an object of the specified size.
0026  *
0027  * The result is undefined if the size is 0.
0028  */
0029 static __always_inline __attribute_const__ int get_order(unsigned long size)
0030 {
0031     if (__builtin_constant_p(size)) {
0032         if (!size)
0033             return BITS_PER_LONG - PAGE_SHIFT;
0034 
0035         if (size < (1UL << PAGE_SHIFT))
0036             return 0;
0037 
0038         return ilog2((size) - 1) - PAGE_SHIFT + 1;
0039     }
0040 
0041     size--;
0042     size >>= PAGE_SHIFT;
0043 #if BITS_PER_LONG == 32
0044     return fls(size);
0045 #else
0046     return fls64(size);
0047 #endif
0048 }
0049 
0050 #endif  /* __ASSEMBLY__ */
0051 
0052 #endif  /* __ASM_GENERIC_GETORDER_H */