fix share_ptr array (#1786)

Signed-off-by: shengjun.li <shengjun.li@zilliz.com>
This commit is contained in:
shengjun.li 2020-03-28 20:27:07 +08:00 committed by GitHub
parent a3935eed71
commit 52ac810a1f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 39 additions and 51 deletions

View File

@ -48,7 +48,7 @@ struct MXLogBufferHandler {
uint8_t buf_idx;
};
using BufferPtr = std::shared_ptr<char>;
using BufferPtr = std::shared_ptr<char[]>;
class MXLogBuffer {
public:

View File

@ -41,7 +41,8 @@ MXLogFileHandler::Load(char* buf, uint32_t data_offset) {
if (file_size > data_offset) {
read_size = file_size - data_offset;
fseek(p_file_, data_offset, SEEK_SET);
fread(buf, 1, read_size, p_file_);
auto ret = fread(buf, 1, read_size, p_file_);
__glibcxx_assert(ret == read_size);
}
}
return read_size;
@ -56,7 +57,8 @@ MXLogFileHandler::Load(char* buf, uint32_t data_offset, uint32_t data_size) {
}
fseek(p_file_, data_offset, SEEK_SET);
fread(buf, 1, data_size, p_file_);
auto ret = fread(buf, 1, data_size, p_file_);
__glibcxx_assert(ret == data_size);
}
return true;
}

View File

@ -100,8 +100,7 @@ read_index(const std::string& location) {
rp += bin_length;
reader_ptr->seekg(rp);
auto binptr = std::make_shared<uint8_t>();
binptr.reset(bin);
std::shared_ptr<uint8_t[]> binptr(bin);
load_data_list.Append(std::string(meta, meta_length), binptr, bin_length);
delete[] meta;
}

View File

