From 036cf2da06aa09897d05fa713fa1da2c600d417a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20M=C3=BCller?= Date: Tue, 12 Apr 2016 22:05:19 +0200 Subject: [PATCH 1/5] Fix crash in detail::ordered_free_memory_list When allocating arrays, last_dealloc_ wasn't properly updated if removing a sequence starting with the first node. This was because less(i.prev, last_dealloc_) doesn't work as intended when i.prev is the proxy. Change comparison to less_equal with i.first which is never proxy. Likewise for end comparison. Also add less_equal()/greater_equal() for pointers. --- src/detail/free_list.cpp | 3 ++- src/detail/free_list_utils.hpp | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/detail/free_list.cpp b/src/detail/free_list.cpp index 8b19c03b..e5a73cf1 100644 --- a/src/detail/free_list.cpp +++ b/src/detail/free_list.cpp @@ -479,7 +479,8 @@ void* ordered_free_memory_list::allocate(std::size_t n) FOONATHAN_NOEXCEPT xor_list_change(i.next, i.last, i.prev); // change prev pointer from i.next to i.prev capacity_ -= i.size(actual_size); - if (less(i.prev, last_dealloc_) && less(last_dealloc_, i.next)) + // if last_dealloc_ points into the array being removed + if (less_equal(i.first, last_dealloc_) && less_equal(last_dealloc_, i.last)) { // move last_dealloc just outside range last_dealloc_ = i.next; diff --git a/src/detail/free_list_utils.hpp b/src/detail/free_list_utils.hpp index b094a813..c04be145 100644 --- a/src/detail/free_list_utils.hpp +++ b/src/detail/free_list_utils.hpp @@ -122,6 +122,11 @@ namespace foonathan { namespace memory #endif } + inline bool less_equal(void *a, void *b) FOONATHAN_NOEXCEPT + { + return a == b || less(a, b); + } + inline bool greater(void *a, void *b) FOONATHAN_NOEXCEPT { #if FOONATHAN_HOSTED_IMPLEMENTATION @@ -130,6 +135,11 @@ namespace foonathan { namespace memory return to_int(a) < to_int(b); #endif } + + inline bool greater_equal(void *a, void *b) FOONATHAN_NOEXCEPT + { + return a == b || greater(a, b); + } } // namespace detail }} // namespace foonathan::memory From 166c76a727680d0419a14f53dc474f790ca6d727 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20M=C3=BCller?= Date: Tue, 12 Apr 2016 22:06:53 +0200 Subject: [PATCH 2/5] Add CMD arg specifying sample_size to profiling Passing 1 allows using profiling as more deep testing. --- test/benchmark.hpp | 2 +- test/profiling.cpp | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/test/benchmark.hpp b/test/benchmark.hpp index cc10c566..d9bda50f 100644 --- a/test/benchmark.hpp +++ b/test/benchmark.hpp @@ -26,7 +26,7 @@ std::size_t measure(F func, Args&&... args) return std::size_t(duration.count()); } -const std::size_t sample_size = 1024u; +std::size_t sample_size = 1024u; template std::size_t benchmark(F measure_func, Alloc make_alloc, Args&& ... args) diff --git a/test/profiling.cpp b/test/profiling.cpp index d06fbafc..43b66aae 100644 --- a/test/profiling.cpp +++ b/test/profiling.cpp @@ -114,8 +114,11 @@ void benchmark_array(std::initializer_list counts, benchmark_array(counts, node_sizes, array_sizes); } -int main() +int main(int argc, char *argv[]) { + if (argc >= 2) + sample_size = std::size_t(std::atoi(argv[1])); + class comma_numpunct : public std::numpunct { protected: From 6c09f598eb03b4a5960a59cb48f1f91ed03ee387 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20M=C3=BCller?= Date: Tue, 12 Apr 2016 22:08:02 +0200 Subject: [PATCH 3/5] Add profiling run to Travis CI This will hopefully prevent issues such as the last crash. --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 9955fb8c..d2a22c56 100644 --- a/.travis.yml +++ b/.travis.yml @@ -23,4 +23,5 @@ script: - mkdir $CXX && cd $CXX - ../cmake-3.3.1-Linux-x86_64/bin/cmake -DCMAKE_BUILD_TYPE=$build_type -DCMAKE_CXX_FLAGS="-Wall -Wextra -pedantic -Wno-parentheses" ../ - ../cmake-3.3.1-Linux-x86_64/bin/cmake --build . - - ./test/foonathan_memory_test \ No newline at end of file + - ./test/foonathan_memory_test + - ./test/foonathan_memory_profiling 1 \ No newline at end of file From dff047434d09e2df3358dec1da59936872ded901 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20M=C3=BCller?= Date: Tue, 12 Apr 2016 22:16:09 +0200 Subject: [PATCH 4/5] Fix non-critical bug in small_free_memory_list::find_chunk() It didn't actually search into both directions as advertised. This has no significant impact on the profiling results but should be fixed anyways. --- src/detail/small_free_list.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/detail/small_free_list.cpp b/src/detail/small_free_list.cpp index 6a3e457b..c81e2683 100644 --- a/src/detail/small_free_list.cpp +++ b/src/detail/small_free_list.cpp @@ -344,8 +344,10 @@ chunk* small_free_memory_list::find_chunk_impl(std::size_t n) FOONATHAN_NOEXCEPT return c; cur_forward = cur_forward->next; - cur_backward = cur_backward->next; - } while (cur_forward != cur_backward); + cur_backward = cur_backward->prev; + FOONATHAN_MEMORY_ASSERT(cur_forward != alloc_chunk_); + FOONATHAN_MEMORY_ASSERT(cur_backward != alloc_chunk_); + } while (true); FOONATHAN_MEMORY_UNREACHABLE("there is memory available somewhere..."); return nullptr; } From 1cf90795d267000cc8cc481c05bd2c9c5a97962a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20M=C3=BCller?= Date: Tue, 12 Apr 2016 22:17:15 +0200 Subject: [PATCH 5/5] Fixes #7 --- README.md | 2 +- doc/index.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e3b391f1..107a53e5 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ In addition: ## Basic example ```cpp -include +#include #include #include diff --git a/doc/index.md b/doc/index.md index 8ce72087..8cf36980 100644 --- a/doc/index.md +++ b/doc/index.md @@ -38,7 +38,7 @@ The concepts of this library are defined are [here](md_doc_concepts.html). ## Basic example ```cpp - include + #include #include #include