1 #include "file_pager.h" 2 #include "file_paging.h" 3 4 #include <systypes/fcntl.h> 5 6 7 8 FilePaging::FilePaging(Pages *pages) 9 : _pages(pages) 10 { 11 } 12 13 14 15 /* Return any registered page mapper for the given 'fileid' or NULL if no such 16 mapper is registered. */ 17 18 PageMapper *FilePaging::get(fileid_t fileid) 19 { 20 FileMapping::iterator entry = _mappers.find(fileid); 21 PageMapper *mapper; 22 23 if (entry == _mappers.end()) 24 mapper = NULL; 25 else 26 mapper = entry->second; 27 28 return mapper; 29 } 30 31 /* Register a page 'mapper' for the given 'fileid'. */ 32 33 void FilePaging::set(fileid_t fileid, PageMapper *mapper) 34 { 35 FileMapping::iterator entry = _mappers.find(fileid); 36 37 if (entry == _mappers.end()) 38 _mappers[fileid] = mapper; 39 } 40 41 42 43 /* Convert opening flags to paging flags. */ 44 45 flags_t FilePaging::get_flags(flags_t flags) 46 { 47 return flags & (O_WRONLY | O_RDWR) ? L4_FPAGE_RW : L4_FPAGE_RO; 48 } 49 50 51 52 /* Obtain a page mapper for the 'fileid' or register a new one in the 53 paging object. */ 54 55 PageMapper *FilePaging::get_mapper(fileid_t fileid) 56 { 57 /* Obtain any registered page mapper. */ 58 59 PageMapper *mapper = get(fileid); 60 61 if (mapper != NULL) 62 return mapper; 63 64 /* Make an accessor and page mapper, registering the mapper. */ 65 66 Accessor *accessor = make_accessor(fileid); 67 mapper = new PageMapper(accessor, _pages); 68 69 set(fileid, mapper); 70 71 return mapper; 72 } 73 74 75 76 /* Return a pager initialised with a page mapper. */ 77 78 Pager *FilePaging::get_pager(fileid_t fileid, flags_t flags) 79 { 80 std::lock_guard<std::mutex> guard(_lock); 81 82 /* Initialise the pager with the mapper and a reference to this object for 83 closing the mapper and accessor. */ 84 85 PageMapper *mapper = get_mapper(fileid); 86 return new FilePager(fileid, mapper, flags, this); 87 } 88 89 /* Detach a pager, potentially removing its resources. */ 90 91 void FilePaging::detach_pager(fileid_t fileid, PageMapper *mapper) 92 { 93 std::lock_guard<std::mutex> guard(_lock); 94 95 if (!mapper->detach()) 96 { 97 _mappers.erase(fileid); 98 delete mapper->accessor(); 99 delete mapper; 100 } 101 } 102 103 // vim: tabstop=4 expandtab shiftwidth=4