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.0 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 free memory.
  31. ///
  32. /// \param[in] ptr Define the pointer of a certain memory.
  33. virtual void Free(void *ptr) = 0;
  34. /// \brief Reference count of a certain memory.
  35. ///
  36. /// \param[in] ptr Define the pointer of a certain memory.
  37. ///
  38. /// \return Reference count of a certain memory currently.
  39. virtual int RefCount(void *ptr) = 0;
  40. /// \brief Set reference count of a certain memory.
  41. ///
  42. /// \param[in] ptr Define the pointer of a certain memory.
  43. /// \param[in] ref_count Define the reference count to set.
  44. ///
  45. /// \return Reference count of a certain memory after setting.
  46. virtual int SetRefCount(void *ptr, int ref_count) = 0;
  47. /// \brief Decrease the reference count of a certain memory.
  48. ///
  49. /// \param[in] ptr Define the pointer of a certain memory.
  50. /// \param[in] ref_count Define the reference count to reduce.
  51. ///
  52. /// \return Reference count of a certain memory after decreating.
  53. virtual int DecRefCount(void *ptr, int ref_count) = 0;
  54. /// \brief Increase the reference count of a certain memory.
  55. ///
  56. /// \param[in] ptr Define the pointer of a certain memory.
  57. /// \param[in] ref_count Define the reference count to increase.
  58. ///
  59. /// \return Reference count of a certain memory after increasing.
  60. virtual int IncRefCount(void *ptr, int ref_count) = 0;
  61. /// \brief Static method to create an allocator.
  62. ///
  63. /// \return Smart pointer of an allocator.
  64. static std::shared_ptr<Allocator> Create();
  65. /// \brief Prepare a certain memory.
  66. ///
  67. /// \param[in] ptr Define the pointer of a certain memory to prepare.
  68. ///
  69. /// \return Pointer of ready memory.
  70. virtual void *Prepare(void *ptr) { return ptr; }
  71. protected:
  72. // memory aligned bytes
  73. size_t aligned_size_ = 32;
  74. };
  75. } // namespace mindspore
  76. #endif // MINDSPORE_INCLUDE_API_ALLOCATOR_H