paul@93 | 1 | /* |
paul@93 | 2 | * General functionality supporting file paging. |
paul@93 | 3 | * |
paul@93 | 4 | * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk> |
paul@93 | 5 | * |
paul@93 | 6 | * This program is free software; you can redistribute it and/or |
paul@93 | 7 | * modify it under the terms of the GNU General Public License as |
paul@93 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@93 | 9 | * the License, or (at your option) any later version. |
paul@93 | 10 | * |
paul@93 | 11 | * This program is distributed in the hope that it will be useful, |
paul@93 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@93 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@93 | 14 | * GNU General Public License for more details. |
paul@93 | 15 | * |
paul@93 | 16 | * You should have received a copy of the GNU General Public License |
paul@93 | 17 | * along with this program; if not, write to the Free Software |
paul@93 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@93 | 19 | * Boston, MA 02110-1301, USA |
paul@93 | 20 | */ |
paul@93 | 21 | |
paul@79 | 22 | #include "file_pager.h" |
paul@81 | 23 | #include "file_paging.h" |
paul@9 | 24 | |
paul@85 | 25 | #include <systypes/fcntl.h> |
paul@85 | 26 | |
paul@79 | 27 | |
paul@79 | 28 | |
paul@81 | 29 | FilePaging::FilePaging(Pages *pages) |
paul@79 | 30 | : _pages(pages) |
paul@9 | 31 | { |
paul@79 | 32 | } |
paul@9 | 33 | |
paul@85 | 34 | |
paul@85 | 35 | |
paul@79 | 36 | /* Return any registered page mapper for the given 'fileid' or NULL if no such |
paul@79 | 37 | mapper is registered. */ |
paul@9 | 38 | |
paul@81 | 39 | PageMapper *FilePaging::get(fileid_t fileid) |
paul@9 | 40 | { |
paul@78 | 41 | FileMapping::iterator entry = _mappers.find(fileid); |
paul@9 | 42 | PageMapper *mapper; |
paul@9 | 43 | |
paul@9 | 44 | if (entry == _mappers.end()) |
paul@9 | 45 | mapper = NULL; |
paul@9 | 46 | else |
paul@9 | 47 | mapper = entry->second; |
paul@9 | 48 | |
paul@9 | 49 | return mapper; |
paul@9 | 50 | } |
paul@9 | 51 | |
paul@79 | 52 | /* Register a page 'mapper' for the given 'fileid'. */ |
paul@79 | 53 | |
paul@81 | 54 | void FilePaging::set(fileid_t fileid, PageMapper *mapper) |
paul@9 | 55 | { |
paul@78 | 56 | FileMapping::iterator entry = _mappers.find(fileid); |
paul@9 | 57 | |
paul@9 | 58 | if (entry == _mappers.end()) |
paul@78 | 59 | _mappers[fileid] = mapper; |
paul@79 | 60 | } |
paul@9 | 61 | |
paul@85 | 62 | |
paul@85 | 63 | |
paul@87 | 64 | /* Convert opening flags to map-compatible paging flags. */ |
paul@85 | 65 | |
paul@85 | 66 | flags_t FilePaging::get_flags(flags_t flags) |
paul@85 | 67 | { |
paul@87 | 68 | return flags & (O_WRONLY | O_RDWR) ? L4RE_DS_MAP_FLAG_RW : L4RE_DS_MAP_FLAG_RO; |
paul@85 | 69 | } |
paul@85 | 70 | |
paul@85 | 71 | |
paul@85 | 72 | |
paul@79 | 73 | /* Obtain a page mapper for the 'fileid' or register a new one in the |
paul@79 | 74 | paging object. */ |
paul@79 | 75 | |
paul@81 | 76 | PageMapper *FilePaging::get_mapper(fileid_t fileid) |
paul@79 | 77 | { |
paul@79 | 78 | /* Obtain any registered page mapper. */ |
paul@79 | 79 | |
paul@79 | 80 | PageMapper *mapper = get(fileid); |
paul@79 | 81 | |
paul@79 | 82 | if (mapper != NULL) |
paul@79 | 83 | return mapper; |
paul@79 | 84 | |
paul@79 | 85 | /* Make an accessor and page mapper, registering the mapper. */ |
paul@79 | 86 | |
paul@79 | 87 | Accessor *accessor = make_accessor(fileid); |
paul@79 | 88 | mapper = new PageMapper(accessor, _pages); |
paul@79 | 89 | |
paul@79 | 90 | set(fileid, mapper); |
paul@79 | 91 | |
paul@79 | 92 | return mapper; |
paul@79 | 93 | } |
paul@79 | 94 | |
paul@79 | 95 | |
paul@79 | 96 | |
paul@79 | 97 | /* Return a pager initialised with a page mapper. */ |
paul@79 | 98 | |
paul@81 | 99 | Pager *FilePaging::get_pager(fileid_t fileid, flags_t flags) |
paul@79 | 100 | { |
paul@79 | 101 | std::lock_guard<std::mutex> guard(_lock); |
paul@79 | 102 | |
paul@79 | 103 | /* Initialise the pager with the mapper and a reference to this object for |
paul@79 | 104 | closing the mapper and accessor. */ |
paul@79 | 105 | |
paul@79 | 106 | PageMapper *mapper = get_mapper(fileid); |
paul@79 | 107 | return new FilePager(fileid, mapper, flags, this); |
paul@79 | 108 | } |
paul@79 | 109 | |
paul@79 | 110 | /* Detach a pager, potentially removing its resources. */ |
paul@79 | 111 | |
paul@81 | 112 | void FilePaging::detach_pager(fileid_t fileid, PageMapper *mapper) |
paul@79 | 113 | { |
paul@79 | 114 | std::lock_guard<std::mutex> guard(_lock); |
paul@79 | 115 | |
paul@79 | 116 | if (!mapper->detach()) |
paul@79 | 117 | { |
paul@79 | 118 | _mappers.erase(fileid); |
paul@79 | 119 | delete mapper->accessor(); |
paul@79 | 120 | delete mapper; |
paul@79 | 121 | } |
paul@9 | 122 | } |
paul@9 | 123 | |
paul@9 | 124 | // vim: tabstop=4 expandtab shiftwidth=4 |