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.

allocator.h 2.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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_ALLOCATOR_H_
  17. #define DATASET_UTIL_ALLOCATOR_H_
  18. #include <cstdlib>
  19. #include <memory>
  20. #include <type_traits>
  21. #include "dataset/util/memory_pool.h"
  22. namespace mindspore {
  23. namespace dataset {
  24. // The following conforms to the requirements of
  25. // std::allocator. Do not rename/change any needed
  26. // requirements, e.g. function names, typedef etc.
  27. template <typename T>
  28. class Allocator {
  29. public:
  30. template <typename U>
  31. friend class Allocator;
  32. using value_type = T;
  33. using pointer = T *;
  34. using const_pointer = const T *;
  35. using reference = T &;
  36. using const_reference = const T &;
  37. using size_type = uint64_t;
  38. template <typename U>
  39. struct rebind {
  40. using other = Allocator<U>;
  41. };
  42. using propagate_on_container_copy_assignment = std::true_type;
  43. using propagate_on_container_move_assignment = std::true_type;
  44. using propagate_on_container_swap = std::true_type;
  45. explicit Allocator(const std::shared_ptr<MemoryPool> &b) : pool_(b) {}
  46. ~Allocator() = default;
  47. template <typename U>
  48. explicit Allocator(Allocator<U> const &rhs) : pool_(rhs.pool_) {}
  49. template <typename U>
  50. bool operator==(Allocator<U> const &rhs) const {
  51. return pool_ == rhs.pool_;
  52. }
  53. template <typename U>
  54. bool operator!=(Allocator<U> const &rhs) const {
  55. return pool_ != rhs.pool_;
  56. }
  57. pointer allocate(std::size_t n) {
  58. void *p;
  59. Status rc = pool_->Allocate(n * sizeof(T), &p);
  60. if (rc.IsOk()) {
  61. return reinterpret_cast<pointer>(p);
  62. } else if (rc.IsOutofMemory()) {
  63. throw std::bad_alloc();
  64. } else {
  65. throw std::exception();
  66. }
  67. }
  68. void deallocate(pointer p, std::size_t n = 0) noexcept { pool_->Deallocate(p); }
  69. size_type max_size() { return pool_->get_max_size(); }
  70. private:
  71. std::shared_ptr<MemoryPool> pool_;
  72. };
  73. } // namespace dataset
  74. } // namespace mindspore
  75. #endif // DATASET_UTIL_ALLOCATOR_H_