Skip to content

Commit

Permalink
Remove boost dependency
Browse files Browse the repository at this point in the history
- Explicit inlining from boost source to not pull entire boost.
- Does dangerous things for any inter-process hooks, however unneeded
for out use-case.
  • Loading branch information
jerinphilip authored Sep 29, 2021
1 parent 9968fd0 commit 8103d00
Show file tree
Hide file tree
Showing 22 changed files with 276 additions and 136 deletions.
79 changes: 45 additions & 34 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
cmake_minimum_required(VERSION 2.8.12)
project(L4 CXX)

if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
message(FATAL_ERROR "Use provided solution file.")
elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
add_compile_options(-std=c++14)
add_definitions(-DBOOST_TEST_DYN_LINK)
# Enable setting options from repository above.
if (POLICY CMP0077)
cmake_policy(SET CMP0077 NEW)
endif()

set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost 1.45.0 COMPONENTS unit_test_framework REQUIRED)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
if(L4_COMPILE_UNIT_TESTS)
add_definitions(-DBOOST_TEST_DYN_LINK)
endif(L4_COMPILE_UNIT_TESTS)

if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
endif()

if(L4_COMPILE_UNIT_TESTS)
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost 1.45.0 COMPONENTS unit_test_framework REQUIRED)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
endif()
endif(L4_COMPILE_UNIT_TESTS)

