You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

buddy.h 3.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**
  2. * Copyright 2019 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_BUDDY_H_
  17. #define MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_BUDDY_H_
  18. #include <cstddef>
  19. #include <cstdint>
  20. #include <cstring>
  21. #include <iostream>
  22. #include <memory>
  23. #include <mutex>
  24. #include "minddata/dataset/util/status.h"
  25. using addr_t = int64_t;
  26. using rel_addr_t = int32_t;
  27. using log_t = int;
  28. #define ALLOC_BIT 0x80
  29. #define ONE_BIT 0x40
  30. #define TWO_BIT 0x20
  31. #define MORE_BIT 0x10
  32. #define NOSPACE ((addr_t)(-1))
  33. namespace mindspore {
  34. namespace dataset {
  35. struct BSpaceDescriptor {
  36. int32_t sig;
  37. rel_addr_t addr;
  38. size_t req_size;
  39. size_t blk_size;
  40. };
  41. class BuddySpace {
  42. public:
  43. // C++11 feature. Change STATE into a type safe class with
  44. // the keyword. Don't take out the keyword 'class'
  45. enum class STATE { kFree, kAlloc, kEmpty };
  46. BuddySpace(const BuddySpace &) = delete;
  47. BuddySpace &operator=(const BuddySpace &) = delete;
  48. virtual ~BuddySpace();
  49. Status Alloc(uint64_t sz, BSpaceDescriptor *desc, addr_t *) noexcept;
  50. void Free(const BSpaceDescriptor *desc);
  51. uint64_t GetMinSize() const { return min_; }
  52. uint64_t GetMaxSize() const { return max_; }
  53. int PercentFree() const;
  54. friend std::ostream &operator<<(std::ostream &os, const BuddySpace &s);
  55. static uint64_t NextPowerOf2(uint64_t n) {
  56. if (n <= 1) {
  57. return 1;
  58. }
  59. n = n - 1;
  60. while (n & (n - 1)) {
  61. n = n & (n - 1);
  62. }
  63. return n << 1;
  64. }
  65. static uint32_t Log2(uint64_t n) {
  66. uint32_t cnt = 0;
  67. while (n >>= 1) {
  68. cnt++;
  69. }
  70. return cnt;
  71. }
  72. static Status CreateBuddySpace(std::unique_ptr<BuddySpace> *out_bs, int log_min = 15, int num_lvl = 18);
  73. private:
  74. rel_addr_t *hint_;
  75. int *count_;
  76. char *map_;
  77. int log_min_;
  78. int num_lvl_;
  79. uint64_t min_;
  80. uint64_t max_;
  81. std::unique_ptr<uint8_t[]> mem_;
  82. std::mutex mutex_;
  83. explicit BuddySpace(int log_min = 15, int num_lvl = 18);
  84. Status Init();
  85. addr_t AllocNoLock(const uint64_t sz, BSpaceDescriptor *desc) noexcept;
  86. void FreeNoLock(const BSpaceDescriptor *desc);
  87. uint32_t SizeToBlock(const uint64_t sz) const {
  88. uint32_t reqSize = (sz / min_);
  89. if (sz % min_) {
  90. reqSize++;
  91. }
  92. return reqSize;
  93. }
  94. void GetBuddySegState(const rel_addr_t rel_addr, size_t *rel_sz, STATE *st) const;
  95. void SetBuddySegState(rel_addr_t rel_addr, size_t rel_sz, STATE st);
  96. void JoinBuddySeg(rel_addr_t addr, size_t blk_sz);
  97. void TrimBuddySeg(rel_addr_t addr, size_t blk_sz, size_t ask_sz);
  98. void UnTrimBuddySeg(rel_addr_t addr, size_t blk_sz, size_t ask_sz);
  99. rel_addr_t AllocBuddySeg(uint32_t req_size) noexcept;
  100. void FreeBuddySeg(rel_addr_t addr, size_t blk_size, size_t req_size);
  101. };
  102. } // namespace dataset
  103. } // namespace mindspore
  104. #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_BUDDY_H_