premiere-libtorrent/include/libtorrent/disk_buffer_pool.hpp

215 lines
6.5 KiB
C++
Raw Normal View History

2011-08-01 10:03:26 +02:00
/*
2014-02-23 20:12:25 +01:00
Copyright (c) 2007-2014, 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
2013-06-22 01:00:34 +02:00
#include <boost/utility.hpp>
2011-08-01 10:03:26 +02:00
#include "libtorrent/config.hpp"
#include "libtorrent/thread.hpp"
2014-07-06 21:18:00 +02:00
#include "libtorrent/io_service_fwd.hpp"
#include <vector>
#include <boost/shared_ptr.hpp>
#include <boost/function.hpp>
2011-08-01 10:03:26 +02:00
2014-07-06 21:18:00 +02:00
#if defined TORRENT_DEBUG || TORRENT_RELEASE_ASSERTS
#include <set>
#endif
2014-07-06 21:18:00 +02:00
#ifndef TORRENT_DISABLE_POOL_ALLOCATOR
#include "libtorrent/allocator.hpp" // for page_aligned_allocator
#include <boost/pool/pool.hpp>
2012-05-05 21:36:04 +02:00
#endif
2011-08-01 10:03:26 +02:00
namespace libtorrent
{
2014-07-06 21:18:00 +02:00
namespace aux { struct session_settings; }
class alert;
struct alert_dispatcher;
struct disk_observer;
struct TORRENT_EXTRA_EXPORT disk_buffer_pool : boost::noncopyable
2011-08-01 10:03:26 +02:00
{
2014-07-06 21:18:00 +02:00
disk_buffer_pool(int block_size, io_service& ios
, boost::function<void()> const& trigger_trim
, alert_dispatcher* alert_disp);
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
, mutex::scoped_lock& l) const;
bool is_disk_buffer(char* buffer) const;
#endif
2014-07-06 21:18:00 +02:00
// tries to allocate a disk buffer. If the cache is full, this function will
// return NULL and call the disk_observer once a buffer becomes available
char* async_allocate_buffer(char const* category, boost::function<void(char*)> const& handler);
2011-08-01 10:03:26 +02:00
char* allocate_buffer(char const* category);
2014-07-06 21:18:00 +02:00
char* allocate_buffer(bool& exceeded, boost::shared_ptr<disk_observer> o, char const* category);
2011-08-01 10:03:26 +02:00
void free_buffer(char* buf);
void free_multiple_buffers(char** bufvec, int numbufs);
int block_size() const { return m_block_size; }
#ifdef TORRENT_STATS
int disk_allocations() const
{ return m_allocations; }
#endif
void release_memory();
2014-07-06 21:18:00 +02:00
boost::uint32_t in_use() const
{
mutex::scoped_lock l(m_pool_mutex);
return m_in_use;
}
boost::uint32_t num_to_evict(int num_needed = 0);
bool exceeded_max_size() const { return m_exceeded_max_size; }
void set_settings(aux::session_settings const& sett);
struct handler_t
{
char* buffer; // argument to the callback
char const* category; // category of allocation
boost::function<void(char*)> callback;
};
2011-08-01 10:03:26 +02:00
protected:
void free_buffer_impl(char* buf, mutex::scoped_lock& l);
2014-07-06 21:18:00 +02:00
char* allocate_buffer_impl(mutex::scoped_lock& l, char const* category);
2011-08-01 10:03:26 +02:00
// number of bytes per block. The BitTorrent
// protocol defines the block size to 16 KiB.
const int m_block_size;
// 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
// TODO: try to remove the observers, only using the async_allocate handlers
std::vector<boost::shared_ptr<disk_observer> > m_observers;
// these handlers are executed when a new buffer is available
std::vector<handler_t> m_handlers;
// callback used to tell the cache it needs to free up some blocks
boost::function<void()> m_trigger_cache_trim;
// 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:
2014-07-06 21:18:00 +02:00
void check_buffer_level(mutex::scoped_lock& l);
2011-08-01 10:03:26 +02:00
mutable mutex m_pool_mutex;
2014-07-06 21:18:00 +02:00
int m_cache_buffer_chunk_size;
bool m_lock_disk_cache;
#if TORRENT_HAVE_MMAP
// the file descriptor of the cache mmap file
int m_cache_fd;
// the pointer to the block of virtual address space
// making up the mmapped cache space
char* m_cache_pool;
// list of block indices that are not in use. block_index
// times 0x4000 + m_cache_pool is the address where the
// corresponding memory lives
std::vector<int> m_free_list;
#endif
alert_dispatcher* m_post_alert;
#ifndef TORRENT_DISABLE_POOL_ALLOCATOR
// if this is true, all buffers are allocated
// from m_pool. If this is false, all buffers
// are allocated using page_aligned_allocator.
// if the settings change to prefer the other
// allocator, this bool will not switch over
// to match the settings until all buffers have
// been freed. That way, we never have a mixture
// of buffers allocated from different sources.
// in essence, this make the setting only take
// effect after a restart (which seems fine).
// or once the client goes idle for a while.
bool m_using_pool_allocator;
2014-07-06 21:18:00 +02:00
// this is the actual user setting
bool m_want_pool_allocator;
// memory pool for read and write operations
// and disk cache
boost::pool<page_aligned_allocator> m_pool;
#endif
2014-07-19 10:20:20 +02:00
#if defined TORRENT_STATS
2011-08-01 10:03:26 +02:00
int m_allocations;
#endif
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.
#if defined TORRENT_DEBUG
std::set<char*> m_buffers_in_use;
#endif
#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
2011-08-01 10:03:26 +02:00