1.1 --- a/Makefile Sun Jan 24 01:29:41 2021 +0100
1.2 +++ b/Makefile Sun Jan 24 19:29:07 2021 +0100
1.3 @@ -36,12 +36,13 @@
1.4 dstest_client.cc memory_utils.cc
1.5
1.6 PLAIN_SRC_CC_dstest_server = \
1.7 - access_map.cc accessor.cc \
1.8 + access_map.cc accessing.cc accessor.cc \
1.9 dstest_server.cc flexpage.cc file_pager.cc \
1.10 ipc.cc memory.cc memory_map.cc memory_utils.cc \
1.11 - page_mapper.cc page_queue.cc pager.cc pages.cc \
1.12 + opener.cc opener_context.cc \
1.13 + page_mapper.cc page_queue.cc pager.cc pages.cc paging.cc \
1.14 region.cc resource.cc simple_pager.cc \
1.15 - test_file_accessor.cc
1.16 + test_file_accessor.cc test_file_opener.cc
1.17
1.18 # Normal definitions.
1.19
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/accessing.cc Sun Jan 24 19:29:07 2021 +0100
2.3 @@ -0,0 +1,41 @@
2.4 +#include "accessing.h"
2.5 +
2.6 +/* Reserve this mapping and return whether 'fileid' is associated with an
2.7 + accessor. */
2.8 +
2.9 +bool Accessing::reserve(fileid_t fileid)
2.10 +{
2.11 + _lock.lock();
2.12 +
2.13 + return _accessors.find(fileid) != _accessors.end();
2.14 +}
2.15 +
2.16 +/* Return the accessor for 'fileid' and release this mapping. */
2.17 +
2.18 +Accessor *Accessing::get(fileid_t fileid)
2.19 +{
2.20 + FileMapping::iterator entry = _accessors.find(fileid);
2.21 + Accessor *accessor;
2.22 +
2.23 + if (entry == _accessors.end())
2.24 + accessor = NULL;
2.25 + else
2.26 + accessor = entry->second;
2.27 +
2.28 + _lock.unlock();
2.29 + return accessor;
2.30 +}
2.31 +
2.32 +/* Associate 'fileid' with the given 'accessor' and release this mapping. */
2.33 +
2.34 +void Accessing::set(fileid_t fileid, Accessor *accessor)
2.35 +{
2.36 + FileMapping::iterator entry = _accessors.find(fileid);
2.37 +
2.38 + if (entry == _accessors.end())
2.39 + _accessors[fileid] = accessor;
2.40 +
2.41 + _lock.unlock();
2.42 +}
2.43 +
2.44 +// vim: tabstop=4 expandtab shiftwidth=4
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/accessing.h Sun Jan 24 19:29:07 2021 +0100
3.3 @@ -0,0 +1,33 @@
3.4 +#pragma once
3.5 +
3.6 +#include <map>
3.7 +#include <mutex>
3.8 +
3.9 +#include "accessor.h"
3.10 +
3.11 +
3.12 +
3.13 +/* Mapping type from file identifiers to accessors. */
3.14 +
3.15 +typedef std::map<fileid_t, Accessor *> FileMapping;
3.16 +typedef std::pair<fileid_t, Accessor *> FileMappingEntry;
3.17 +
3.18 +
3.19 +
3.20 +/* A registry of accessors for files. */
3.21 +
3.22 +class Accessing
3.23 +{
3.24 +protected:
3.25 + FileMapping _accessors;
3.26 + std::mutex _lock;
3.27 +
3.28 +public:
3.29 + bool reserve(fileid_t fileid);
3.30 +
3.31 + Accessor *get(fileid_t fileid);
3.32 +
3.33 + void set(fileid_t fileid, Accessor *accessor);
3.34 +};
3.35 +
3.36 +// vim: tabstop=4 expandtab shiftwidth=4
4.1 --- a/dstest_server.cc Sun Jan 24 01:29:41 2021 +0100
4.2 +++ b/dstest_server.cc Sun Jan 24 19:29:07 2021 +0100
4.3 @@ -28,42 +28,29 @@
4.4
4.5 #include <ipc/server.h>
4.6
4.7 +#include "accessing.h"
4.8 #include "mapped_file_object_server.h"
4.9 -#include "page_mapper.h"
4.10 +#include "memory_utils.h"
4.11 #include "file_pager.h"
4.12 #include "pages.h"
4.13 -#include "test_file_accessor.h"
4.14 +#include "paging.h"
4.15 +#include "test_file_opener.h"
4.16
4.17
4.18
4.19 -/* A component exposing the dataspace and mapped file interfaces. */
4.20 -
4.21 -class MappedFileServer : public FilePager, public MappedFileObject
4.22 -{
4.23 -public:
4.24 - explicit MappedFileServer(fileid_t fileid, PageMapper *mapper)
4.25 - : FilePager(fileid, mapper)
4.26 - {
4.27 - }
4.28 -
4.29 - virtual long map(unsigned long offset, l4_addr_t hot_spot, unsigned long flags, l4_snd_fpage_t *region)
4.30 - { return FilePager::map(offset, hot_spot, flags, region); }
4.31 -
4.32 - virtual long mmap(size_t position, size_t length, size_t *start_pos, size_t *end_pos, size_t *data_end)
4.33 - { return FilePager::mmap(position, length, start_pos, end_pos, data_end); }
4.34 -};
4.35 -
4.36 -
4.37 +const unsigned int MEMORY_PAGES = 10;
4.38 +const unsigned int FILE_PAGES = 20;
4.39
4.40 int main(void)
4.41 {
4.42 - /* Some memory. */
4.43 + /* Some memory plus infrastructure. */
4.44
4.45 - Memory mem(10);
4.46 + Memory mem(MEMORY_PAGES);
4.47 + Accessing accessing;
4.48 + Paging paging;
4.49 Pages pages(&mem);
4.50 - TestFileAccessor a(123UL);
4.51 - PageMapper m(&a, &pages);
4.52 - MappedFileServer obj(123UL, &m);
4.53 + TestFileOpener opener(&accessing, &paging, &pages, page(FILE_PAGES));
4.54 + FilePager *obj = opener.open("123");
4.55
4.56 /* Server capability. */
4.57
4.58 @@ -71,7 +58,7 @@
4.59
4.60 /* Register a server associating it with the given object. */
4.61
4.62 - long err = ipc_server_bind("server", (l4_umword_t) &obj, &server);
4.63 + long err = ipc_server_bind("server", (l4_umword_t) obj, &server);
4.64
4.65 if (err)
4.66 {
4.67 @@ -81,7 +68,7 @@
4.68
4.69 /* Wait for messages, dispatching to the handler. */
4.70
4.71 - ipc_server_loop_for(MappedFileObject, &obj);
4.72 + ipc_server_loop_for(MappedFileObject, obj);
4.73
4.74 return 0;
4.75 }
5.1 --- a/file_pager.h Sun Jan 24 01:29:41 2021 +0100
5.2 +++ b/file_pager.h Sun Jan 24 19:29:07 2021 +0100
5.3 @@ -1,16 +1,20 @@
5.4 #pragma once
5.5
5.6 +#include "mapped_file_object_interface.h"
5.7 #include "pager.h"
5.8
5.9 /* A pager abstraction for a file. */
5.10
5.11 -class FilePager : public Pager
5.12 +class FilePager : public Pager, public MappedFileObject
5.13 {
5.14 public:
5.15 fileid_t fileid;
5.16
5.17 explicit FilePager(fileid_t fileid, PageMapper *mapper);
5.18
5.19 + virtual long map(unsigned long offset, l4_addr_t hot_spot, unsigned long flags, l4_snd_fpage_t *region)
5.20 + { return Pager::map(offset, hot_spot, flags, region); }
5.21 +
5.22 virtual long mmap(size_t position, size_t length, size_t *start_pos, size_t *end_pos, size_t *data_end);
5.23 };
5.24
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
6.2 +++ b/opener.cc Sun Jan 24 19:29:07 2021 +0100
6.3 @@ -0,0 +1,63 @@
6.4 +#include "opener.h"
6.5 +
6.6 +/* Support for providing access to files. */
6.7 +
6.8 +Opener::Opener(Accessing *accessing, Paging *paging, Pages *pages)
6.9 +: _accessing(accessing), _paging(paging), _pages(pages)
6.10 +{
6.11 +}
6.12 +
6.13 +OpenerContext *Opener::context()
6.14 +{
6.15 + return new OpenerContext(this);
6.16 +}
6.17 +
6.18 +FilePager *Opener::open(const char *path)
6.19 +{
6.20 + fileid_t fileid = get_fileid(path);
6.21 +
6.22 + if (fileid == FILEID_INVALID)
6.23 + return NULL;
6.24 +
6.25 + Accessor *accessor = get_accessor(fileid);
6.26 + PageMapper *mapper = get_mapper(accessor);
6.27 + return new FilePager(fileid, mapper);
6.28 +}
6.29 +
6.30 +/* Obtain an accessor for the 'fileid' or register a new one in the accessing
6.31 + object. */
6.32 +
6.33 +Accessor *Opener::get_accessor(fileid_t fileid)
6.34 +{
6.35 + Accessor *accessor;
6.36 +
6.37 + if (_accessing->reserve(fileid))
6.38 + accessor = _accessing->get(fileid);
6.39 + else
6.40 + {
6.41 + accessor = make_accessor(fileid);
6.42 + _accessing->set(fileid, accessor);
6.43 + }
6.44 +
6.45 + return accessor;
6.46 +}
6.47 +
6.48 +/* Obtain a page mapper for the 'accessor' or register a new one in the
6.49 + paging object. */
6.50 +
6.51 +PageMapper *Opener::get_mapper(Accessor *accessor)
6.52 +{
6.53 + PageMapper *mapper;
6.54 +
6.55 + if (_paging->reserve(accessor))
6.56 + mapper = _paging->get(accessor);
6.57 + else
6.58 + {
6.59 + mapper = new PageMapper(accessor, _pages);
6.60 + _paging->set(accessor, mapper);
6.61 + }
6.62 +
6.63 + return mapper;
6.64 +}
6.65 +
6.66 +// vim: tabstop=4 expandtab shiftwidth=4
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
7.2 +++ b/opener.h Sun Jan 24 19:29:07 2021 +0100
7.3 @@ -0,0 +1,39 @@
7.4 +#pragma once
7.5 +
7.6 +#include "accessing.h"
7.7 +#include "file_pager.h"
7.8 +#include "opener_context.h"
7.9 +#include "pager.h"
7.10 +#include "paging.h"
7.11 +#include "pages.h"
7.12 +
7.13 +/* Support for providing access to files. */
7.14 +
7.15 +class Opener
7.16 +{
7.17 +protected:
7.18 + Accessing *_accessing;
7.19 + Paging *_paging;
7.20 + Pages *_pages;
7.21 +
7.22 + /* Convenience methods. */
7.23 +
7.24 + Accessor *get_accessor(fileid_t fileid);
7.25 +
7.26 + PageMapper *get_mapper(Accessor *accessor);
7.27 +
7.28 + /* Configurable methods. */
7.29 +
7.30 + virtual fileid_t get_fileid(const char *path) = 0;
7.31 +
7.32 + virtual Accessor *make_accessor(fileid_t fileid) = 0;
7.33 +
7.34 +public:
7.35 + explicit Opener(Accessing *accessing, Paging *paging, Pages *pages);
7.36 +
7.37 + OpenerContext *context();
7.38 +
7.39 + FilePager *open(const char *path);
7.40 +};
7.41 +
7.42 +// vim: tabstop=4 expandtab shiftwidth=4
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
8.2 +++ b/opener_context.cc Sun Jan 24 19:29:07 2021 +0100
8.3 @@ -0,0 +1,21 @@
8.4 +#include "opener.h"
8.5 +#include "opener_context.h"
8.6 +
8.7 +/* Support for providing access to files. */
8.8 +
8.9 +OpenerContext::OpenerContext(Opener *opener, Memory *memory)
8.10 +: SimplePager(memory), _opener(opener)
8.11 +{
8.12 +}
8.13 +
8.14 +FilePager *OpenerContext::open()
8.15 +{
8.16 + return _opener->open(get_path());
8.17 +}
8.18 +
8.19 +char *OpenerContext::get_path()
8.20 +{
8.21 + return _region->read();
8.22 +}
8.23 +
8.24 +// vim: tabstop=4 expandtab shiftwidth=4
9.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
9.2 +++ b/opener_context.h Sun Jan 24 19:29:07 2021 +0100
9.3 @@ -0,0 +1,29 @@
9.4 +#pragma once
9.5 +
9.6 +#include "file_pager.h"
9.7 +#include "simple_pager.h"
9.8 +
9.9 +
9.10 +
9.11 +/* Forward declaration. */
9.12 +
9.13 +class Opener;
9.14 +
9.15 +
9.16 +
9.17 +/* Support for indicating files to be opened. */
9.18 +
9.19 +class OpenerContext : public SimplePager
9.20 +{
9.21 +protected:
9.22 + Opener *_opener;
9.23 +
9.24 +public:
9.25 + explicit OpenerContext(Opener *opener, Memory *memory=NULL);
9.26 +
9.27 + FilePager *open();
9.28 +
9.29 + char *get_path();
9.30 +};
9.31 +
9.32 +// vim: tabstop=4 expandtab shiftwidth=4
10.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
10.2 +++ b/paging.cc Sun Jan 24 19:29:07 2021 +0100
10.3 @@ -0,0 +1,34 @@
10.4 +#include "paging.h"
10.5 +
10.6 +bool Paging::reserve(Accessor *accessor)
10.7 +{
10.8 + _lock.lock();
10.9 +
10.10 + return _mappers.find(accessor) != _mappers.end();
10.11 +}
10.12 +
10.13 +PageMapper *Paging::get(Accessor *accessor)
10.14 +{
10.15 + AccessorMapping::iterator entry = _mappers.find(accessor);
10.16 + PageMapper *mapper;
10.17 +
10.18 + if (entry == _mappers.end())
10.19 + mapper = NULL;
10.20 + else
10.21 + mapper = entry->second;
10.22 +
10.23 + _lock.unlock();
10.24 + return mapper;
10.25 +}
10.26 +
10.27 +void Paging::set(Accessor *accessor, PageMapper *mapper)
10.28 +{
10.29 + AccessorMapping::iterator entry = _mappers.find(accessor);
10.30 +
10.31 + if (entry == _mappers.end())
10.32 + _mappers[accessor] = mapper;
10.33 +
10.34 + _lock.unlock();
10.35 +}
10.36 +
10.37 +// vim: tabstop=4 expandtab shiftwidth=4
11.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
11.2 +++ b/paging.h Sun Jan 24 19:29:07 2021 +0100
11.3 @@ -0,0 +1,34 @@
11.4 +#pragma once
11.5 +
11.6 +#include <map>
11.7 +#include <mutex>
11.8 +
11.9 +#include "accessor.h"
11.10 +#include "page_mapper.h"
11.11 +
11.12 +
11.13 +
11.14 +/* Mapping type from accessors to page mappers. */
11.15 +
11.16 +typedef std::map<Accessor *, PageMapper *> AccessorMapping;
11.17 +typedef std::pair<Accessor *, PageMapper *> AccessorMappingEntry;
11.18 +
11.19 +
11.20 +
11.21 +/* A registry of mappers for accessors. */
11.22 +
11.23 +class Paging
11.24 +{
11.25 +protected:
11.26 + AccessorMapping _mappers;
11.27 + std::mutex _lock;
11.28 +
11.29 +public:
11.30 + bool reserve(Accessor *accessor);
11.31 +
11.32 + PageMapper *get(Accessor *accessor);
11.33 +
11.34 + void set(Accessor *accessor, PageMapper *mapper);
11.35 +};
11.36 +
11.37 +// vim: tabstop=4 expandtab shiftwidth=4
12.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
12.2 +++ b/test_file_opener.cc Sun Jan 24 19:29:07 2021 +0100
12.3 @@ -0,0 +1,30 @@
12.4 +#include "test_file_accessor.h"
12.5 +#include "test_file_opener.h"
12.6 +
12.7 +#include <stdlib.h>
12.8 +
12.9 +/* Support for providing access to files. */
12.10 +
12.11 +TestFileOpener::TestFileOpener(Accessing *accessing, Paging *paging, Pages *pages,
12.12 + offset_t file_size)
12.13 +: Opener(accessing, paging, pages), _file_size(file_size)
12.14 +{
12.15 +}
12.16 +
12.17 +/* Return a file identifier for the given 'path'. */
12.18 +
12.19 +fileid_t TestFileOpener::get_fileid(const char *path)
12.20 +{
12.21 + /* NOTE: Just convert the path to a number. */
12.22 +
12.23 + return atol(path);
12.24 +}
12.25 +
12.26 +/* Return a new accessor for 'fileid'. */
12.27 +
12.28 +Accessor *TestFileOpener::make_accessor(fileid_t fileid)
12.29 +{
12.30 + return new TestFileAccessor(fileid, _file_size);
12.31 +}
12.32 +
12.33 +// vim: tabstop=4 expandtab shiftwidth=4
13.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
13.2 +++ b/test_file_opener.h Sun Jan 24 19:29:07 2021 +0100
13.3 @@ -0,0 +1,23 @@
13.4 +#pragma once
13.5 +
13.6 +#include "opener.h"
13.7 +
13.8 +/* Support for providing access to files. */
13.9 +
13.10 +class TestFileOpener : public Opener
13.11 +{
13.12 +protected:
13.13 + offset_t _file_size;
13.14 +
13.15 + /* Configurable methods. */
13.16 +
13.17 + virtual fileid_t get_fileid(const char *path);
13.18 +
13.19 + virtual Accessor *make_accessor(fileid_t fileid);
13.20 +
13.21 +public:
13.22 + explicit TestFileOpener(Accessing *accessing, Paging *paging, Pages *pages,
13.23 + offset_t file_size=0);
13.24 +};
13.25 +
13.26 +// vim: tabstop=4 expandtab shiftwidth=4