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