|
| 1 | +// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <stdlib.h> |
| 16 | +#include <sstream> |
| 17 | +#include "paddle/fluid/inference/lite/paddle_api.h" |
| 18 | + |
| 19 | +namespace paddle { |
| 20 | + |
| 21 | +int PaddleDtypeSize(PaddleDType dtype) { |
| 22 | + switch (dtype) { |
| 23 | + case PaddleDType::FLOAT32: |
| 24 | + return sizeof(float); |
| 25 | + case PaddleDType::INT64: |
| 26 | + return sizeof(int64_t); |
| 27 | + case PaddleDType::INT32: |
| 28 | + return sizeof(int32_t); |
| 29 | + default: |
| 30 | + assert(false); |
| 31 | + return -1; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +PaddleBuf::PaddleBuf(PaddleBuf &&other) |
| 36 | + : data_(other.data_), |
| 37 | + length_(other.length_), |
| 38 | + memory_owned_(other.memory_owned_) { |
| 39 | + other.memory_owned_ = false; |
| 40 | + other.data_ = nullptr; |
| 41 | + other.length_ = 0; |
| 42 | +} |
| 43 | + |
| 44 | +PaddleBuf::PaddleBuf(const PaddleBuf &other) { *this = other; } |
| 45 | + |
| 46 | +PaddleBuf &PaddleBuf::operator=(const PaddleBuf &other) { |
| 47 | + if (!other.memory_owned_) { |
| 48 | + data_ = other.data_; |
| 49 | + length_ = other.length_; |
| 50 | + memory_owned_ = other.memory_owned_; |
| 51 | + } else { |
| 52 | + Resize(other.length()); |
| 53 | + memcpy(data_, other.data(), other.length()); |
| 54 | + length_ = other.length(); |
| 55 | + memory_owned_ = true; |
| 56 | + } |
| 57 | + return *this; |
| 58 | +} |
| 59 | + |
| 60 | +PaddleBuf &PaddleBuf::operator=(PaddleBuf &&other) { |
| 61 | + // only the buffer with external memory can be copied |
| 62 | + data_ = other.data_; |
| 63 | + length_ = other.length_; |
| 64 | + memory_owned_ = other.memory_owned_; |
| 65 | + other.data_ = nullptr; |
| 66 | + other.length_ = 0; |
| 67 | + other.memory_owned_ = false; |
| 68 | + return *this; |
| 69 | +} |
| 70 | + |
| 71 | +void PaddleBuf::Resize(size_t length) { |
| 72 | + // Only the owned memory can be reset, the external memory can't be changed. |
| 73 | + if (length_ >= length) return; |
| 74 | + if (memory_owned_) { |
| 75 | + Free(); |
| 76 | + data_ = malloc(length); |
| 77 | + length_ = length; |
| 78 | + memory_owned_ = true; |
| 79 | + } else { |
| 80 | + // PADDLE_THROW("The memory is allocated externally, can not Resized"); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +void PaddleBuf::Reset(void *data, size_t length) { |
| 85 | + Free(); |
| 86 | + memory_owned_ = false; |
| 87 | + data_ = data; |
| 88 | + length_ = length; |
| 89 | +} |
| 90 | + |
| 91 | +void PaddleBuf::Free() { |
| 92 | + if (memory_owned_ && data_) { |
| 93 | + // PADDLE_ENFORCE_GT(length_, 0UL); |
| 94 | + free(static_cast<char *>(data_)); |
| 95 | + data_ = nullptr; |
| 96 | + length_ = 0; |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +} // namespace paddle |
0 commit comments