allocation slot type (#1352)

use a specialized type for allocation slot indices
This commit is contained in:
Arvid Norberg 2016-11-26 03:09:01 -05:00 committed by GitHub
parent 15ab8f387b
commit 54f052a97b
9 changed files with 92 additions and 74 deletions

View File

@ -16,5 +16,9 @@
#undef snprintf
#endif
#ifdef vsnprintf
#undef vsnprintf
#endif
#endif

View File

@ -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 <boost/shared_array.hpp>
#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<aux::stack_allocator const> 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<aux::stack_allocator const> 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<aux::stack_allocator const> 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<aux::stack_allocator const> 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<aux::stack_allocator const> 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<aux::stack_allocator> 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<aux::stack_allocator> 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<aux::stack_allocator> 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<piece_block> 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<aux::stack_allocator> m_alloc;
int const m_msg_idx;
aux::allocation_slot const m_msg_idx;
};
#undef TORRENT_DEFINE_ALERT_IMPL

View File

@ -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 <cstdio> // for vsnprintf
#include <cstring>
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<char const> buf)
allocation_slot copy_buffer(span<char const> 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)

View File

@ -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

View File

@ -60,6 +60,7 @@ POSSIBILITY OF SUCH DAMAGE.
#if DEBUG_DISK_THREAD
#include <cstdarg>
#include <sstream>
#include <cstdio> // 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)

View File

@ -33,6 +33,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include <cstdlib>
#include <cstdarg>
#include <functional>
#include <cstdio> // 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);
}

View File

@ -57,6 +57,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include <cstdarg>
#include <tuple>
#include <iostream>
#include <cstdio> // 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);

View File

@ -48,6 +48,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include <functional>
#include <iostream>
#include <cstdarg>
#include <cstdio> // 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"

View File

@ -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<char const>("testing"));
allocation_slot const idx1 = a.copy_buffer(libtorrent::span<char const>("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);