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