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