@@ -12,6 +12,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
12
See the License for the specific language governing permissions and
13
13
limitations under the License. */
14
14
15
+ #include < glog/logging.h>
15
16
#include " paddle/fluid/inference/api/paddle_inference_api.h"
16
17
17
18
namespace paddle {
@@ -40,19 +41,36 @@ PaddleBuf::PaddleBuf(PaddleBuf&& other)
40
41
PaddleBuf::PaddleBuf (const PaddleBuf& other) { *this = other; }
41
42
42
43
PaddleBuf& PaddleBuf::operator =(const PaddleBuf& other) {
44
+ if (!other.memory_owned_ ) {
45
+ data_ = other.data_ ;
46
+ length_ = other.length_ ;
47
+ memory_owned_ = other.memory_owned_ ;
48
+ } else {
49
+ Resize (other.length ());
50
+ memcpy (data_, other.data (), other.length ());
51
+ length_ = other.length ();
52
+ memory_owned_ = true ;
53
+ }
54
+ return *this ;
55
+ }
56
+
57
+ PaddleBuf& PaddleBuf::operator =(PaddleBuf&& other) {
43
58
// only the buffer with external memory can be copied
44
- assert (!other.memory_owned_ );
45
59
data_ = other.data_ ;
46
60
length_ = other.length_ ;
47
61
memory_owned_ = other.memory_owned_ ;
62
+ other.data_ = nullptr ;
63
+ other.length_ = 0 ;
64
+ other.memory_owned_ = false ;
48
65
return *this ;
49
66
}
50
67
51
68
void PaddleBuf::Resize (size_t length) {
52
69
// Only the owned memory can be reset, the external memory can't be changed.
53
70
if (length_ == length) return ;
54
- assert (memory_owned_);
55
- Free ();
71
+ if (memory_owned_) {
72
+ Free ();
73
+ }
56
74
data_ = new char [length];
57
75
length_ = length;
58
76
memory_owned_ = true ;
@@ -68,7 +86,7 @@ void PaddleBuf::Reset(void* data, size_t length) {
68
86
void PaddleBuf::Free () {
69
87
if (memory_owned_ && data_) {
70
88
assert (length_ > 0 );
71
- delete static_cast <char *>(data_);
89
+ delete[] static_cast <char *>(data_);
72
90
data_ = nullptr ;
73
91
length_ = 0 ;
74
92
}
0 commit comments