Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add File::open and File::close in FileSystem plugin
authorSUTER Frederic <frederic.suter@cc.in2p3.fr>
Wed, 27 Oct 2021 08:56:37 +0000 (10:56 +0200)
committerSUTER Frederic <frederic.suter@cc.in2p3.fr>
Wed, 27 Oct 2021 08:56:37 +0000 (10:56 +0200)
+ better match between C and C++ APIs
+ save some explicit new/delete in user code

examples/cpp/io-file-remote/s4u-io-file-remote.cpp
examples/cpp/io-file-system/s4u-io-file-system.cpp
include/simgrid/plugins/file_system.h
src/plugins/file_system/s4u_FileSystem.cpp
src/smpi/mpi/smpi_file.cpp

index f699bc5..1ccd0ae 100644 (file)
@@ -13,25 +13,26 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(remote_io, "Messages specific for this io example")
 
 static void host(std::vector<std::string> 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)
index 4157828..1c13eb9 100644 (file)
@@ -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);
   }
index 49b13a3..9b0483e 100644 (file)
@@ -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(); }
 
index 16c19cc..ad88b62 100644 (file)
@@ -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
index 91561ea..4e45d40 100644 (file)
@@ -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_);