getorder.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. * get_order - Determine the allocation order of a memory size
  9. * @size: The size for which to get the order
  10. *
  11. * Determine the allocation order of a particular sized block of memory. This
  12. * is on a logarithmic scale, where:
  13. *
  14. * 0 -> 2^0 * PAGE_SIZE and below
  15. * 1 -> 2^1 * PAGE_SIZE to 2^0 * PAGE_SIZE + 1
  16. * 2 -> 2^2 * PAGE_SIZE to 2^1 * PAGE_SIZE + 1
  17. * 3 -> 2^3 * PAGE_SIZE to 2^2 * PAGE_SIZE + 1
  18. * 4 -> 2^4 * PAGE_SIZE to 2^3 * PAGE_SIZE + 1
  19. * ...
  20. *
  21. * The order returned is used to find the smallest allocation granule required
  22. * to hold an object of the specified size.
  23. *
  24. * The result is undefined if the size is 0.
  25. */
  26. static inline __attribute_const__ int get_order(unsigned long size)
  27. {
  28. if (__builtin_constant_p(size)) {
  29. if (!size)
  30. return BITS_PER_LONG - PAGE_SHIFT;
  31. if (size < (1UL << PAGE_SHIFT))
  32. return 0;
  33. return ilog2((size) - 1) - PAGE_SHIFT + 1;
  34. }
  35. size--;
  36. size >>= PAGE_SHIFT;
  37. #if BITS_PER_LONG == 32
  38. return fls(size);
  39. #else
  40. return fls64(size);
  41. #endif
  42. }
  43. #endif /* __ASSEMBLY__ */
  44. #endif /* __ASM_GENERIC_GETORDER_H */