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

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