X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/d8d9b58131bfeed28a5c1458ea2bee892121e3a6..04d3d36e9e87d194bee4f88859f96d66975cce31:/src/plugins/file_system/s4u_FileSystem.cpp diff --git a/src/plugins/file_system/s4u_FileSystem.cpp b/src/plugins/file_system/s4u_FileSystem.cpp index b5460bf847..d62f41723b 100644 --- a/src/plugins/file_system/s4u_FileSystem.cpp +++ b/src/plugins/file_system/s4u_FileSystem.cpp @@ -18,10 +18,12 @@ #include XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_file, "S4U files"); +int sg_storage_max_file_descriptors = 1024; namespace simgrid { namespace s4u { -simgrid::xbt::Extension FileSystemStorageExt::EXTENSION_ID; +simgrid::xbt::Extension FileSystemStorageExt::EXTENSION_ID; +simgrid::xbt::Extension FileDescriptorHostExt::EXTENSION_ID; File::File(std::string fullpath, void* userdata) : File(fullpath, Host::current(), userdata){}; @@ -50,6 +52,16 @@ File::File(std::string fullpath, sg_host_t host, void* userdata) : fullpath_(ful localStorage = st; + // assign a file descriptor id to the newly opened File + FileDescriptorHostExt* ext = host->extension(); + if (ext->file_descriptor_table == nullptr) { + ext->file_descriptor_table = new std::vector(sg_storage_max_file_descriptors); + std::iota(ext->file_descriptor_table->rbegin(), ext->file_descriptor_table->rend(), 0); // Fill with ..., 1, 0. + } + xbt_assert(not ext->file_descriptor_table->empty(), "Too much files are opened! Some have to be closed."); + desc_id = ext->file_descriptor_table->back(); + ext->file_descriptor_table->pop_back(); + XBT_DEBUG("\tOpen file '%s'", path_.c_str()); std::map* content = localStorage->extension()->getContent(); // if file does not exist create an empty file @@ -63,6 +75,11 @@ File::File(std::string fullpath, sg_host_t host, void* userdata) : fullpath_(ful } } +File::~File() +{ + Host::current()->extension()->file_descriptor_table->push_back(desc_id); +} + void File::dump() { XBT_INFO("File Descriptor information:\n" @@ -218,6 +235,7 @@ std::map* FileSystemStorageExt::parseContent(std::string } using simgrid::s4u::FileSystemStorageExt; +using simgrid::s4u::FileDescriptorHostExt; static void onStorageCreation(simgrid::s4u::Storage& st) { @@ -229,18 +247,36 @@ static void onStorageDestruction(simgrid::s4u::Storage& st) delete st.extension(); } +static void onHostCreation(simgrid::s4u::Host& host) +{ + host.extension_set(new FileDescriptorHostExt()); +} + /* **************************** Public interface *************************** */ SG_BEGIN_DECL() void sg_storage_file_system_init() { - if (FileSystemStorageExt::EXTENSION_ID.valid()) - return; + if (not FileSystemStorageExt::EXTENSION_ID.valid()) { + FileSystemStorageExt::EXTENSION_ID = simgrid::s4u::Storage::extension_create(); + simgrid::s4u::Storage::onCreation.connect(&onStorageCreation); + simgrid::s4u::Storage::onDestruction.connect(&onStorageDestruction); + } + + if (not FileDescriptorHostExt::EXTENSION_ID.valid()) { + FileDescriptorHostExt::EXTENSION_ID = simgrid::s4u::Host::extension_create(); + simgrid::s4u::Host::onCreation.connect(&onHostCreation); + } +} - FileSystemStorageExt::EXTENSION_ID = simgrid::s4u::Storage::extension_create(); +sg_file_t sg_file_open(const char* fullpath, void* data) +{ + return new simgrid::s4u::File(fullpath, data); +} - simgrid::s4u::Storage::onCreation.connect(&onStorageCreation); - simgrid::s4u::Storage::onDestruction.connect(&onStorageDestruction); +void sg_file_close(sg_file_t fd) +{ + delete fd; } const char* sg_file_get_name(sg_file_t fd)