1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/files/file_paging.cc Thu Apr 01 00:57:43 2021 +0200
1.3 @@ -0,0 +1,88 @@
1.4 +#include "file_pager.h"
1.5 +#include "file_paging.h"
1.6 +
1.7 +
1.8 +
1.9 +FilePaging::FilePaging(Pages *pages)
1.10 +: _pages(pages)
1.11 +{
1.12 +}
1.13 +
1.14 +/* Return any registered page mapper for the given 'fileid' or NULL if no such
1.15 + mapper is registered. */
1.16 +
1.17 +PageMapper *FilePaging::get(fileid_t fileid)
1.18 +{
1.19 + FileMapping::iterator entry = _mappers.find(fileid);
1.20 + PageMapper *mapper;
1.21 +
1.22 + if (entry == _mappers.end())
1.23 + mapper = NULL;
1.24 + else
1.25 + mapper = entry->second;
1.26 +
1.27 + return mapper;
1.28 +}
1.29 +
1.30 +/* Register a page 'mapper' for the given 'fileid'. */
1.31 +
1.32 +void FilePaging::set(fileid_t fileid, PageMapper *mapper)
1.33 +{
1.34 + FileMapping::iterator entry = _mappers.find(fileid);
1.35 +
1.36 + if (entry == _mappers.end())
1.37 + _mappers[fileid] = mapper;
1.38 +}
1.39 +
1.40 +/* Obtain a page mapper for the 'fileid' or register a new one in the
1.41 + paging object. */
1.42 +
1.43 +PageMapper *FilePaging::get_mapper(fileid_t fileid)
1.44 +{
1.45 + /* Obtain any registered page mapper. */
1.46 +
1.47 + PageMapper *mapper = get(fileid);
1.48 +
1.49 + if (mapper != NULL)
1.50 + return mapper;
1.51 +
1.52 + /* Make an accessor and page mapper, registering the mapper. */
1.53 +
1.54 + Accessor *accessor = make_accessor(fileid);
1.55 + mapper = new PageMapper(accessor, _pages);
1.56 +
1.57 + set(fileid, mapper);
1.58 +
1.59 + return mapper;
1.60 +}
1.61 +
1.62 +
1.63 +
1.64 +/* Return a pager initialised with a page mapper. */
1.65 +
1.66 +Pager *FilePaging::get_pager(fileid_t fileid, flags_t flags)
1.67 +{
1.68 + std::lock_guard<std::mutex> guard(_lock);
1.69 +
1.70 + /* Initialise the pager with the mapper and a reference to this object for
1.71 + closing the mapper and accessor. */
1.72 +
1.73 + PageMapper *mapper = get_mapper(fileid);
1.74 + return new FilePager(fileid, mapper, flags, this);
1.75 +}
1.76 +
1.77 +/* Detach a pager, potentially removing its resources. */
1.78 +
1.79 +void FilePaging::detach_pager(fileid_t fileid, PageMapper *mapper)
1.80 +{
1.81 + std::lock_guard<std::mutex> guard(_lock);
1.82 +
1.83 + if (!mapper->detach())
1.84 + {
1.85 + _mappers.erase(fileid);
1.86 + delete mapper->accessor();
1.87 + delete mapper;
1.88 + }
1.89 +}
1.90 +
1.91 +// vim: tabstop=4 expandtab shiftwidth=4