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