@ -22,7 +22,7 @@ namespace milvus {
namespace knowhere {
struct Binary {
std::shared_ptr<uint8_t> data;
std::shared_ptr<uint8_t[]> data;
int64_t size = 0;
};
using BinaryPtr = std::shared_ptr<Binary>;
@ -47,7 +47,7 @@ class BinarySet {
}
void
Append(const std::string& name, std::shared_ptr<uint8_t> data, int64_t size) {
Append(const std::string& name, std::shared_ptr<uint8_t[]> data, int64_t size) {
auto binary = std::make_shared<Binary>();
binary->data = data;
binary->size = size;

View File

@ -25,8 +25,7 @@ FaissBaseBinaryIndex::SerializeImpl(const IndexType& type) {
MemoryIOWriter writer;
faiss::write_index_binary(index, &writer);
auto data = std::make_shared<uint8_t>();
data.reset(writer.data_);
std::shared_ptr<uint8_t[]> data(writer.data_);
BinarySet res_set;
res_set.Append("BinaryIVF", data, writer.rp);

View File

@ -28,8 +28,7 @@ FaissBaseIndex::SerializeImpl(const IndexType& type) {
MemoryIOWriter writer;
faiss::write_index(index, &writer);
auto data = std::make_shared<uint8_t>();
data.reset(writer.data_);
std::shared_ptr<uint8_t[]> data(writer.data_);
BinarySet res_set;
// TODO(linxj): use virtual func Name() instead of raw string.

View File

@ -35,22 +35,19 @@ IndexAnnoy::Serialize(const Config& config) {
KNOWHERE_THROW_MSG("index not initialize or trained");
}
BinarySet res_set;
auto metric_type_length = metric_type_.length();
uint8_t* p = new uint8_t[metric_type_length];
std::shared_ptr<uint8_t> metric_type(p, [](uint8_t* p) { delete[] p; });
memcpy(p, metric_type_.data(), metric_type_.length());
std::shared_ptr<uint8_t[]> metric_type(new uint8_t[metric_type_length]);
memcpy(metric_type.get(), metric_type_.data(), metric_type_.length());
uint8_t* p_dim = new uint8_t[sizeof(uint64_t)];
std::shared_ptr<uint8_t> dim_data(p_dim, [](uint8_t* p_dim) { delete[] p_dim; });
auto dim = Dim();
memcpy(p_dim, &dim, sizeof(uint64_t));
std::shared_ptr<uint8_t[]> dim_data(new uint8_t[sizeof(uint64_t)]);
memcpy(dim_data.get(), &dim, sizeof(uint64_t));
auto index_length = index_->get_index_length();
uint8_t* q = new uint8_t[index_length];
std::shared_ptr<uint8_t> index_data(q, [](uint8_t* q) { delete[] q; });
memcpy(q, index_->get_index(), (size_t)index_length);
std::shared_ptr<uint8_t[]> index_data(new uint8_t[index_length]);
memcpy(index_data.get(), index_->get_index(), (size_t)index_length);
BinarySet res_set;
res_set.Append("annoy_metric_type", metric_type, metric_type_length);
res_set.Append("annoy_dim", dim_data, sizeof(uint64_t));
res_set.Append("annoy_index_data", index_data, index_length);

View File

@ -45,8 +45,7 @@ IndexHNSW::Serialize(const Config& config) {
try {
MemoryIOWriter writer;
index_->saveIndex(writer);
auto data = std::make_shared<uint8_t>();
data.reset(writer.data_);
std::shared_ptr<uint8_t[]> data(writer.data_);
BinarySet res_set;
res_set.Append("HNSW", data, writer.total);

View File

@ -44,8 +44,7 @@ NSG::Serialize(const Config& config) {
MemoryIOWriter writer;
impl::write_index(index, writer);
auto data = std::make_shared<uint8_t>();
data.reset(writer.data_);
std::shared_ptr<uint8_t[]> data(writer.data_);
BinarySet res_set;
res_set.Append("NSG", data, writer.total);

View File

@ -60,19 +60,19 @@ CPUSPTAGRNG::Serialize(const Config& config) {
snprintf(cstr, length, "%s", index_config.c_str());
BinarySet binary_set;
auto sample = std::make_shared<uint8_t>();
std::shared_ptr<uint8_t[]> sample;
sample.reset(static_cast<uint8_t*>(index_blobs[0].Data()));
auto tree = std::make_shared<uint8_t>();
std::shared_ptr<uint8_t[]> tree;
tree.reset(static_cast<uint8_t*>(index_blobs[1].Data()));
auto graph = std::make_shared<uint8_t>();
std::shared_ptr<uint8_t[]> graph;
graph.reset(static_cast<uint8_t*>(index_blobs[2].Data()));
auto deleteid = std::make_shared<uint8_t>();
std::shared_ptr<uint8_t[]> deleteid;
deleteid.reset(static_cast<uint8_t*>(index_blobs[3].Data()));
auto metadata1 = std::make_shared<uint8_t>();
std::shared_ptr<uint8_t[]> metadata1;
metadata1.reset(static_cast<uint8_t*>(index_blobs[4].Data()));
auto metadata2 = std::make_shared<uint8_t>();
std::shared_ptr<uint8_t[]> metadata2;
metadata2.reset(static_cast<uint8_t*>(index_blobs[5].Data()));
auto x_cfg = std::make_shared<uint8_t>();
std::shared_ptr<uint8_t[]> x_cfg;
x_cfg.reset(static_cast<uint8_t*>((void*)cstr));
binary_set.Append("samples", sample, index_blobs[0].Length());

View File

@ -52,8 +52,7 @@ GPUIDMAP::SerializeImpl(const IndexType& type) {
faiss::write_index(host_index, &writer);
delete host_index;
}
auto data = std::make_shared<uint8_t>();
data.reset(writer.data_);
std::shared_ptr<uint8_t[]> data(writer.data_);
BinarySet res_set;
res_set.Append("IVF", data, writer.rp);

View File

@ -102,8 +102,7 @@ GPUIVF::SerializeImpl(const IndexType& type) {
faiss::write_index(host_index, &writer);
delete host_index;
}
auto data = std::make_shared<uint8_t>();
data.reset(writer.data_);
std::shared_ptr<uint8_t[]> data(writer.data_);
BinarySet res_set;
res_set.Append("IVF", data, writer.rp);

View File

@ -227,8 +227,7 @@ IVFSQHybrid::SerializeImpl(const IndexType& type) {
MemoryIOWriter writer;
faiss::write_index(index_.get(), &writer);
auto data = std::make_shared<uint8_t>();
data.reset(writer.data_);
std::shared_ptr<uint8_t[]> data(writer.data_);
BinarySet res_set;
res_set.Append("IVF", data, writer.rp);

View File

@ -35,7 +35,7 @@ MemoryIOWriter::operator()(const void* ptr, size_t size, size_t nitems) {
total = total_need * magic_num;
auto new_data = new uint8_t[total];
memcpy((void*)new_data, (void*)data_, rp);
delete data_;
delete[] data_;
data_ = new_data;
memcpy((void*)(data_ + rp), ptr, size * nitems);

View File

@ -112,8 +112,7 @@ TEST_P(BinaryIDMAPTest, binaryidmap_serialize) {
serialize(filename, bin, load_data);
binaryset.clear();
auto data = std::make_shared<uint8_t>();
data.reset(load_data);
std::shared_ptr<uint8_t[]> data(load_data);
binaryset.Append("BinaryIVF", data, bin->size);
index_->Load(binaryset);

View File

@ -134,8 +134,7 @@ TEST_P(BinaryIVFTest, binaryivf_serialize) {
serialize(filename, bin, load_data);
binaryset.clear();
auto data = std::make_shared<uint8_t>();
data.reset(load_data);
std::shared_ptr<uint8_t[]> data(load_data);
binaryset.Append("BinaryIVF", data, bin->size);
index_->Load(binaryset);

View File

@ -126,8 +126,7 @@ TEST_F(IDMAPTest, idmap_serialize) {
serialize(filename, bin, load_data);
binaryset.clear();
auto data = std::make_shared<uint8_t>();
data.reset(load_data);
std::shared_ptr<uint8_t[]> data(load_data);
binaryset.Append("IVF", data, bin->size);
index_->Load(binaryset);

View File

@ -165,8 +165,7 @@ TEST_P(IVFTest, ivf_serialize) {
serialize(filename, bin, load_data);
binaryset.clear();
auto data = std::make_shared<uint8_t>();
data.reset(load_data);
std::shared_ptr<uint8_t[]> data(load_data);
binaryset.Append("IVF", data, bin->size);
index_->Load(binaryset);

View File

@ -124,8 +124,7 @@ TEST_P(SPTAGTest, sptag_serialize) {
auto load_data = new uint8_t[bin_size];
reader(load_data, bin_size);
auto data = std::make_shared<uint8_t>();
data.reset(load_data);
std::shared_ptr<uint8_t[]> data(load_data);
load_data_list.Append(meta_list[i].first, data, bin_size);
}

View File

@ -32,11 +32,13 @@ namespace {
void
MakeEmptyTestPath() {
pid_t ret;
if (access(WAL_GTEST_PATH, 0) == 0) {
::system("rm -rf " WAL_GTEST_PATH "*");
ret = ::system("rm -rf " WAL_GTEST_PATH "*");
} else {
::system("mkdir -m 777 -p " WAL_GTEST_PATH);
ret = ::system("mkdir -m 777 -p " WAL_GTEST_PATH);
}
__glibcxx_assert(ret != -1);
}
} // namespace