From 74185a9b6030c5351dd11ff5dce1541bffd0bc15 Mon Sep 17 00:00:00 2001 From: Theodor Date: Sat, 28 May 2022 14:02:21 +0300 Subject: [PATCH] Updated SharedGcPtr. Removed stl_allocator interface - Closes #47. --- CMakeLists.txt | 16 --- README.md | 7 +- example_project/src/main.cpp | 16 +-- libmemplusplus/CMakeLists.txt | 13 --- .../include/mpplib/shared_gcptr-imp.hpp | 11 +++ .../include/mpplib/shared_gcptr.hpp | 10 ++ .../mpplib/stl_allocator_interface.hpp | 99 ------------------- mempp.code-workspace | 3 + 8 files changed, 29 insertions(+), 146 deletions(-) delete mode 100644 libmemplusplus/include/mpplib/stl_allocator_interface.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 3657140..de34624 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,7 +24,6 @@ option(MPP_USE_FUZZER_DEFINITIONS "Build mpp with fuzzing-friendly defines" OFF) option(MPP_BUILD_EXAMPLE "Build mpp example project" ON) option(MPP_BUILD_TESTS "Build mpp tests" ON) option(MPP_BUILD_DOCS "Build mpp documentation" OFF) -option(MPP_BUILD_BENCHMARKS "Build mpp benchmark (tcmalloc, jemalloc, malloc, mpp)" ON) option(CMAKE_EXPORT_COMPILE_COMMANDS "Request generate of json file with all build commands" ON) message(STATUS "##################### GLOBAL OPTIONS #####################") @@ -34,7 +33,6 @@ message(STATUS "MPP_USE_FUZZER_DEFINITIONS : ${MPP_USE_FUZZER_DEFINITIONS}") message(STATUS "MPP_BUILD_EXAMPLE : ${MPP_BUILD_EXAMPLE}") message(STATUS "MPP_BUILD_TESTS : ${MPP_BUILD_TESTS}") message(STATUS "MPP_BUILD_DOCS : ${MPP_BUILD_DOCS}") -message(STATUS "MPP_BUILD_BENCHMARKS : ${MPP_BUILD_BENCHMARKS}") ################# ADD PROJECTS + DOCS + TESTS ################# add_subdirectory(libmemplusplus) @@ -74,19 +72,5 @@ if (MPP_BUILD_DOCS) add_subdirectory(docs) endif() -if (MPP_BUILD_BENCHMARKS) - include_directories("libraries") - - message(STATUS "[+] Building with benchmark") - enable_testing() - - add_subdirectory(libraries/googletest) - set(BENCHMARK_ENABLE_GTEST_TESTS OFF CACHE BOOL "Disable google benchmark tests" FORCE) - set(BENCHMARK_ENABLE_TESTING OFF CACHE BOOL "Disable google benchmark tests" FORCE) - add_subdirectory(libraries/benchmark) - - add_subdirectory(memplusplus-benchmarks) -endif() - ################# CLANG FORMAT / CLANG-TIDY ################# include(cmake/clang-cxx-dev-tools.cmake) \ No newline at end of file diff --git a/README.md b/README.md index e540fd0..a879450 100644 --- a/README.md +++ b/README.md @@ -27,8 +27,9 @@ Current library version: 2.3.5 1. Install latest build systems: `apt install cmake g++ clang` 2. Clone just the library: `git clone https://github.com/m4drat/memplusplus/` -3. Clone the library (with benchmarks and tests support): `git clone --recurse-submodules https://github.com/m4drat/memplusplus/` -4. Clone the library (with tests support): `git clone --recurse-submodules=./libraries/Catch2 https://github.com/m4drat/memplusplus/` +3. Clone the library (with tests support): `git clone --recurse-submodules=./libraries/Catch2 https://github.com/m4drat/memplusplus/` +4. Benchmarks (can be found in separate git repo): `git clone https://github.com/m4drat/memplusplus-benchmarks` + 1. Run them as follows: `cd memplusplus-benchmarks && ./run_all.sh` ### How to use the library as a dependency (external project) @@ -92,8 +93,6 @@ Global options: - `MPP_BUILD_DOCS` - build documentation -- `MPP_BUILD_BENCHMARKS` - build benchmarks - Library options: - `MPP_BUILD_SHARED_LIBS` - build shared or static libraries diff --git a/example_project/src/main.cpp b/example_project/src/main.cpp index 1fbc842..8a9b105 100644 --- a/example_project/src/main.cpp +++ b/example_project/src/main.cpp @@ -7,6 +7,7 @@ #include "mpplib/containers/gc_graph.hpp" #include "mpplib/gc.hpp" #include "mpplib/shared_gcptr.hpp" +#include "mpplib/stl_allocator_interface.hpp" #include "mpplib/utils/profiler_definitions.hpp" using namespace mpp; @@ -30,20 +31,7 @@ void logic() using namespace mpp; using namespace std::literals::chrono_literals; - char* ptr = (char*)MM::Allocate(1024); - SharedGcPtr a(ptr); - SharedGcPtr b = MakeSharedGcPtr(1337); - SharedGcPtr c = MakeSharedGcPtr(1337); - SharedGcPtr d = MakeSharedGcPtr(1337); - - a = nullptr; - // b = nullptr; - c = nullptr; - // d = nullptr; - - GC::GetInstance().Collect(); - // Should trigger invalid initialization - // SharedGcPtr b(ptr); + SharedGcPtr b = MakeSharedGcPtr(1337); } int main() diff --git a/libmemplusplus/CMakeLists.txt b/libmemplusplus/CMakeLists.txt index 8226077..51efb13 100644 --- a/libmemplusplus/CMakeLists.txt +++ b/libmemplusplus/CMakeLists.txt @@ -22,19 +22,6 @@ option(MPP_PROFILE "Build mpp with support of profiling" ON) option(MPP_COLOUR "Build mpp with support of coloured output" ON) option(MPP_STATS "Build mpp with support of statistics dumping" ON) -################# BENCHMARKS OPTIONS CHECKING ################# -if (MPP_BUILD_BENCHMARKS MATCHES "ON") - if (MPP_FULL_DEBUG MATCHES "ON") - message(WARNING "Building benchmarks in MPP_FULL_DEBUG mode! This may show inaccurate benchmark results.") - endif() - if (MPP_PROFILE MATCHES "ON") - message(WARNING "Building benchmarks with profiling enabled! This may show inaccurate benchmark results.") - endif() - if (MPP_STATS MATCHES "ON") - message(WARNING "Building benchmarks with statistics enabled! This may show inaccurate benchmark results.") - endif() -endif() - ################# DEFAULT BUILD TYPE ################# if (NOT CMAKE_BUILD_TYPE) if ("${CMAKE_BINARY_DIR}" MATCHES ".*(D|d)ebug$") diff --git a/libmemplusplus/include/mpplib/shared_gcptr-imp.hpp b/libmemplusplus/include/mpplib/shared_gcptr-imp.hpp index b3a0ad1..554fe8e 100644 --- a/libmemplusplus/include/mpplib/shared_gcptr-imp.hpp +++ b/libmemplusplus/include/mpplib/shared_gcptr-imp.hpp @@ -201,6 +201,17 @@ namespace mpp { return *m_objectPtr; } + template + SharedGcPtr::operator bool() const + { + return Get() != nullptr; + } + + template + ptrdiff_t SharedGcPtr::operator-(const SharedGcPtr& t_other) const noexcept { + return Get() - t_other.Get(); + } + template bool SharedGcPtr::AddToGcList() { diff --git a/libmemplusplus/include/mpplib/shared_gcptr.hpp b/libmemplusplus/include/mpplib/shared_gcptr.hpp index 6554b62..b9e1fd7 100644 --- a/libmemplusplus/include/mpplib/shared_gcptr.hpp +++ b/libmemplusplus/include/mpplib/shared_gcptr.hpp @@ -139,6 +139,16 @@ namespace mpp { */ Type& operator*() const noexcept; + /** + * @brief Allows automatic conversions to bool. + */ + explicit operator bool() const; + + /** + * @brief Calculates distance between two pointers + */ + ptrdiff_t operator-(const SharedGcPtr& t_other) const noexcept; + /** * @brief Tries to add SharedGcPtr to list of all active GcPtr's. * @return true if succeed, false otherwise diff --git a/libmemplusplus/include/mpplib/stl_allocator_interface.hpp b/libmemplusplus/include/mpplib/stl_allocator_interface.hpp deleted file mode 100644 index 4008f33..0000000 --- a/libmemplusplus/include/mpplib/stl_allocator_interface.hpp +++ /dev/null @@ -1,99 +0,0 @@ -#pragma once - -#include "mpplib/memory_manager.hpp" - -namespace mpp { - /** - * @brief MppStlAllocator class. Implements basic stl allocator. - * @tparam Type of user object. - */ - template - struct MppStlAllocator - { - typedef std::size_t size_type; - typedef ptrdiff_t difference_type; - typedef T* pointer; - typedef const T* const_pointer; - typedef T& reference; - typedef const T& const_reference; - typedef T value_type; - - MppStlAllocator() = default; - - // template struct rebind { typedef MppStlAllocator other; }; - template - MppStlAllocator(const MppStlAllocator&) - {} - - pointer address(reference x) const - { - return &x; - } - - const_pointer address(const_reference x) const - { - return &x; - } - - size_type max_size() const throw() - { - return std::size_t(-1) / sizeof(value_type); - } - - /** - * @brief Allocates memory using mpp::MemoryManager::Allocate(). - * @param n - * @return pointer to object - */ - pointer allocate(size_type n) - { - return static_cast(mpp::MemoryManager::Allocate(n * sizeof(T))); - } - - /** - * @brief Deallocates memory using mpp::MemoryManager::Deallocate(). - * @param p - pointer to object - * @param n - */ - void deallocate(pointer p, size_type n) - { - mpp::MemoryManager::Deallocate(static_cast(p)); - } - - /** - * @brief Constructs object. - * @param p pointer to object to construct - * @param val - */ - void construct(pointer p, const T& val) - { - new (static_cast(p)) T(val); - } - - void construct(pointer p) - { - new (static_cast(p)) T(); - } - - /** - * @brief Destroy object. - * @param p - */ - void destroy(pointer p) - { - p->~T(); - } - }; - - template - bool operator==(const MppStlAllocator&, const MppStlAllocator&) - { - return true; - } - - template - bool operator!=(const MppStlAllocator&, const MppStlAllocator&) - { - return false; - } -} \ No newline at end of file diff --git a/mempp.code-workspace b/mempp.code-workspace index 3ab3552..fccba7e 100644 --- a/mempp.code-workspace +++ b/mempp.code-workspace @@ -2,6 +2,9 @@ "folders": [ { "path": "." + }, + { + "path": "../memplusplus-benchmarks" } ], "settings": {