paul@93 | 1 | /* |
paul@93 | 2 | * File-specific pager functionality. |
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@8 | 22 | #include "file_pager.h" |
paul@10 | 23 | #include "mapped_file_object_server.h" |
paul@8 | 24 | |
paul@93 | 25 | |
paul@93 | 26 | |
paul@200 | 27 | /* Initialise a pager for a file with a unique file identifier, file provider, |
paul@202 | 28 | mapping flags and a file registry. The provider offers a shared page mapper |
paul@202 | 29 | for moderating access to loaded pages. */ |
paul@8 | 30 | |
paul@222 | 31 | FilePager::FilePager(fileid_t fileid, FileProvider *provider, map_flags_t flags) |
paul@200 | 32 | : Pager(provider->mapper(), flags), |
paul@222 | 33 | _provider(provider), fileid(fileid) |
paul@8 | 34 | { |
paul@8 | 35 | } |
paul@8 | 36 | |
paul@10 | 37 | int FilePager::expected_items() |
paul@10 | 38 | { |
paul@10 | 39 | return MappedFileObject_expected_items; |
paul@10 | 40 | } |
paul@10 | 41 | |
paul@10 | 42 | ipc_server_handler_type FilePager::handler() |
paul@10 | 43 | { |
paul@10 | 44 | return (ipc_server_handler_type) handle_MappedFileObject; |
paul@10 | 45 | } |
paul@10 | 46 | |
paul@78 | 47 | |
paul@78 | 48 | |
paul@201 | 49 | /* Close the pager, removing the provider from the registry if appropriate. */ |
paul@79 | 50 | |
paul@79 | 51 | void FilePager::close() |
paul@79 | 52 | { |
paul@141 | 53 | /* Notify other users of the file. */ |
paul@141 | 54 | |
paul@200 | 55 | _provider->notify_others(_endpoint, NOTIFY_PEER_CLOSED); |
paul@193 | 56 | |
paul@200 | 57 | /* Detach the pager, potentially removing the file provider. */ |
paul@200 | 58 | |
paul@222 | 59 | _provider->registry()->detach(fileid, _provider); |
paul@79 | 60 | } |
paul@79 | 61 | |
paul@79 | 62 | |
paul@79 | 63 | |
paul@78 | 64 | /* File-specific operations. */ |
paul@78 | 65 | |
paul@53 | 66 | long FilePager::flush(offset_t populated_size, offset_t *size) |
paul@53 | 67 | { |
paul@141 | 68 | long err = Pager::flush(populated_size, size); |
paul@141 | 69 | |
paul@141 | 70 | if (_resized) |
paul@141 | 71 | { |
paul@200 | 72 | _provider->notify_others(_endpoint, NOTIFY_CONTENT_AVAILABLE); |
paul@141 | 73 | _resized = false; |
paul@141 | 74 | } |
paul@141 | 75 | |
paul@141 | 76 | return err; |
paul@53 | 77 | } |
paul@53 | 78 | |
paul@55 | 79 | long FilePager::resize(offset_t *size) |
paul@55 | 80 | { |
paul@141 | 81 | offset_t old_size = get_data_size(); |
paul@141 | 82 | long err = Pager::resize(size); |
paul@141 | 83 | |
paul@141 | 84 | /* If the size has changed, notify other users, but only after data has been |
paul@141 | 85 | flushed. */ |
paul@141 | 86 | |
paul@141 | 87 | if (old_size < get_data_size()) |
paul@141 | 88 | _resized = true; |
paul@141 | 89 | |
paul@141 | 90 | return err; |
paul@55 | 91 | } |
paul@55 | 92 | |
paul@90 | 93 | long FilePager::mmap(offset_t position, offset_t length, offset_t *start_pos, |
paul@90 | 94 | offset_t *end_pos, offset_t *size) |
paul@8 | 95 | { |
paul@8 | 96 | /* Set the limits of the paged region. */ |
paul@8 | 97 | |
paul@90 | 98 | return Pager::mmap(position, length, start_pos, end_pos, size); |
paul@8 | 99 | } |
paul@8 | 100 | |
paul@78 | 101 | |
paul@78 | 102 | |
paul@78 | 103 | /* Generic pager operations. */ |
paul@78 | 104 | |
paul@121 | 105 | long FilePager::map(offset_t offset, address_t hot_spot, map_flags_t flags, l4_snd_fpage_t *region) |
paul@16 | 106 | { |
paul@16 | 107 | return Pager::map(offset, hot_spot, flags, region); |
paul@16 | 108 | } |
paul@16 | 109 | |
paul@141 | 110 | |
paul@141 | 111 | |
paul@141 | 112 | /* Subscribe to notifications. */ |
paul@141 | 113 | |
paul@193 | 114 | long FilePager::subscribe(l4_cap_idx_t endpoint, notify_flags_t flags) |
paul@141 | 115 | { |
paul@206 | 116 | /* Readers can subscribe to new data (flush) and file closed events. |
paul@206 | 117 | Writers can subscribe to file closed events. */ |
paul@141 | 118 | |
paul@200 | 119 | _endpoint = _provider->subscribe(endpoint, flags); |
paul@141 | 120 | return L4_EOK; |
paul@141 | 121 | } |
paul@141 | 122 | |
paul@193 | 123 | long FilePager::unsubscribe(l4_cap_idx_t endpoint) |
paul@141 | 124 | { |
paul@200 | 125 | _provider->unsubscribe(_endpoint, endpoint); |
paul@141 | 126 | return L4_EOK; |
paul@141 | 127 | } |
paul@141 | 128 | |
paul@8 | 129 | // vim: tabstop=4 expandtab shiftwidth=4 |