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 3.3 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * Copyright 2021 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 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_INCLUDE_API_ALLOCATOR_H
  17. #define MINDSPORE_INCLUDE_API_ALLOCATOR_H
  18. #include <memory>
  19. #include "include/api/types.h"
  20. namespace mindspore {
  21. /// \brief Allocator defined a memory pool for malloc memory and free memory dynamically.
  22. class MS_API Allocator {
  23. public:
  24. /// \brief Destructor of MindSpore Allocator.
  25. virtual ~Allocator() = default;
  26. /// \brief Method to request memory.
  27. ///
  28. /// \param[in] size Define the memory size to request.
  29. virtual void *Malloc(size_t size) = 0;
  30. /// \brief Method to request memory.
  31. ///
  32. /// \param[in] weight Defines the width of memory to request
  33. /// \param[in] height Defines the height of memory to request
  34. /// \param[in] type Defines the data type of memory to request
  35. virtual void *Malloc(size_t weight, size_t height, DataType type) {
  36. return nullptr;
  37. }
  38. /// \brief Method to free memory.
  39. ///
  40. /// \param[in] ptr Define the pointer of a certain memory.
  41. virtual void Free(void *ptr) = 0;
  42. /// \brief Reference count of a certain memory.
  43. ///
  44. /// \param[in] ptr Define the pointer of a certain memory.
  45. ///
  46. /// \return Reference count of a certain memory currently.
  47. virtual int RefCount(void *ptr) = 0;
  48. /// \brief Set reference count of a certain memory.
  49. ///
  50. /// \param[in] ptr Define the pointer of a certain memory.
  51. /// \param[in] ref_count Define the reference count to set.
  52. ///
  53. /// \return Reference count of a certain memory after setting.
  54. virtual int SetRefCount(void *ptr, int ref_count) = 0;
  55. /// \brief Decrease the reference count of a certain memory.
  56. ///
  57. /// \param[in] ptr Define the pointer of a certain memory.
  58. /// \param[in] ref_count Define the reference count to reduce.
  59. ///
  60. /// \return Reference count of a certain memory after decreating.
  61. virtual int DecRefCount(void *ptr, int ref_count) = 0;
  62. /// \brief Increase the reference count of a certain memory.
  63. ///
  64. /// \param[in] ptr Define the pointer of a certain memory.
  65. /// \param[in] ref_count Define the reference count to increase.
  66. ///
  67. /// \return Reference count of a certain memory after increasing.
  68. virtual int IncRefCount(void *ptr, int ref_count) = 0;
  69. /// \brief Static method to create an allocator.
  70. ///
  71. /// \return Smart pointer of an allocator.
  72. static std::shared_ptr<Allocator> Create();
  73. /// \brief Prepare a certain memory.
  74. ///
  75. /// \param[in] ptr Define the pointer of a certain memory to prepare.
  76. ///
  77. /// \return Pointer of ready memory.
  78. virtual void *Prepare(void *ptr) { return ptr; }
  79. protected:
  80. // memory aligned bytes
  81. size_t aligned_size_ = 32;
  82. };
  83. } // namespace mindspore
  84. #endif // MINDSPORE_INCLUDE_API_ALLOCATOR_H