2011-08-01 10:03:26 +02:00
|
|
|
/*
|
|
|
|
|
2016-01-18 00:57:46 +01:00
|
|
|
Copyright (c) 2007-2016, Arvid Norberg
|
2011-08-01 10:03:26 +02:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
#ifndef TORRENT_DISK_BUFFER_POOL_HPP
|
|
|
|
#define TORRENT_DISK_BUFFER_POOL_HPP
|
2011-08-01 10:03:26 +02:00
|
|
|
|
|
|
|
#include "libtorrent/config.hpp"
|
2015-04-18 04:33:39 +02:00
|
|
|
|
2016-07-02 01:46:59 +02:00
|
|
|
#if TORRENT_USE_INVARIANT_CHECKS
|
2015-04-18 04:33:39 +02:00
|
|
|
#include <set>
|
|
|
|
#endif
|
2016-08-13 03:31:55 +02:00
|
|
|
#include <vector>
|
|
|
|
#include <mutex>
|
|
|
|
#include <functional>
|
2016-09-01 03:42:18 +02:00
|
|
|
#include <memory>
|
2015-04-18 04:33:39 +02:00
|
|
|
|
|
|
|
#include "libtorrent/io_service_fwd.hpp"
|
2016-10-22 20:43:40 +02:00
|
|
|
#include "libtorrent/span.hpp"
|
2017-04-11 06:52:46 +02:00
|
|
|
#include "libtorrent/aux_/storage_utils.hpp" // for iovec_t
|
2015-04-18 04:33:39 +02:00
|
|
|
|
2017-04-12 19:00:57 +02:00
|
|
|
namespace libtorrent {
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
namespace aux { struct session_settings; }
|
|
|
|
class alert;
|
|
|
|
struct disk_observer;
|
|
|
|
|
2017-04-11 06:52:46 +02:00
|
|
|
struct TORRENT_EXTRA_EXPORT disk_buffer_pool
|
2011-08-01 10:03:26 +02:00
|
|
|
{
|
2018-01-03 12:54:03 +01:00
|
|
|
disk_buffer_pool(io_service& ios, std::function<void()> const& trigger_trim);
|
2017-04-11 06:52:46 +02:00
|
|
|
disk_buffer_pool(disk_buffer_pool const&) = delete;
|
|
|
|
disk_buffer_pool& operator=(disk_buffer_pool const&) = delete;
|
2011-08-01 10:03:26 +02:00
|
|
|
~disk_buffer_pool();
|
|
|
|
|
2014-07-19 10:20:20 +02:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2011-08-01 10:03:26 +02:00
|
|
|
bool is_disk_buffer(char* buffer
|
2016-05-01 00:54:23 +02:00
|
|
|
, std::unique_lock<std::mutex>& l) const;
|
2011-08-01 10:03:26 +02:00
|
|
|
bool is_disk_buffer(char* buffer) const;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
char* allocate_buffer(char const* category);
|
2016-09-01 03:42:18 +02:00
|
|
|
char* allocate_buffer(bool& exceeded, std::shared_ptr<disk_observer> o
|
2015-02-15 00:20:45 +01:00
|
|
|
, char const* category);
|
2011-08-01 10:03:26 +02:00
|
|
|
void free_buffer(char* buf);
|
2016-10-22 20:43:40 +02:00
|
|
|
void free_multiple_buffers(span<char*> bufvec);
|
2011-08-01 10:03:26 +02:00
|
|
|
|
2017-01-11 06:42:10 +01:00
|
|
|
int allocate_iovec(span<iovec_t> iov);
|
|
|
|
void free_iovec(span<iovec_t const> iov);
|
2015-02-15 00:20:45 +01:00
|
|
|
|
2016-11-27 14:46:53 +01:00
|
|
|
int in_use() const
|
2014-07-06 21:18:00 +02:00
|
|
|
{
|
2016-05-01 00:54:23 +02:00
|
|
|
std::unique_lock<std::mutex> l(m_pool_mutex);
|
2014-07-06 21:18:00 +02:00
|
|
|
return m_in_use;
|
|
|
|
}
|
2016-11-27 14:46:53 +01:00
|
|
|
int num_to_evict(int num_needed = 0);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2016-12-26 01:07:31 +01:00
|
|
|
void set_settings(aux::session_settings const& sett);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2011-08-01 10:03:26 +02:00
|
|
|
protected:
|
|
|
|
|
2016-05-01 00:54:23 +02:00
|
|
|
void free_buffer_impl(char* buf, std::unique_lock<std::mutex>& l);
|
|
|
|
char* allocate_buffer_impl(std::unique_lock<std::mutex>& l, char const* category);
|
2011-08-01 10:03:26 +02:00
|
|
|
|
|
|
|
// number of disk buffers currently allocated
|
|
|
|
int m_in_use;
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// cache size limit
|
|
|
|
int m_max_use;
|
|
|
|
|
|
|
|
// if we have exceeded the limit, we won't start
|
|
|
|
// allowing allocations again until we drop below
|
|
|
|
// this low watermark
|
|
|
|
int m_low_watermark;
|
|
|
|
|
|
|
|
// if we exceed the max number of buffers, we start
|
|
|
|
// adding up callbacks to this queue. Once the number
|
|
|
|
// of buffers in use drops below the low watermark,
|
|
|
|
// we start calling these functions back
|
2016-09-01 03:42:18 +02:00
|
|
|
std::vector<std::weak_ptr<disk_observer>> m_observers;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
// callback used to tell the cache it needs to free up some blocks
|
2016-08-13 03:31:55 +02:00
|
|
|
std::function<void()> m_trigger_cache_trim;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
// set to true to throttle more allocations
|
|
|
|
bool m_exceeded_max_size;
|
|
|
|
|
|
|
|
// this is the main thread io_service. Callbacks are
|
|
|
|
// posted on this in order to have them execute in
|
|
|
|
// the main thread.
|
|
|
|
io_service& m_ios;
|
2011-08-01 10:03:26 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2016-05-01 00:54:23 +02:00
|
|
|
void check_buffer_level(std::unique_lock<std::mutex>& l);
|
2016-10-19 07:18:05 +02:00
|
|
|
void remove_buffer_in_use(char* buf);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2016-05-01 00:54:23 +02:00
|
|
|
mutable std::mutex m_pool_mutex;
|
2011-08-01 10:03:26 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// this is specifically exempt from release_asserts
|
|
|
|
// since it's a quite costly check. Only for debug
|
|
|
|
// builds.
|
2016-07-02 01:46:59 +02:00
|
|
|
#if TORRENT_USE_INVARIANT_CHECKS
|
2014-07-06 21:18:00 +02:00
|
|
|
std::set<char*> m_buffers_in_use;
|
|
|
|
#endif
|
2014-01-19 20:45:50 +01:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2011-08-01 10:03:26 +02:00
|
|
|
int m_magic;
|
2014-07-06 21:18:00 +02:00
|
|
|
bool m_settings_set;
|
2011-08-01 10:03:26 +02:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
#endif // TORRENT_DISK_BUFFER_POOL_HPP
|