diff --git a/bindings/python/src/boost_python.hpp b/bindings/python/src/boost_python.hpp index dd60b0132..90353a5b8 100644 --- a/bindings/python/src/boost_python.hpp +++ b/bindings/python/src/boost_python.hpp @@ -16,5 +16,9 @@ #undef snprintf #endif +#ifdef vsnprintf +#undef vsnprintf +#endif + #endif diff --git a/include/libtorrent/alert_types.hpp b/include/libtorrent/alert_types.hpp index 12230c377..455c83688 100644 --- a/include/libtorrent/alert_types.hpp +++ b/include/libtorrent/alert_types.hpp @@ -51,20 +51,14 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/piece_block.hpp" #include "libtorrent/aux_/escape_string.hpp" // for convert_from_native #include "libtorrent/string_view.hpp" +#include "libtorrent/stack_allocator.hpp" #include "libtorrent/aux_/disable_warnings_push.hpp" - #include - #include "libtorrent/aux_/disable_warnings_pop.hpp" namespace libtorrent { - - namespace aux { - struct stack_allocator; - } - // maps an operation id (from peer_error_alert and peer_disconnected_alert) // to its name. See peer_connection for the constants TORRENT_EXPORT char const* operation_name(int op); @@ -99,7 +93,7 @@ namespace libtorrent protected: std::reference_wrapper m_alloc; private: - int m_name_idx; + aux::allocation_slot m_name_idx; }; // The peer alert is a base class for alerts that refer to a specific peer. It includes all @@ -150,7 +144,7 @@ namespace libtorrent std::string url; #endif private: - int const m_url_idx; + aux::allocation_slot const m_url_idx; }; #define TORRENT_DEFINE_ALERT_IMPL(name, seq, prio) \ @@ -273,7 +267,7 @@ namespace libtorrent // refers to the index of the file that was renamed, int const index; private: - int const m_name_idx; + aux::allocation_slot const m_name_idx; }; // This is posted as a response to a torrent_handle::rename_file() call, if the rename @@ -447,7 +441,7 @@ namespace libtorrent char const* error_message() const; private: - int const m_msg_idx; + aux::allocation_slot const m_msg_idx; }; // This alert is triggered if the tracker reply contains a warning field. @@ -473,7 +467,7 @@ namespace libtorrent char const* warning_message() const; private: - int const m_msg_idx; + aux::allocation_slot const m_msg_idx; }; // This alert is generated when a scrape request succeeds. @@ -524,7 +518,7 @@ namespace libtorrent char const* error_message() const; private: - int const m_msg_idx; + aux::allocation_slot const m_msg_idx; }; // This alert is only for informational purpose. It is generated when a tracker announce @@ -892,7 +886,7 @@ namespace libtorrent char const* storage_path() const; private: - int const m_path_idx; + aux::allocation_slot const m_path_idx; }; // The ``storage_moved_failed_alert`` is generated when an attempt to move the storage, @@ -923,7 +917,7 @@ namespace libtorrent // terminated string naming which one, otherwise it's nullptr. char const* operation; private: - int const m_file_idx; + aux::allocation_slot const m_file_idx; }; // This alert is generated when a request to delete the files of a torrent complete. @@ -1088,8 +1082,8 @@ namespace libtorrent char const* error_message() const; private: - int const m_url_idx; - int const m_msg_idx; + aux::allocation_slot const m_url_idx; + aux::allocation_slot const m_msg_idx; }; // If the storage fails to read or write files that it needs access to, this alert is @@ -1123,7 +1117,7 @@ namespace libtorrent std::string msg; #endif private: - int const m_file_idx; + aux::allocation_slot const m_file_idx; }; // This alert is generated when the metadata has been completely received and the info-hash @@ -1305,7 +1299,7 @@ namespace libtorrent private: std::reference_wrapper m_alloc; - int const m_interface_idx; + aux::allocation_slot const m_interface_idx; }; // This alert is posted when the listen port succeeds to be opened on a @@ -1452,7 +1446,7 @@ namespace libtorrent // TODO: 2 should the alert baseclass have this object instead? std::reference_wrapper m_alloc; - int const m_log_idx; + aux::allocation_slot const m_log_idx; }; // This alert is generated when a fastresume file has been passed to @@ -1489,7 +1483,7 @@ namespace libtorrent std::string msg; #endif private: - int const m_path_idx; + aux::allocation_slot const m_path_idx; }; // This alert is posted when an incoming peer connection, or a peer that's about to be added @@ -1690,7 +1684,7 @@ namespace libtorrent char const* tracker_id() const; private: - int const m_tracker_idx; + aux::allocation_slot const m_tracker_idx; }; // This alert is posted when the initial DHT bootstrap is done. @@ -1729,7 +1723,7 @@ namespace libtorrent char const* filename() const; private: - int const m_file_idx; + aux::allocation_slot const m_file_idx; }; // This is always posted for SSL torrents. This is a reminder to the client that @@ -2120,7 +2114,7 @@ namespace libtorrent private: std::reference_wrapper m_alloc; - int const m_str_idx; + aux::allocation_slot const m_str_idx; }; // This alert is posted by torrent wide events. It's meant to be used for @@ -2148,7 +2142,7 @@ namespace libtorrent #endif private: - int const m_str_idx; + aux::allocation_slot const m_str_idx; }; // This alert is posted by events specific to a peer. It's meant to be used @@ -2195,7 +2189,7 @@ namespace libtorrent #endif private: - int const m_str_idx; + aux::allocation_slot const m_str_idx; }; // posted if the local service discovery socket fails to start properly. @@ -2342,7 +2336,7 @@ namespace libtorrent private: std::reference_wrapper m_alloc; - int const m_msg_idx; + aux::allocation_slot const m_msg_idx; }; // This alert is posted every time a DHT message is sent or received. It is @@ -2380,7 +2374,7 @@ namespace libtorrent private: std::reference_wrapper m_alloc; - int const m_msg_idx; + aux::allocation_slot const m_msg_idx; int const m_size; }; @@ -2408,7 +2402,7 @@ namespace libtorrent private: std::reference_wrapper m_alloc; int const m_num_peers; - int m_peers_idx; + aux::allocation_slot m_peers_idx; }; // This is posted exactly once for every call to session_handle::dht_direct_request. @@ -2438,7 +2432,7 @@ namespace libtorrent private: std::reference_wrapper m_alloc; - int const m_response_idx; + aux::allocation_slot const m_response_idx; int const m_response_size; }; @@ -2486,7 +2480,7 @@ namespace libtorrent std::vector blocks() const; private: - int const m_array_idx; + aux::allocation_slot const m_array_idx; int const m_num_blocks; }; @@ -2508,7 +2502,7 @@ namespace libtorrent private: std::reference_wrapper m_alloc; - int const m_msg_idx; + aux::allocation_slot const m_msg_idx; }; #undef TORRENT_DEFINE_ALERT_IMPL diff --git a/include/libtorrent/stack_allocator.hpp b/include/libtorrent/stack_allocator.hpp index 647ec1ad2..84ea3bfd8 100644 --- a/include/libtorrent/stack_allocator.hpp +++ b/include/libtorrent/stack_allocator.hpp @@ -31,15 +31,32 @@ POSSIBILITY OF SUCH DAMAGE. */ #ifndef TORRENT_STACK_ALLOCATOR +#define TORRENT_STACK_ALLOCATOR #include "libtorrent/assert.hpp" #include "libtorrent/span.hpp" #include "libtorrent/string_view.hpp" +#include // for vsnprintf #include namespace libtorrent { namespace aux { + struct allocation_slot + { + allocation_slot() : m_idx(-1) {} + allocation_slot(allocation_slot const&) = default; + allocation_slot(allocation_slot&&) = default; + allocation_slot& operator=(allocation_slot const&) = default; + allocation_slot& operator=(allocation_slot&&) = default; + bool operator==(allocation_slot const& s) const { return m_idx == s.m_idx; } + bool operator!=(allocation_slot const& s) const { return m_idx != s.m_idx; } + friend struct stack_allocator; + private: + explicit allocation_slot(int idx) : m_idx(idx) {} + int val() const { return m_idx; } + int m_idx; + }; struct stack_allocator { @@ -49,25 +66,25 @@ namespace libtorrent { namespace aux stack_allocator(stack_allocator const&) = delete; stack_allocator& operator=(stack_allocator const&) = delete; - int copy_string(string_view str) + allocation_slot copy_string(string_view str) { int const ret = int(m_storage.size()); m_storage.resize(ret + str.size() + 1); std::memcpy(&m_storage[ret], str.data(), str.size()); m_storage[ret + str.length()] = '\0'; - return ret; + return allocation_slot(ret); } - int copy_string(char const* str) + allocation_slot copy_string(char const* str) { int const ret = int(m_storage.size()); int const len = int(std::strlen(str)); m_storage.resize(ret + len + 1); std::strcpy(&m_storage[ret], str); - return ret; + return allocation_slot(ret); } - int format_string(char const* fmt, va_list v) + allocation_slot format_string(char const* fmt, va_list v) { int const ret = int(m_storage.size()); m_storage.resize(ret + 512); @@ -89,39 +106,39 @@ namespace libtorrent { namespace aux // +1 is to include the 0-terminator m_storage.resize(ret + (len > 512 ? 512 : len) + 1); - return ret; + return allocation_slot(ret); } - int copy_buffer(span buf) + allocation_slot copy_buffer(span buf) { int const ret = int(m_storage.size()); int const size = int(buf.size()); - if (size < 1) return -1; + if (size < 1) return allocation_slot(); m_storage.resize(ret + size); std::memcpy(&m_storage[ret], buf.data(), size); - return ret; + return allocation_slot(ret); } - int allocate(int const bytes) + allocation_slot allocate(int const bytes) { - if (bytes < 1) return -1; + if (bytes < 1) return allocation_slot(); int const ret = int(m_storage.size()); m_storage.resize(ret + bytes); - return ret; + return allocation_slot(ret); } - char* ptr(int const idx) + char* ptr(allocation_slot const idx) { - if(idx < 0) return NULL; - TORRENT_ASSERT(idx < int(m_storage.size())); - return &m_storage[idx]; + if(idx.val() < 0) return nullptr; + TORRENT_ASSERT(idx.val() < int(m_storage.size())); + return &m_storage[idx.val()]; } - char const* ptr(int const idx) const + char const* ptr(allocation_slot const idx) const { - if(idx < 0) return NULL; - TORRENT_ASSERT(idx < int(m_storage.size())); - return &m_storage[idx]; + if(idx.val() < 0) return nullptr; + TORRENT_ASSERT(idx.val() < int(m_storage.size())); + return &m_storage[idx.val()]; } void swap(stack_allocator& rhs) diff --git a/src/alert.cpp b/src/alert.cpp index 97c551401..81ab930d9 100644 --- a/src/alert.cpp +++ b/src/alert.cpp @@ -366,7 +366,7 @@ namespace libtorrent { , msg(convert_from_native(e.message())) #endif , error(e) - , m_msg_idx(-1) + , m_msg_idx() { TORRENT_ASSERT(!u.empty()); } @@ -388,7 +388,7 @@ namespace libtorrent { #ifndef TORRENT_NO_DEPRECATE return msg.c_str(); #else - if (m_msg_idx == -1) return ""; + if (m_msg_idx == aux::allocation_slot()) return ""; else return m_alloc.get().ptr(m_msg_idx); #endif } @@ -1817,7 +1817,7 @@ namespace libtorrent { #endif , error(e) , m_url_idx(alloc.copy_string(u)) - , m_msg_idx(-1) + , m_msg_idx() {} url_seed_alert::url_seed_alert(aux::stack_allocator& alloc, torrent_handle const& h @@ -1847,7 +1847,7 @@ namespace libtorrent { #ifndef TORRENT_NO_DEPRECATE return msg.c_str(); #else - if (m_msg_idx == -1) return ""; + if (m_msg_idx == aux::allocation_slot()) return ""; return m_alloc.get().ptr(m_msg_idx); #endif } @@ -2042,7 +2042,8 @@ namespace libtorrent { , addr(addr_) #endif , m_alloc(alloc) - , m_response_idx(-1), m_response_size(0) + , m_response_idx() + , m_response_size(0) {} std::string dht_direct_response_alert::message() const diff --git a/src/disk_io_thread.cpp b/src/disk_io_thread.cpp index c63a1abc2..b1a81b58a 100644 --- a/src/disk_io_thread.cpp +++ b/src/disk_io_thread.cpp @@ -60,6 +60,7 @@ POSSIBILITY OF SUCH DAMAGE. #if DEBUG_DISK_THREAD #include #include +#include // for vsnprintf #define DLOG(...) debug_log(__VA_ARGS__) #else #define DLOG(...) do {} while(false) @@ -94,7 +95,7 @@ namespace libtorrent va_start(v, fmt); char usr[2048]; - int len = vsnprintf(usr, sizeof(usr), fmt, v); + int len = std::vsnprintf(usr, sizeof(usr), fmt, v); static bool prepend_time = true; if (!prepend_time) diff --git a/src/lsd.cpp b/src/lsd.cpp index 8050d65f5..3236f8287 100644 --- a/src/lsd.cpp +++ b/src/lsd.cpp @@ -33,6 +33,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include // for vsnprintf #include "libtorrent/lsd.hpp" #include "libtorrent/io.hpp" @@ -94,7 +95,7 @@ void lsd::debug_log(char const* fmt, ...) const va_start(v, fmt); char buf[1024]; - vsnprintf(buf, sizeof(buf), fmt, v); + std::vsnprintf(buf, sizeof(buf), fmt, v); va_end(v); m_callback.log_lsd(buf); } diff --git a/test/test_dht.cpp b/test/test_dht.cpp index 397bc5b71..e2cc7fb05 100644 --- a/test/test_dht.cpp +++ b/test/test_dht.cpp @@ -57,6 +57,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include // for vsnprintf #include "setup_transfer.hpp" @@ -489,7 +490,7 @@ struct obs : dht::dht_observer va_list v; va_start(v, fmt); char buf[1024]; - vsnprintf(buf, sizeof(buf), fmt, v); + std::vsnprintf(buf, sizeof(buf), fmt, v); va_end(v); std::printf("%s\n", buf); m_log.push_back(buf); diff --git a/test/test_fast_extension.cpp b/test/test_fast_extension.cpp index c61d5f342..dc4a9865c 100644 --- a/test/test_fast_extension.cpp +++ b/test/test_fast_extension.cpp @@ -48,6 +48,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include // for vsnprintf using namespace libtorrent; using namespace std::placeholders; @@ -59,7 +60,7 @@ void log(char const* fmt, ...) va_start(v, fmt); char buf[1024]; - vsnprintf(buf, sizeof(buf), fmt, v); + std::vsnprintf(buf, sizeof(buf), fmt, v); va_end(v); std::printf("\x1b[1m\x1b[36m%s: %s\x1b[0m\n" diff --git a/test/test_stack_allocator.cpp b/test/test_stack_allocator.cpp index e5a7dd470..23b509ddc 100644 --- a/test/test_stack_allocator.cpp +++ b/test/test_stack_allocator.cpp @@ -34,17 +34,17 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/stack_allocator.hpp" using libtorrent::aux::stack_allocator; +using libtorrent::aux::allocation_slot; TORRENT_TEST(copy_string) { stack_allocator a; - int const idx1 = a.copy_string("testing"); + allocation_slot const idx1 = a.copy_string("testing"); // attempt to trigger a reallocation a.allocate(100000); - int const idx2 = a.copy_string(std::string("foobar")); - + allocation_slot const idx2 = a.copy_string(std::string("foobar")); TEST_CHECK(strcmp(a.ptr(idx1), "testing") == 0); TEST_CHECK(strcmp(a.ptr(idx2), "foobar") == 0); @@ -53,7 +53,7 @@ TORRENT_TEST(copy_string) TORRENT_TEST(copy_buffer) { stack_allocator a; - int const idx1 = a.copy_buffer(libtorrent::span("testing")); + allocation_slot const idx1 = a.copy_buffer(libtorrent::span("testing")); // attempt to trigger a reallocation a.allocate(100000); @@ -61,18 +61,17 @@ TORRENT_TEST(copy_buffer) TEST_CHECK(strcmp(a.ptr(idx1), "testing") == 0); // attempt zero size allocation - int const idx2 = a.copy_buffer({}); - TEST_CHECK(idx2 == -1); + allocation_slot const idx2 = a.copy_buffer({}); // attempt to get a pointer after zero allocation char* ptr = a.ptr(idx2); - TEST_CHECK(ptr == NULL); + TEST_CHECK(ptr == nullptr); } TORRENT_TEST(allocate) { stack_allocator a; - int const idx1 = a.allocate(100); + allocation_slot const idx1 = a.allocate(100); char* ptr = a.ptr(idx1); for (int i = 0; i < 100; ++i) ptr[i] = char(i % 256); @@ -85,12 +84,11 @@ TORRENT_TEST(allocate) TEST_CHECK(ptr[i] == char(i % 256)); // attempt zero size allocation - int const idx2 = a.allocate(0); - TEST_CHECK(idx2 == -1); + allocation_slot const idx2 = a.allocate(0); // attempt to get a pointer after zero allocation ptr = a.ptr(idx2); - TEST_CHECK(ptr == NULL); + TEST_CHECK(ptr == nullptr); } TORRENT_TEST(swap) @@ -98,8 +96,8 @@ TORRENT_TEST(swap) stack_allocator a1; stack_allocator a2; - int const idx1 = a1.copy_string("testing"); - int const idx2 = a2.copy_string("foobar"); + allocation_slot const idx1 = a1.copy_string("testing"); + allocation_slot const idx2 = a2.copy_string("foobar"); a1.swap(a2);