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.

arena.h 3.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 DATASET_UTIL_ARENA_H_
  17. #define DATASET_UTIL_ARENA_H_
  18. #include <memory>
  19. #include <mutex>
  20. #include <utility>
  21. #include "dataset/util/memory_pool.h"
  22. #include "dataset/util/treap.h"
  23. #define ARENA_LOG_BLK_SZ (6u)
  24. #define ARENA_BLK_SZ (static_cast<uint16_t>(1u << ARENA_LOG_BLK_SZ))
  25. #define ARENA_WALL_OVERHEAD_SZ 32
  26. namespace mindspore {
  27. namespace dataset {
  28. // This is a memory arena based on a treap data structure.
  29. // The constructor of the Arena takes the size of the initial memory size (in MB).
  30. // Internally we divide the memory into multiple blocks. Each block is 64 bytes.
  31. // The treap contains all the free blocks with the relative memory address as key
  32. // and the size of the block as priority.
  33. //
  34. // Initially the treap has only one root which is the whole memory piece.
  35. //
  36. // For memory suballocation, we pop the root node of the treap which contains the largest free block.
  37. // We allocate what we need and return the rest back to the treap. We search for the first fit instead
  38. // of the best fit so to give us a constant time in memory allocation.
  39. //
  40. // When a block of memory is freed. It is joined with the blocks before and after (if they are available) to
  41. // form a bigger block.
  42. class Arena : public MemoryPool {
  43. public:
  44. Arena(const Arena &) = delete;
  45. Arena &operator=(const Arena &) = delete;
  46. ~Arena() override {
  47. if (ptr_ != nullptr) {
  48. free(ptr_);
  49. ptr_ = nullptr;
  50. }
  51. }
  52. Status Allocate(size_t n, void **p) override;
  53. Status Reallocate(void **, size_t old_sz, size_t new_sz) override;
  54. void Deallocate(void *) override;
  55. uint64_t get_max_size() const override;
  56. static uint64_t SizeToBlk(uint64_t sz) {
  57. uint64_t req_blk = sz / ARENA_BLK_SZ;
  58. if (sz % ARENA_BLK_SZ) {
  59. ++req_blk;
  60. }
  61. return req_blk;
  62. }
  63. int PercentFree() const override;
  64. const void *get_base_addr() const { return ptr_; }
  65. friend std::ostream &operator<<(std::ostream &os, const Arena &s);
  66. static Status CreateArena(std::shared_ptr<Arena> *p_ba, size_t val_in_MB = 4096);
  67. private:
  68. std::mutex mux_;
  69. Treap<uint64_t, uint64_t> tr_;
  70. void *ptr_;
  71. size_t size_in_MB_;
  72. size_t size_in_bytes_;
  73. explicit Arena(size_t val_in_MB = 4096);
  74. std::pair<std::pair<uint64_t, uint64_t>, bool> FindPrevBlk(uint64_t addr);
  75. Status Init();
  76. bool BlockEnlarge(uint64_t *addr, uint64_t old_sz, uint64_t new_sz);
  77. Status FreeAndAlloc(void **pp, size_t old_sz, size_t new_sz);
  78. void *get_user_addr(void *base_addr) const { return reinterpret_cast<char *>(base_addr) + ARENA_WALL_OVERHEAD_SZ; }
  79. void *get_base_addr(void *user_addr) const { return reinterpret_cast<char *>(user_addr) - ARENA_WALL_OVERHEAD_SZ; }
  80. };
  81. } // namespace dataset
  82. } // namespace mindspore
  83. #endif // DATASET_UTIL_ARENA_H_