ytalloc-inl.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #pragma once
  2. #ifndef YT_ALLOC_INL_H_
  3. #error "Direct inclusion of this file is not allowed, include ytalloc.h"
  4. // For the sake of sane code completion.
  5. #include "ytalloc.h"
  6. #endif
  7. #include <util/system/types.h>
  8. namespace NYT::NYTAlloc {
  9. ////////////////////////////////////////////////////////////////////////////////
  10. // Maps small chunk ranks to size in bytes.
  11. constexpr ui16 SmallRankToSize[SmallRankCount] = {
  12. 0,
  13. 16, 32, 48, 64, 96, 128,
  14. 192, 256, 384, 512, 768, 1024, 1536, 2048,
  15. 3072, 4096, 6144, 8192, 12288, 16384, 24576, 32768
  16. };
  17. // Helper array for mapping size to small chunk rank.
  18. constexpr ui8 SizeToSmallRank1[65] = {
  19. 1, 1, 1, 2, 2, // 16, 32
  20. 3, 3, 4, 4, // 48, 64
  21. 5, 5, 5, 5, 6, 6, 6, 6, // 96, 128
  22. 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, // 192, 256
  23. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, // 384
  24. 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 // 512
  25. };
  26. // Helper array for mapping size to small chunk rank.
  27. constexpr unsigned char SizeToSmallRank2[128] = {
  28. 10, 10, 11, 12, // 512, 512, 768, 1022
  29. 13, 13, 14, 14, // 1536, 2048
  30. 15, 15, 15, 15, 16, 16, 16, 16, // 3072, 4096
  31. 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, // 6144, 8192
  32. 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, // 12288
  33. 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, // 16384
  34. 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
  35. 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, // 22576
  36. 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22,
  37. 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22 // 32768
  38. };
  39. ////////////////////////////////////////////////////////////////////////////////
  40. constexpr size_t SizeToSmallRank(size_t size)
  41. {
  42. if (size <= 512) {
  43. return SizeToSmallRank1[(size + 7) >> 3];
  44. } else {
  45. if (size <= LargeAllocationSizeThreshold) {
  46. return SizeToSmallRank2[(size - 1) >> 8];
  47. } else {
  48. return 0;
  49. }
  50. }
  51. }
  52. void* AllocateSmall(size_t rank);
  53. template <size_t Size>
  54. void* AllocateConstSize()
  55. {
  56. constexpr auto rank = SizeToSmallRank(Size);
  57. if constexpr(rank != 0) {
  58. return AllocateSmall(rank);
  59. } else {
  60. return Allocate(Size);
  61. }
  62. }
  63. ////////////////////////////////////////////////////////////////////////////////
  64. } // namespace NYT::NYTAlloc