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@94 | 34 | FilePaging::~FilePaging() |
paul@94 | 35 | { |
paul@94 | 36 | } |
paul@94 | 37 | |
paul@85 | 38 | |
paul@85 | 39 | |
paul@79 | 40 | /* Return any registered page mapper for the given 'fileid' or NULL if no such |
paul@79 | 41 | mapper is registered. */ |
paul@9 | 42 | |
paul@81 | 43 | PageMapper *FilePaging::get(fileid_t fileid) |
paul@9 | 44 | { |
paul@78 | 45 | FileMapping::iterator entry = _mappers.find(fileid); |
paul@9 | 46 | PageMapper *mapper; |
paul@9 | 47 | |
paul@9 | 48 | if (entry == _mappers.end()) |
paul@9 | 49 | mapper = NULL; |
paul@9 | 50 | else |
paul@9 | 51 | mapper = entry->second; |
paul@9 | 52 | |
paul@9 | 53 | return mapper; |
paul@9 | 54 | } |
paul@9 | 55 | |
paul@79 | 56 | /* Register a page 'mapper' for the given 'fileid'. */ |
paul@79 | 57 | |
paul@81 | 58 | void FilePaging::set(fileid_t fileid, PageMapper *mapper) |
paul@9 | 59 | { |
paul@78 | 60 | FileMapping::iterator entry = _mappers.find(fileid); |
paul@9 | 61 | |
paul@9 | 62 | if (entry == _mappers.end()) |
paul@78 | 63 | _mappers[fileid] = mapper; |
paul@79 | 64 | } |
paul@9 | 65 | |
paul@85 | 66 | |
paul@85 | 67 | |
paul@87 | 68 | /* Convert opening flags to map-compatible paging flags. */ |
paul@85 | 69 | |
paul@85 | 70 | flags_t FilePaging::get_flags(flags_t flags) |
paul@85 | 71 | { |
paul@87 | 72 | return flags & (O_WRONLY | O_RDWR) ? L4RE_DS_MAP_FLAG_RW : L4RE_DS_MAP_FLAG_RO; |
paul@85 | 73 | } |
paul@85 | 74 | |
paul@85 | 75 | |
paul@85 | 76 | |
paul@79 | 77 | /* Obtain a page mapper for the 'fileid' or register a new one in the |
paul@79 | 78 | paging object. */ |
paul@79 | 79 | |
paul@81 | 80 | PageMapper *FilePaging::get_mapper(fileid_t fileid) |
paul@79 | 81 | { |
paul@79 | 82 | /* Obtain any registered page mapper. */ |
paul@79 | 83 | |
paul@79 | 84 | PageMapper *mapper = get(fileid); |
paul@79 | 85 | |
paul@79 | 86 | if (mapper != NULL) |
paul@79 | 87 | return mapper; |
paul@79 | 88 | |
paul@79 | 89 | /* Make an accessor and page mapper, registering the mapper. */ |
paul@79 | 90 | |
paul@79 | 91 | Accessor *accessor = make_accessor(fileid); |
paul@79 | 92 | mapper = new PageMapper(accessor, _pages); |
paul@79 | 93 | |
paul@79 | 94 | set(fileid, mapper); |
paul@79 | 95 | |
paul@79 | 96 | return mapper; |
paul@79 | 97 | } |
paul@79 | 98 | |
paul@79 | 99 | |
paul@79 | 100 | |
paul@79 | 101 | /* Return a pager initialised with a page mapper. */ |
paul@79 | 102 | |
paul@81 | 103 | Pager *FilePaging::get_pager(fileid_t fileid, flags_t flags) |
paul@79 | 104 | { |
paul@79 | 105 | std::lock_guard<std::mutex> guard(_lock); |
paul@79 | 106 | |
paul@79 | 107 | /* Initialise the pager with the mapper and a reference to this object for |
paul@79 | 108 | closing the mapper and accessor. */ |
paul@79 | 109 | |
paul@79 | 110 | PageMapper *mapper = get_mapper(fileid); |
paul@79 | 111 | return new FilePager(fileid, mapper, flags, this); |
paul@79 | 112 | } |
paul@79 | 113 | |
paul@79 | 114 | /* Detach a pager, potentially removing its resources. */ |
paul@79 | 115 | |
paul@81 | 116 | void FilePaging::detach_pager(fileid_t fileid, PageMapper *mapper) |
paul@79 | 117 | { |
paul@79 | 118 | std::lock_guard<std::mutex> guard(_lock); |
paul@79 | 119 | |
paul@79 | 120 | if (!mapper->detach()) |
paul@79 | 121 | { |
paul@79 | 122 | _mappers.erase(fileid); |
paul@79 | 123 | delete mapper->accessor(); |
paul@79 | 124 | delete mapper; |
paul@79 | 125 | } |
paul@9 | 126 | } |
paul@9 | 127 | |
paul@9 | 128 | // vim: tabstop=4 expandtab shiftwidth=4 |