Skip to content

Commit

Permalink
Inject block memory allocation.
Browse files Browse the repository at this point in the history
  • Loading branch information
evoskuil committed Aug 1, 2024
1 parent d62837c commit 99fc188
Show file tree
Hide file tree
Showing 25 changed files with 73 additions and 68 deletions.
6 changes: 3 additions & 3 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ src_libbitcoin_network_la_CPPFLAGS = -I${srcdir}/include ${bitcoin_system_BUILD_
src_libbitcoin_network_la_LIBADD = ${boost_regex_LIBS} ${bitcoin_system_LIBS}
src_libbitcoin_network_la_SOURCES = \
src/error.cpp \
src/memory.cpp \
src/p2p.cpp \
src/settings.cpp \
src/async/thread.cpp \
Expand Down Expand Up @@ -94,7 +95,6 @@ src_libbitcoin_network_la_SOURCES = \
src/net/deadline.cpp \
src/net/distributor.cpp \
src/net/hosts.cpp \
src/net/memory.cpp \
src/net/proxy.cpp \
src/net/socket.cpp \
src/protocols/protocol.cpp \
Expand Down Expand Up @@ -126,6 +126,7 @@ test_libbitcoin_network_test_LDADD = src/libbitcoin-network.la ${boost_unit_test
test_libbitcoin_network_test_SOURCES = \
test/error.cpp \
test/main.cpp \
test/memory.cpp \
test/p2p.cpp \
test/settings.cpp \
test/test.cpp \
Expand Down Expand Up @@ -193,7 +194,6 @@ test_libbitcoin_network_test_SOURCES = \
test/net/deadline.cpp \
test/net/distributor.cpp \
test/net/hosts.cpp \
test/net/memory.cpp \
test/net/proxy.cpp \
test/net/socket.cpp \
test/protocols/protocol.cpp \
Expand Down Expand Up @@ -226,6 +226,7 @@ include_bitcoin_network_HEADERS = \
include/bitcoin/network/boost.hpp \
include/bitcoin/network/define.hpp \
include/bitcoin/network/error.hpp \
include/bitcoin/network/memory.hpp \
include/bitcoin/network/p2p.hpp \
include/bitcoin/network/settings.hpp \
include/bitcoin/network/version.hpp
Expand Down Expand Up @@ -344,7 +345,6 @@ include_bitcoin_network_net_HEADERS = \
include/bitcoin/network/net/deadline.hpp \
include/bitcoin/network/net/distributor.hpp \
include/bitcoin/network/net/hosts.hpp \
include/bitcoin/network/net/memory.hpp \
include/bitcoin/network/net/net.hpp \
include/bitcoin/network/net/proxy.hpp \
include/bitcoin/network/net/socket.hpp
Expand Down
4 changes: 2 additions & 2 deletions builds/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ link_libraries(
#------------------------------------------------------------------------------
add_library( ${CANONICAL_LIB_NAME}
"../../src/error.cpp"
"../../src/memory.cpp"
"../../src/p2p.cpp"
"../../src/settings.cpp"
"../../src/async/thread.cpp"
Expand Down Expand Up @@ -277,7 +278,6 @@ add_library( ${CANONICAL_LIB_NAME}
"../../src/net/deadline.cpp"
"../../src/net/distributor.cpp"
"../../src/net/hosts.cpp"
"../../src/net/memory.cpp"
"../../src/net/proxy.cpp"
"../../src/net/socket.cpp"
"../../src/protocols/protocol.cpp"
Expand Down Expand Up @@ -334,6 +334,7 @@ if (with-tests)
add_executable( libbitcoin-network-test
"../../test/error.cpp"
"../../test/main.cpp"
"../../test/memory.cpp"
"../../test/p2p.cpp"
"../../test/settings.cpp"
"../../test/test.cpp"
Expand Down Expand Up @@ -401,7 +402,6 @@ if (with-tests)
"../../test/net/deadline.cpp"
"../../test/net/distributor.cpp"
"../../test/net/hosts.cpp"
"../../test/net/memory.cpp"
"../../test/net/proxy.cpp"
"../../test/net/socket.cpp"
"../../test/protocols/protocol.cpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
<ClCompile Include="..\..\..\..\test\log\timer.cpp" />
<ClCompile Include="..\..\..\..\test\log\tracker.cpp" />
<ClCompile Include="..\..\..\..\test\main.cpp" />
<ClCompile Include="..\..\..\..\test\memory.cpp" />
<ClCompile Include="..\..\..\..\test\messages\address.cpp">
<ObjectFileName>$(IntDir)test_messages_address.obj</ObjectFileName>
</ClCompile>
Expand Down Expand Up @@ -139,7 +140,6 @@
<ClCompile Include="..\..\..\..\test\net\deadline.cpp" />
<ClCompile Include="..\..\..\..\test\net\distributor.cpp" />
<ClCompile Include="..\..\..\..\test\net\hosts.cpp" />
<ClCompile Include="..\..\..\..\test\net\memory.cpp" />
<ClCompile Include="..\..\..\..\test\net\proxy.cpp" />
<ClCompile Include="..\..\..\..\test\net\socket.cpp" />
<ClCompile Include="..\..\..\..\test\p2p.cpp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@
<ClCompile Include="..\..\..\..\test\main.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\test\memory.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\test\messages\address.cpp">
<Filter>src\messages</Filter>
</ClCompile>
Expand Down Expand Up @@ -231,9 +234,6 @@
<ClCompile Include="..\..\..\..\test\net\hosts.cpp">
<Filter>src\net</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\test\net\memory.cpp">
<Filter>src\net</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\test\net\proxy.cpp">
<Filter>src\net</Filter>
</ClCompile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
<ClCompile Include="..\..\..\..\src\log\capture.cpp" />
<ClCompile Include="..\..\..\..\src\log\logger.cpp" />
<ClCompile Include="..\..\..\..\src\log\reporter.cpp" />
<ClCompile Include="..\..\..\..\src\memory.cpp" />
<ClCompile Include="..\..\..\..\src\messages\address.cpp">
<ObjectFileName>$(IntDir)src_messages_address.obj</ObjectFileName>
</ClCompile>
Expand Down Expand Up @@ -134,7 +135,6 @@
<ClCompile Include="..\..\..\..\src\net\deadline.cpp" />
<ClCompile Include="..\..\..\..\src\net\distributor.cpp" />
<ClCompile Include="..\..\..\..\src\net\hosts.cpp" />
<ClCompile Include="..\..\..\..\src\net\memory.cpp" />
<ClCompile Include="..\..\..\..\src\net\proxy.cpp" />
<ClCompile Include="..\..\..\..\src\net\socket.cpp" />
<ClCompile Include="..\..\..\..\src\p2p.cpp" />
Expand Down Expand Up @@ -189,6 +189,7 @@
<ClInclude Include="..\..\..\..\include\bitcoin\network\log\reporter.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\log\timer.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\log\tracker.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\memory.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\messages\address.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\messages\address_item.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\messages\alert.hpp" />
Expand Down Expand Up @@ -240,7 +241,6 @@
<ClInclude Include="..\..\..\..\include\bitcoin\network\net\deadline.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\net\distributor.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\net\hosts.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\net\memory.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\net\net.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\net\proxy.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\net\socket.hpp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@
<ClCompile Include="..\..\..\..\src\log\reporter.cpp">
<Filter>src\log</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\src\memory.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\src\messages\address.cpp">
<Filter>src\messages</Filter>
</ClCompile>
Expand Down Expand Up @@ -252,9 +255,6 @@
<ClCompile Include="..\..\..\..\src\net\hosts.cpp">
<Filter>src\net</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\src\net\memory.cpp">
<Filter>src\net</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\src\net\proxy.cpp">
<Filter>src\net</Filter>
</ClCompile>
Expand Down Expand Up @@ -413,6 +413,9 @@
<ClInclude Include="..\..\..\..\include\bitcoin\network\log\tracker.hpp">
<Filter>include\bitcoin\network\log</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\network\memory.hpp">
<Filter>include\bitcoin\network</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\network\messages\address.hpp">
<Filter>include\bitcoin\network\messages</Filter>
</ClInclude>
Expand Down Expand Up @@ -566,9 +569,6 @@
<ClInclude Include="..\..\..\..\include\bitcoin\network\net\hosts.hpp">
<Filter>include\bitcoin\network\net</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\network\net\memory.hpp">
<Filter>include\bitcoin\network\net</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\network\net\net.hpp">
<Filter>include\bitcoin\network\net</Filter>
</ClInclude>
Expand Down
2 changes: 1 addition & 1 deletion include/bitcoin/network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <bitcoin/network/boost.hpp>
#include <bitcoin/network/define.hpp>
#include <bitcoin/network/error.hpp>
#include <bitcoin/network/memory.hpp>
#include <bitcoin/network/p2p.hpp>
#include <bitcoin/network/settings.hpp>
#include <bitcoin/network/version.hpp>
Expand Down Expand Up @@ -100,7 +101,6 @@
#include <bitcoin/network/net/deadline.hpp>
#include <bitcoin/network/net/distributor.hpp>
#include <bitcoin/network/net/hosts.hpp>
#include <bitcoin/network/net/memory.hpp>
#include <bitcoin/network/net/net.hpp>
#include <bitcoin/network/net/proxy.hpp>
#include <bitcoin/network/net/socket.hpp>
Expand Down
5 changes: 3 additions & 2 deletions include/bitcoin/network/define.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,16 @@ namespace network {
// Each header includes only its required common headers.

// /async : define
// /messages : define
// /messages : define memory
// /log : define /async
// /config : define /messages /async
// /net : define settings /config /log
// /net : define settings memory /config /log
// /sessions : define settings /net [forward: p2p]
// /protocols : define settings /sessions

// Root directory singletons.

// memory : define
// settings : define /messages /config
// p2p : define settings /sessions

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LIBBITCOIN_NETWORK_NET_MEMORY_HPP
#define LIBBITCOIN_NETWORK_NET_MEMORY_HPP
#ifndef LIBBITCOIN_NETWORK_MEMORY_HPP
#define LIBBITCOIN_NETWORK_MEMORY_HPP

#include <memory>
#include <bitcoin/system.hpp>
Expand All @@ -26,20 +26,31 @@
namespace libbitcoin {
namespace network {

/// Tracked memory allocation interface.
class BCT_API memory
{
public:
DELETE_COPY_MOVE_DESTRUCT(memory);
/// Get memory arena.
virtual arena* get_arena() NOEXCEPT = 0;

memory() NOEXCEPT;
memory(arena* arena) NOEXCEPT;
/// Get memory retainer.
virtual retainer::ptr get_retainer() NOEXCEPT = 0;
};

/// Default tracked memory implementation (untracked).
class BCT_API default_memory final
: public memory
{
public:
DELETE_COPY_MOVE_DESTRUCT(default_memory);

default_memory() NOEXCEPT;

virtual arena* get_arena() NOEXCEPT;
virtual retainer::ptr get_retainer() NOEXCEPT;
/// Get memory arena (system default).
arena* get_arena() NOEXCEPT override;

private:
// This is thread safe.
arena* arena_;
/// Get memory retainer (empty pointer).
retainer::ptr get_retainer() NOEXCEPT override;
};

} // namespace network
Expand Down
3 changes: 2 additions & 1 deletion include/bitcoin/network/messages/block.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <memory>
#include <bitcoin/system.hpp>
#include <bitcoin/network/define.hpp>
#include <bitcoin/network/memory.hpp>
#include <bitcoin/network/messages/enums/identifier.hpp>

namespace libbitcoin {
Expand All @@ -38,7 +39,7 @@ struct BCT_API block
static const uint32_t version_minimum;
static const uint32_t version_maximum;

static cptr deserialize(auto& memory, uint32_t version,
static cptr deserialize(memory& memory, uint32_t version,
const system::data_chunk& data, bool witness=true) NOEXCEPT;
static cptr deserialize(uint32_t version, const system::data_chunk& data,
bool witness=true) NOEXCEPT;
Expand Down
2 changes: 1 addition & 1 deletion include/bitcoin/network/net/channel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
#include <bitcoin/network/config/config.hpp>
#include <bitcoin/network/define.hpp>
#include <bitcoin/network/log/log.hpp>
#include <bitcoin/network/memory.hpp>
#include <bitcoin/network/messages/messages.hpp>
#include <bitcoin/network/net/broadcaster.hpp>
#include <bitcoin/network/net/deadline.hpp>
#include <bitcoin/network/net/memory.hpp>
#include <bitcoin/network/net/proxy.hpp>
#include <bitcoin/network/settings.hpp>

Expand Down
2 changes: 1 addition & 1 deletion include/bitcoin/network/net/distributor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
#include <bitcoin/system.hpp>
#include <bitcoin/network/async/async.hpp>
#include <bitcoin/network/define.hpp>
#include <bitcoin/network/memory.hpp>
#include <bitcoin/network/messages/messages.hpp>
#include <bitcoin/network/net/memory.hpp>

namespace libbitcoin {
namespace network {
Expand Down
1 change: 0 additions & 1 deletion include/bitcoin/network/net/net.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include <bitcoin/network/net/deadline.hpp>
#include <bitcoin/network/net/distributor.hpp>
#include <bitcoin/network/net/hosts.hpp>
#include <bitcoin/network/net/memory.hpp>
#include <bitcoin/network/net/proxy.hpp>
#include <bitcoin/network/net/socket.hpp>

Expand Down
2 changes: 1 addition & 1 deletion include/bitcoin/network/net/proxy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
#include <bitcoin/system.hpp>
#include <bitcoin/network/async/async.hpp>
#include <bitcoin/network/define.hpp>
#include <bitcoin/network/memory.hpp>
#include <bitcoin/network/messages/messages.hpp>
#include <bitcoin/network/net/distributor.hpp>
#include <bitcoin/network/net/memory.hpp>
#include <bitcoin/network/net/socket.hpp>

namespace libbitcoin {
Expand Down
17 changes: 5 additions & 12 deletions src/net/memory.cpp → src/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,24 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <bitcoin/network/net/memory.hpp>
#include <bitcoin/network/memory.hpp>

#include <memory>
#include <bitcoin/system.hpp>
#include <bitcoin/network/define.hpp>

namespace libbitcoin {
namespace network {

memory::memory() NOEXCEPT
: memory(default_arena::get())
default_memory::default_memory() NOEXCEPT
{
}

memory::memory(arena* arena) NOEXCEPT
: arena_(arena)
arena* default_memory::get_arena() NOEXCEPT
{
return default_arena::get();
}

arena* memory::get_arena() NOEXCEPT
{
return arena_;
}

retainer::ptr memory::get_retainer() NOEXCEPT
retainer::ptr default_memory::get_retainer() NOEXCEPT
{
return {};
}
Expand Down
6 changes: 3 additions & 3 deletions src/messages/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
#include <iterator>
#include <memory>
#include <bitcoin/system.hpp>
#include <bitcoin/network/memory.hpp>
#include <bitcoin/network/messages/enums/identifier.hpp>
#include <bitcoin/network/messages/enums/level.hpp>
#include <bitcoin/network/messages/message.hpp>
#include <bitcoin/network/messages/transaction.hpp>
#include <bitcoin/network/net/memory.hpp>

namespace libbitcoin {
namespace network {
Expand All @@ -42,12 +42,12 @@ const uint32_t block::version_maximum = level::maximum_protocol;
typename block::cptr block::deserialize(uint32_t version,
const system::data_chunk& data, bool witness) NOEXCEPT
{
static memory memory{};
static default_memory memory{};
return deserialize(memory, version, data, witness);
}

// static
typename block::cptr block::deserialize(auto& memory, uint32_t version,
typename block::cptr block::deserialize(memory& memory, uint32_t version,
const system::data_chunk& data, bool witness) NOEXCEPT
{
system::istream source{ data };
Expand Down
Loading

0 comments on commit 99fc188

Please sign in to comment.