getorder.h 1.4 KB

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