From: SUTER Frederic Date: Wed, 27 Oct 2021 08:56:37 +0000 (+0200) Subject: add File::open and File::close in FileSystem plugin X-Git-Tag: v3.30~298 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/52499398fbc12f4d4234cc7c622a0990e3daf094 add File::open and File::close in FileSystem plugin + better match between C and C++ APIs + save some explicit new/delete in user code --- diff --git a/examples/cpp/io-file-remote/s4u-io-file-remote.cpp b/examples/cpp/io-file-remote/s4u-io-file-remote.cpp index f699bc54c0..1ccd0ae0e0 100644 --- a/examples/cpp/io-file-remote/s4u-io-file-remote.cpp +++ b/examples/cpp/io-file-remote/s4u-io-file-remote.cpp @@ -13,25 +13,26 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(remote_io, "Messages specific for this io example") static void host(std::vector args) { - simgrid::s4u::File file(args[1], nullptr); - const char* filename = file.get_path(); + simgrid::s4u::File* file = simgrid::s4u::File::open(args[1], nullptr); + const char* filename = file->get_path(); XBT_INFO("Opened file '%s'", filename); - file.dump(); - XBT_INFO("Try to write %llu MiB to '%s'", file.size() / 1024, filename); - sg_size_t write = file.write(file.size() * 1024); + file->dump(); + XBT_INFO("Try to write %llu MiB to '%s'", file->size() / 1024, filename); + sg_size_t write = file->write(file->size() * 1024); XBT_INFO("Have written %llu MiB to '%s'.", write / (1024 * 1024), filename); if (args.size() > 4) { if (std::stoi(args[4]) != 0) { - XBT_INFO("Move '%s' (of size %llu) from '%s' to '%s'", filename, file.size(), + XBT_INFO("Move '%s' (of size %llu) from '%s' to '%s'", filename, file->size(), simgrid::s4u::Host::current()->get_cname(), args[2].c_str()); - file.remote_move(simgrid::s4u::Host::by_name(args[2]), args[3]); + file->remote_move(simgrid::s4u::Host::by_name(args[2]), args[3]); } else { - XBT_INFO("Copy '%s' (of size %llu) from '%s' to '%s'", filename, file.size(), + XBT_INFO("Copy '%s' (of size %llu) from '%s' to '%s'", filename, file->size(), simgrid::s4u::Host::current()->get_cname(), args[2].c_str()); - file.remote_copy(simgrid::s4u::Host::by_name(args[2]), args[3]); + file->remote_copy(simgrid::s4u::Host::by_name(args[2]), args[3]); } } + file->close(); } int main(int argc, char** argv) diff --git a/examples/cpp/io-file-system/s4u-io-file-system.cpp b/examples/cpp/io-file-system/s4u-io-file-system.cpp index 4157828e2d..1c13eb9fbf 100644 --- a/examples/cpp/io-file-system/s4u-io-file-system.cpp +++ b/examples/cpp/io-file-system/s4u-io-file-system.cpp @@ -32,7 +32,7 @@ public: // Open a non-existing file to create it std::string filename = "/scratch/tmp/data.txt"; - auto* file = new simgrid::s4u::File(filename, nullptr); + auto* file = simgrid::s4u::File::open(filename, nullptr); sg_size_t write = file->write(200000); // Write 200,000 bytes XBT_INFO("Create a %llu bytes file named '%s' on /scratch", write, filename.c_str()); @@ -62,15 +62,15 @@ public: delete file_data; // Close the file - delete file; + file->close(); show_info(disks); // Reopen the file and then unlink it - file = new simgrid::s4u::File("/scratch/tmp/simgrid.readme", nullptr); + file = simgrid::s4u::File::open("/scratch/tmp/simgrid.readme", nullptr); XBT_INFO("Unlink file: '%s'", file->get_path()); file->unlink(); - delete file; // Unlinking the file on "disk" does not free the object + file->close(); // Unlinking the file on "disk" does not close the file and free the object show_info(disks); } diff --git a/include/simgrid/plugins/file_system.h b/include/simgrid/plugins/file_system.h index 49b13a3d40..9b0483e40f 100644 --- a/include/simgrid/plugins/file_system.h +++ b/include/simgrid/plugins/file_system.h @@ -27,7 +27,7 @@ XBT_PUBLIC void sg_storage_file_system_init(); XBT_PUBLIC sg_file_t sg_file_open(const char* fullpath, void* data); XBT_PUBLIC sg_size_t sg_file_read(sg_file_t fd, sg_size_t size); XBT_PUBLIC sg_size_t sg_file_write(sg_file_t fd, sg_size_t size); -XBT_PUBLIC void sg_file_close(const_sg_file_t fd); +XBT_PUBLIC void sg_file_close(sg_file_t fd); XBT_PUBLIC const char* sg_file_get_name(const_sg_file_t fd); XBT_PUBLIC sg_size_t sg_file_get_size(const_sg_file_t fd); @@ -108,6 +108,9 @@ public: File& operator=(const File&) = delete; ~File(); + static File* open(const std::string& fullpath, void* userdata); + void close() { delete this; }; + /** Retrieves the path to the file */ const char* get_path() const { return fullpath_.c_str(); } diff --git a/src/plugins/file_system/s4u_FileSystem.cpp b/src/plugins/file_system/s4u_FileSystem.cpp index 16c19cce46..ad88b620a8 100644 --- a/src/plugins/file_system/s4u_FileSystem.cpp +++ b/src/plugins/file_system/s4u_FileSystem.cpp @@ -111,6 +111,11 @@ File::~File() kernel::actor::simcall([this, desc_table] { desc_table->push_back(this->desc_id); }); } +File* File::open(const std::string& fullpath, void* userdata) +{ + return new File(fullpath, userdata); +} + void File::dump() const { XBT_INFO("File Descriptor information:\n" @@ -471,7 +476,7 @@ void sg_storage_file_system_init() sg_file_t sg_file_open(const char* fullpath, void* data) { - return new simgrid::s4u::File(fullpath, data); + return simgrid::s4u::File::open(fullpath, data); } sg_size_t sg_file_read(sg_file_t fd, sg_size_t size) @@ -484,9 +489,9 @@ sg_size_t sg_file_write(sg_file_t fd, sg_size_t size) return fd->write(size); } -void sg_file_close(const_sg_file_t fd) +void sg_file_close(sg_file_t fd) { - delete fd; + fd->close(); } /** Retrieves the path to the file diff --git a/src/smpi/mpi/smpi_file.cpp b/src/smpi/mpi/smpi_file.cpp index 91561ea5dd..4e45d40871 100644 --- a/src/smpi/mpi/smpi_file.cpp +++ b/src/smpi/mpi/smpi_file.cpp @@ -51,7 +51,7 @@ namespace smpi{ } } - file_= new simgrid::s4u::File(fullname, nullptr); + file_ = simgrid::s4u::File::open(fullname, nullptr); list_=nullptr; if (comm_->rank() == 0) { int size= comm_->size() + FP_SIZE; @@ -80,7 +80,7 @@ namespace smpi{ delete[] list_; } delete win_; - delete file_; + file_->close(); F2C::free_f(this->f2c_id()); if (info_ != MPI_INFO_NULL) simgrid::smpi::Info::unref(info_);