include_directories(${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/inc
${CMAKE_CURRENT_SOURCE_DIR}/inc/L4)
Expand All @@ -34,26 +43,28 @@ add_library(L4
${L4_SOURCES}
${L4_HEADERS})

enable_testing()

add_executable(L4.UnitTests
Unittests/CacheHashTableTest.cpp
Unittests/EpochManagerTest.cpp
Unittests/HashTableManagerTest.cpp
Unittests/HashTableRecordTest.cpp
Unittests/HashTableServiceTest.cpp
Unittests/PerfInfoTest.cpp
Unittests/ReadWriteHashTableSerializerTest.cpp
Unittests/ReadWriteHashTableTest.cpp
Unittests/SettingAdapterTest.cpp
Unittests/Utils.cpp
Unittests/UtilsTest.cpp
Unittests/Main.cpp)

target_link_libraries(L4.UnitTests
L4
${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}
-lpthread)
#${CMAKE_THREAD_LIBS_INIT})

add_test(NAME L4UnitTests COMMAND L4.UnitTests)
if(L4_COMPILE_UNIT_TESTS)
enable_testing()
add_executable(L4.UnitTests
Unittests/CacheHashTableTest.cpp
Unittests/EpochManagerTest.cpp
Unittests/HashTableManagerTest.cpp
Unittests/HashTableRecordTest.cpp
Unittests/HashTableServiceTest.cpp
Unittests/PerfInfoTest.cpp
Unittests/ReadWriteHashTableSerializerTest.cpp
Unittests/ReadWriteHashTableTest.cpp
Unittests/SettingAdapterTest.cpp
Unittests/Utils.cpp
Unittests/UtilsTest.cpp
Unittests/Main.cpp)

target_link_libraries(L4.UnitTests
L4
${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}
-lpthread)
#${CMAKE_THREAD_LIBS_INIT})

add_test(NAME L4UnitTests COMMAND L4.UnitTests)

endif(L4_COMPILE_UNIT_TESTS)
4 changes: 2 additions & 2 deletions Unittests/EpochManagerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ BOOST_AUTO_TEST_CASE(EpochRefManagerTest) {
const std::uint32_t c_epochQueueSize = 100U;

using EpochQueue =
EpochQueue<boost::shared_lock_guard<L4::Utils::ReaderWriterLockSlim>,
EpochQueue<std::shared_lock<L4::Utils::ReaderWriterLockSlim>,
std::lock_guard<L4::Utils::ReaderWriterLockSlim>>;

EpochQueue epochQueue(currentEpochCounter, c_epochQueueSize);
Expand Down Expand Up @@ -50,7 +50,7 @@ BOOST_AUTO_TEST_CASE(EpochCounterManagerTest) {
const std::uint32_t c_epochQueueSize = 100U;

using EpochQueue =
EpochQueue<boost::shared_lock_guard<L4::Utils::ReaderWriterLockSlim>,
EpochQueue<std::shared_lock<L4::Utils::ReaderWriterLockSlim>,
std::lock_guard<L4::Utils::ReaderWriterLockSlim>>;

EpochQueue epochQueue(currentEpochCounter, c_epochQueueSize);
Expand Down
12 changes: 7 additions & 5 deletions inc/L4/Epoch/EpochQueue.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#pragma once

#include <vector>
#include <atomic>
#include <memory>
#include <mutex>
#include "Interprocess/Container/Vector.h"
#include "Utils/Exception.h"
#include "Utils/Lock.h"

Expand Down Expand Up @@ -38,18 +38,20 @@ struct EpochQueue {
using SharableLock = TSharableLock;
using ExclusiveLock = TExclusiveLock;
using RefCount = std::atomic<std::uint32_t>;
using RefCounts = Interprocess::Container::
Vector<RefCount, typename Allocator::template rebind<RefCount>::other>;
// @jerinphilip: dirty-fix, browsermt/L4 hopefully does not need IPC.
// using RefCounts = Interprocess::Container::
// Vector<RefCount, typename Allocator::template rebind<RefCount>::other>;
using RefCounts = std::vector<RefCount, typename Allocator::template rebind<RefCount>::other>;

// The followings (m_frontIndex and m_backIndex) are
// accessed/updated only by the owner thread (only one thread), thus
// they don't require any synchronization.
std::size_t m_frontIndex;
std::uint64_t m_frontIndex;

// Back index represents the latest epoch counter value. Note that
// this is accessed/updated by multiple threads, thus requires
// synchronization.
std::size_t m_backIndex;
std::uint64_t m_backIndex;

// Read/Write lock for m_backIndex.
typename SharableLock::mutex_type m_mutexForBackIndex;
Expand Down
8 changes: 4 additions & 4 deletions inc/L4/Epoch/EpochRefPolicy.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <boost/integer_traits.hpp>
#include <cstdint>
#include <limits>

namespace L4 {

Expand All @@ -17,11 +17,11 @@ class EpochRefPolicy {
: m_epochRefManager{epochRefPolicy.m_epochRefManager},
m_epochCounter{epochRefPolicy.m_epochCounter} {
epochRefPolicy.m_epochCounter =
boost::integer_traits<std::uint64_t>::const_max;
std::numeric_limits<std::uint64_t>::max();
}

~EpochRefPolicy() {
if (m_epochCounter != boost::integer_traits<std::uint64_t>::const_max) {
if (m_epochCounter != std::numeric_limits<std::uint64_t>::max()) {
m_epochRefManager.RemoveRef(m_epochCounter);
}
}
Expand All @@ -34,4 +34,4 @@ class EpochRefPolicy {
std::uint64_t m_epochCounter;
};

} // namespace L4
} // namespace L4
11 changes: 3 additions & 8 deletions inc/L4/HashTable/Common/Record.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <cstdint>
#include <cstring>
#include "HashTable/IHashTable.h"
#include "Utils/Exception.h"

Expand Down Expand Up @@ -73,14 +74,8 @@ class RecordSerializer {

const auto start = SerializeSizes(buffer, key.m_size, value.m_size);

#if defined(_MSC_VER)
memcpy_s(buffer + start, key.m_size, key.m_data, key.m_size);
memcpy_s(buffer + start + key.m_size, value.m_size, value.m_data,
value.m_size);
#else
memcpy(buffer + start, key.m_data, key.m_size);
memcpy(buffer + start + key.m_size, value.m_data, value.m_size);
#endif
std::memcpy(buffer + start, key.m_data, key.m_size);
std::memcpy(buffer + start + key.m_size, value.m_data, value.m_size);
return reinterpret_cast<RecordBuffer*>(buffer);
}

Expand Down
8 changes: 4 additions & 4 deletions inc/L4/HashTable/Common/SettingAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ class SettingAdapter {

to.m_numBuckets = from.m_numBuckets;
to.m_numBucketsPerMutex =
(std::max)(from.m_numBucketsPerMutex.get_value_or(1U), 1U);
to.m_fixedKeySize = from.m_fixedKeySize.get_value_or(0U);
to.m_fixedValueSize = from.m_fixedValueSize.get_value_or(0U);
(std::max)(from.m_numBucketsPerMutex.value_or(1U), 1U);
to.m_fixedKeySize = from.m_fixedKeySize.value_or(0U);
to.m_fixedValueSize = from.m_fixedValueSize.value_or(0U);

return to;
}
};

} // namespace HashTable
} // namespace L4
} // namespace L4
26 changes: 13 additions & 13 deletions inc/L4/HashTable/Config.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <boost/optional.hpp>
#include <optional>
#include <cassert>
#include <chrono>
#include <cstdint>
Expand All @@ -17,18 +17,18 @@ struct HashTableConfig {
using ValueSize = IReadOnlyHashTable::Value::size_type;

explicit Setting(std::uint32_t numBuckets,
boost::optional<std::uint32_t> numBucketsPerMutex = {},
boost::optional<KeySize> fixedKeySize = {},
boost::optional<ValueSize> fixedValueSize = {})
std::optional<std::uint32_t> numBucketsPerMutex = {},
std::optional<KeySize> fixedKeySize = {},
std::optional<ValueSize> fixedValueSize = {})
: m_numBuckets{numBuckets},
m_numBucketsPerMutex{numBucketsPerMutex},
m_fixedKeySize{fixedKeySize},
m_fixedValueSize{fixedValueSize} {}

std::uint32_t m_numBuckets;
boost::optional<std::uint32_t> m_numBucketsPerMutex;
boost::optional<KeySize> m_fixedKeySize;
boost::optional<ValueSize> m_fixedValueSize;
std::optional<std::uint32_t> m_numBucketsPerMutex;
std::optional<KeySize> m_fixedKeySize;
std::optional<ValueSize> m_fixedValueSize;
};

