cnstream_allocator.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*************************************************************************
  2. * Copyright (C) [2020] by Cambricon, Inc. All rights reserved
  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. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. * THE SOFTWARE.
  19. *************************************************************************/
  20. #ifndef CNSTREAM_ALLOCATOR_HPP_
  21. #define CNSTREAM_ALLOCATOR_HPP_
  22. #include <atomic>
  23. #include <memory>
  24. #include <new>
  25. #include "cnstream_common.hpp"
  26. #include "cnstream_logging.hpp"
  27. #include "util/cnstream_queue.hpp"
  28. /**
  29. * @file cnstream_allocator.hpp
  30. *
  31. * This file contains a declaration of the CNStream memory allocator.
  32. */
  33. namespace cnstream {
  34. /**
  35. * @class IDataDeallocator
  36. *
  37. * @brief IDataDeallocator is an abstract class of deallocator for the CNDecoder buffer.
  38. */
  39. class IDataDeallocator {
  40. public:
  41. /*!
  42. * @brief Destructs the base object.
  43. *
  44. * @return No return value.
  45. */
  46. virtual ~IDataDeallocator() {}
  47. };
  48. /*!
  49. * @class MluDeviceGuard
  50. *
  51. * @brief MluDeviceGuard is a class for setting current thread's device handler.
  52. */
  53. class MluDeviceGuard : public NonCopyable {
  54. public:
  55. /*!
  56. * @brief Sets the device handler with the given device ordinal.
  57. *
  58. * @param[in] device_id The device ordinal to retrieve.
  59. *
  60. * @return No return value.
  61. */
  62. explicit MluDeviceGuard(int device_id);
  63. /*!
  64. * @brief Destructs an object.
  65. *
  66. * @return No return value.
  67. */
  68. ~MluDeviceGuard();
  69. private:
  70. int device_id_ = 0;
  71. };
  72. /*!
  73. * @brief Allocates CPU memory with the given size.
  74. *
  75. * @param[in] size The size needs to be allocated.
  76. *
  77. * @return The shared pointer to the allocated memory.
  78. *
  79. * @note Because of CNCodec's constraints, the given size will be aligned up to 4096 inside this
  80. * function before doing allocation.
  81. */
  82. std::shared_ptr<void> cnCpuMemAlloc(size_t size);
  83. /*!
  84. * @brief Allocates MLU memory with the given size at specific device .
  85. *
  86. * @param[in] size The size needs to be allocated.
  87. * @param[in] device_id The device ordinal.
  88. *
  89. * @return The shared pointer to the allocated memory.
  90. *
  91. * @note Because of CNCodec's constraints, the given size will be aligned up to 4096 inside this
  92. * function before doing allocation.
  93. */
  94. std::shared_ptr<void> cnMluMemAlloc(size_t size, int device_id);
  95. } // namespace cnstream
  96. #endif // CNSTREAM_ALLOCATOR_HPP_