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.

slice.h 4.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * Copyright 2020 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 MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_SLICE_H_
  17. #define MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_SLICE_H_
  18. #include <unistd.h>
  19. #include <cstddef>
  20. #include <utility>
  21. #include "./securec.h"
  22. #include "minddata/dataset/util/allocator.h"
  23. #include "minddata/dataset/util/status.h"
  24. namespace mindspore {
  25. namespace dataset {
  26. /// \brief A ReadableSlice wraps a const pointer in memory and its size.
  27. /// \see WritableSlice for a non-const version
  28. ///
  29. class ReadableSlice {
  30. public:
  31. ReadableSlice() : ptr_(nullptr), sz_(0) {}
  32. ReadableSlice(const void *ptr, size_t sz) : ptr_(ptr), sz_(sz) {}
  33. /// \brief Destructor
  34. ~ReadableSlice() = default;
  35. ReadableSlice(const ReadableSlice &src, off64_t offset, size_t len) {
  36. ptr_ = static_cast<const char *>(src.GetPointer()) + offset;
  37. sz_ = len;
  38. }
  39. ReadableSlice(const ReadableSlice &src, off64_t offset) : ReadableSlice(src, offset, src.sz_ - offset) {}
  40. ReadableSlice(const ReadableSlice &lhs) {
  41. ptr_ = lhs.ptr_;
  42. sz_ = lhs.sz_;
  43. }
  44. ReadableSlice &operator=(const ReadableSlice &lhs) {
  45. if (this != &lhs) {
  46. ptr_ = lhs.ptr_;
  47. sz_ = lhs.sz_;
  48. }
  49. return *this;
  50. }
  51. ReadableSlice(ReadableSlice &&lhs) noexcept {
  52. if (this != &lhs) {
  53. ptr_ = lhs.ptr_;
  54. sz_ = lhs.sz_;
  55. lhs.ptr_ = nullptr;
  56. lhs.sz_ = 0;
  57. }
  58. }
  59. ReadableSlice &operator=(ReadableSlice &&lhs) noexcept {
  60. if (this != &lhs) {
  61. ptr_ = lhs.ptr_;
  62. sz_ = lhs.sz_;
  63. lhs.ptr_ = nullptr;
  64. lhs.sz_ = 0;
  65. }
  66. return *this;
  67. }
  68. /// \brief Getter function
  69. /// \return Const version of the pointer
  70. const void *GetPointer() const { return ptr_; }
  71. /// \brief Getter function
  72. /// \return Size of the slice
  73. size_t GetSize() const { return sz_; }
  74. bool empty() const { return ptr_ == nullptr; }
  75. private:
  76. const void *ptr_;
  77. size_t sz_;
  78. };
  79. /// \brief A WritableSlice inherits from ReadableSlice to allow
  80. /// one to write to the address pointed to by the pointer.
  81. ///
  82. class WritableSlice : public ReadableSlice {
  83. public:
  84. friend class StorageContainer;
  85. friend class CacheService;
  86. friend class CacheServer;
  87. /// \brief Default constructor
  88. WritableSlice() : ReadableSlice(), mutable_data_(nullptr) {}
  89. /// \brief This form of a constructor takes a pointer and its size.
  90. WritableSlice(void *ptr, size_t sz) : ReadableSlice(ptr, sz), mutable_data_(ptr) {}
  91. WritableSlice(const WritableSlice &src, off64_t offset, size_t len);
  92. WritableSlice(const WritableSlice &src, off64_t offset);
  93. WritableSlice(const WritableSlice &lhs) : ReadableSlice(lhs) { mutable_data_ = lhs.mutable_data_; }
  94. /// \brief Destructor
  95. ~WritableSlice() = default;
  96. WritableSlice &operator=(const WritableSlice &lhs) {
  97. if (this != &lhs) {
  98. mutable_data_ = lhs.mutable_data_;
  99. ReadableSlice::operator=(lhs);
  100. }
  101. return *this;
  102. }
  103. WritableSlice(WritableSlice &&lhs) noexcept : ReadableSlice(std::move(lhs)) {
  104. if (this != &lhs) {
  105. mutable_data_ = lhs.mutable_data_;
  106. lhs.mutable_data_ = nullptr;
  107. }
  108. }
  109. WritableSlice &operator=(WritableSlice &&lhs) noexcept {
  110. if (this != &lhs) {
  111. mutable_data_ = lhs.mutable_data_;
  112. lhs.mutable_data_ = nullptr;
  113. ReadableSlice::operator=(std::move(lhs));
  114. }
  115. return *this;
  116. }
  117. /// \brief Copy the content from one slice onto another.
  118. static Status Copy(WritableSlice *dest, const ReadableSlice &src);
  119. private:
  120. void *mutable_data_;
  121. void *GetMutablePointer() { return mutable_data_; }
  122. };
  123. } // namespace dataset
  124. } // namespace mindspore
  125. #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_SLICE_H_