struct Cache {
Expand All @@ -48,17 +48,17 @@ struct HashTableConfig {
using Properties = Utils::Properties;

Serializer(std::shared_ptr<std::istream> stream = {},
boost::optional<Properties> properties = {})
std::optional<Properties> properties = {})
: m_stream{stream}, m_properties{properties} {}

std::shared_ptr<std::istream> m_stream;
boost::optional<Properties> m_properties;
std::optional<Properties> m_properties;
};

HashTableConfig(std::string name,
Setting setting,
boost::optional<Cache> cache = {},
boost::optional<Serializer> serializer = {})
std::optional<Cache> cache = {},
std::optional<Serializer> serializer = {})
: m_name{std::move(name)},
m_setting{std::move(setting)},
m_cache{cache},
Expand All @@ -69,8 +69,8 @@ struct HashTableConfig {

std::string m_name;
Setting m_setting;
boost::optional<Cache> m_cache;
boost::optional<Serializer> m_serializer;
std::optional<Cache> m_cache;
std::optional<Serializer> m_serializer;
};

} // namespace L4
5 changes: 3 additions & 2 deletions inc/L4/HashTable/IHashTable.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <cstdint>
#include <cstring>
#include <iosfwd>
#include "Log/PerfCounter.h"
#include "Utils/Properties.h"
Expand All @@ -21,7 +22,7 @@ struct IReadOnlyHashTable {
}

bool operator==(const Blob& other) const {
return (m_size == other.m_size) && !memcmp(m_data, other.m_data, m_size);
return (m_size == other.m_size) && !std::memcmp(m_data, other.m_data, m_size);
}

bool operator!=(const Blob& other) const { return !(*this == other); }
Expand Down Expand Up @@ -80,4 +81,4 @@ struct IWritableHashTable::ISerializer {
const Utils::Properties& properties) = 0;
};

} // namespace L4
} // namespace L4
6 changes: 3 additions & 3 deletions inc/L4/HashTable/ReadWrite/HashTable.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once

#include <boost/optional.hpp>
#include <cstdint>
#include <mutex>
#include <optional>
#include "Epoch/IEpochActionManager.h"
#include "HashTable/Common/Record.h"
#include "HashTable/Common/SharedHashTable.h"
Expand Down Expand Up @@ -32,10 +32,10 @@ class ReadOnlyHashTable : public virtual IReadOnlyHashTable {

explicit ReadOnlyHashTable(
HashTable& hashTable,
boost::optional<RecordSerializer> recordSerializer = boost::none)
std::optional<RecordSerializer> recordSerializer = std::nullopt)
: m_hashTable{hashTable},
m_recordSerializer{
recordSerializer
recordSerializer.has_value()
? *recordSerializer
: RecordSerializer{m_hashTable.m_setting.m_fixedKeySize,
m_hashTable.m_setting.m_fixedValueSize}} {}
Expand Down
7 changes: 4 additions & 3 deletions inc/L4/HashTable/ReadWrite/Serializer.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#pragma once

#include <boost/format.hpp>
#include <cstdint>
#include <iosfwd>
#include <iostream>
#include <sstream>
#include "Epoch/IEpochActionManager.h"
#include "Log/PerfCounter.h"
#include "Serialization/SerializerHelper.h"
Expand Down Expand Up @@ -203,8 +204,8 @@ class Deserializer {
m_properties}
.Deserialize(memory, stream);
default:
boost::format err("Unsupported version '%1%' is given.");
err % version;
std::ostringstream err;
err << std::string("Unsupported version ") << version << std::string(" is given.");
throw RuntimeException(err.str());
}
}
Expand Down
10 changes: 7 additions & 3 deletions inc/L4/Interprocess/Container/Vector.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
#pragma once

#include <boost/interprocess/containers/vector.hpp>
// #include <boost/interprocess/containers/vector.hpp>
#include <vector>

namespace L4 {
namespace Interprocess {
namespace Container {

// template <typename T, typename Allocator>
// using Vector = boost::interprocess::vector<T, Allocator>;
// @jerinphilip: This is probably a bad idea.
template <typename T, typename Allocator>
using Vector = boost::interprocess::vector<T, Allocator>;
using Vector = std::vector<T, Allocator>;

} // namespace Container
} // namespace Interprocess
} // namespace L4
} // namespace L4
4 changes: 2 additions & 2 deletions inc/L4/LocalMemory/EpochManager.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once

#include <atomic>
#include <boost/thread/shared_lock_guard.hpp>
#include <mutex>
#include <shared_mutex>
#include "Epoch/Config.h"
#include "Epoch/EpochActionManager.h"
#include "Epoch/EpochQueue.h"
Expand All @@ -19,7 +19,7 @@ namespace LocalMemory {
class EpochManager : public IEpochActionManager {
public:
using TheEpochQueue =
EpochQueue<boost::shared_lock_guard<Utils::ReaderWriterLockSlim>,
EpochQueue<std::shared_lock<Utils::ReaderWriterLockSlim>,
std::lock_guard<Utils::ReaderWriterLockSlim>>;

using TheEpochRefManager = EpochRefManager<TheEpochQueue>;
Expand Down
Loading

0 comments on commit 8103d00

Please sign in to comment.