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