getorder.h 1.5 KB

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