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

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