2011-04-06 15:02:31 +02:00
|
|
|
/*****************************************************************************
|
2012-02-02 02:53:35 +01:00
|
|
|
*
|
2011-04-06 15:02:31 +02:00
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2021-01-05 15:39:07 +01:00
|
|
|
* Copyright (C) 2021 Artem Pavlenko
|
2011-04-06 15:02:31 +02:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2015-10-16 22:34:53 +02:00
|
|
|
#if defined(MAPNIK_MEMORY_MAPPED_FILE)
|
2013-08-13 20:48:04 +02:00
|
|
|
|
2011-04-06 15:02:31 +02:00
|
|
|
// mapnik
|
2012-04-10 00:51:04 +02:00
|
|
|
#include <mapnik/debug.hpp>
|
2013-06-03 04:28:24 +02:00
|
|
|
#include <mapnik/util/fs.hpp>
|
2011-04-06 15:02:31 +02:00
|
|
|
#include <mapnik/mapped_memory_cache.hpp>
|
|
|
|
|
2020-11-19 15:30:30 +01:00
|
|
|
#include <mapnik/warning.hpp>
|
|
|
|
MAPNIK_DISABLE_WARNING_PUSH
|
2015-11-08 02:53:09 +01:00
|
|
|
#include <mapnik/warning_ignore.hpp>
|
|
|
|
#include <boost/assert.hpp>
|
2013-08-13 20:13:56 +02:00
|
|
|
#include <boost/interprocess/mapped_region.hpp>
|
2011-04-06 15:02:31 +02:00
|
|
|
#include <boost/interprocess/file_mapping.hpp>
|
2020-11-19 15:30:30 +01:00
|
|
|
MAPNIK_DISABLE_WARNING_POP
|
2011-04-06 15:02:31 +02:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
namespace mapnik {
|
2011-04-06 15:02:31 +02:00
|
|
|
|
2016-02-19 10:58:49 +01:00
|
|
|
template class singleton<mapped_memory_cache, CreateStatic>;
|
|
|
|
|
2012-03-10 00:16:01 +01:00
|
|
|
void mapped_memory_cache::clear()
|
|
|
|
{
|
|
|
|
#ifdef MAPNIK_THREADSAFE
|
2015-05-20 17:12:22 +02:00
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
2012-03-10 00:16:01 +01:00
|
|
|
#endif
|
|
|
|
return cache_.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool mapped_memory_cache::insert(std::string const& uri, mapped_region_ptr mem)
|
2011-04-06 15:02:31 +02:00
|
|
|
{
|
|
|
|
#ifdef MAPNIK_THREADSAFE
|
2015-05-20 17:12:22 +02:00
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
2011-04-06 15:02:31 +02:00
|
|
|
#endif
|
2022-01-26 10:43:31 +01:00
|
|
|
return cache_.emplace(uri, mem).second;
|
2011-04-06 15:02:31 +02:00
|
|
|
}
|
|
|
|
|
2022-01-21 00:10:17 +01:00
|
|
|
bool mapped_memory_cache::remove(std::string const& key)
|
|
|
|
{
|
|
|
|
#ifdef MAPNIK_THREADSAFE
|
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
#endif
|
|
|
|
return cache_.erase(key) > 0;
|
|
|
|
}
|
|
|
|
|
2011-04-06 15:02:31 +02:00
|
|
|
boost::optional<mapped_region_ptr> mapped_memory_cache::find(std::string const& uri, bool update_cache)
|
|
|
|
{
|
|
|
|
#ifdef MAPNIK_THREADSAFE
|
2015-05-20 17:12:22 +02:00
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
2011-04-06 15:02:31 +02:00
|
|
|
#endif
|
2016-02-19 10:58:49 +01:00
|
|
|
|
2014-08-04 22:59:02 +02:00
|
|
|
using iterator_type = std::unordered_map<std::string, mapped_region_ptr>::const_iterator;
|
2011-04-06 15:02:31 +02:00
|
|
|
boost::optional<mapped_region_ptr> result;
|
|
|
|
iterator_type itr = cache_.find(uri);
|
|
|
|
if (itr != cache_.end())
|
|
|
|
{
|
|
|
|
result.reset(itr->second);
|
|
|
|
return result;
|
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2013-06-03 04:28:24 +02:00
|
|
|
if (mapnik::util::exists(uri))
|
2011-04-06 15:02:31 +02:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2022-01-26 10:43:31 +01:00
|
|
|
boost::interprocess::file_mapping mapping(uri.c_str(), boost::interprocess::read_only);
|
|
|
|
mapped_region_ptr region(
|
|
|
|
std::make_shared<boost::interprocess::mapped_region>(mapping, boost::interprocess::read_only));
|
2011-04-06 15:02:31 +02:00
|
|
|
result.reset(region);
|
|
|
|
if (update_cache)
|
|
|
|
{
|
2016-02-19 10:58:49 +01:00
|
|
|
cache_.emplace(uri, *result);
|
2011-04-06 15:02:31 +02:00
|
|
|
}
|
|
|
|
return result;
|
2022-01-26 10:43:31 +01:00
|
|
|
} catch (std::exception const& ex)
|
2011-04-06 15:02:31 +02:00
|
|
|
{
|
2013-03-23 01:58:33 +01:00
|
|
|
MAPNIK_LOG_ERROR(mapped_memory_cache)
|
2022-01-26 10:43:31 +01:00
|
|
|
<< "Error loading mapped memory file: '" << uri << "' (" << ex.what() << ")";
|
2011-04-06 15:02:31 +02:00
|
|
|
}
|
|
|
|
}
|
2012-04-10 00:51:04 +02:00
|
|
|
/*
|
|
|
|
else
|
|
|
|
{
|
|
|
|
MAPNIK_LOG_WARN(mapped_memory_cache) << "Memory region does not exist file: " << uri;
|
|
|
|
}
|
|
|
|
*/
|
2011-04-06 15:02:31 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
} // namespace mapnik
|
2013-08-13 20:48:04 +02:00
|
|
|
|
2013-09-20 15:00:11 +02:00
|
|
|
#endif
|