diff --git a/src/ds/bits.h b/src/ds/bits.h index 4aa8e11..68df89e 100644 --- a/src/ds/bits.h +++ b/src/ds/bits.h @@ -452,5 +452,17 @@ namespace snmalloc return (size_t)1 << (m_e + LOW_BITS); } } + + template + constexpr inline T min(T t1, T t2) + { + return t1 < t2 ? t1 : t2; + } + + template + constexpr inline T max(T t1, T t2) + { + return t1 > t2 ? t1 : t2; + } } } diff --git a/src/ds/mpscq.h b/src/ds/mpscq.h index e7395f1..1993e94 100644 --- a/src/ds/mpscq.h +++ b/src/ds/mpscq.h @@ -1,9 +1,7 @@ #pragma once #include "bits.h" - -#include -#include +#include "helpers.h" namespace snmalloc { diff --git a/src/mem/alloc.h b/src/mem/alloc.h index efd7d4e..a344ec8 100644 --- a/src/mem/alloc.h +++ b/src/mem/alloc.h @@ -21,8 +21,6 @@ #include "sizeclasstable.h" #include "slab.h" -#include - namespace snmalloc { enum Boundary diff --git a/src/mem/globalalloc.h b/src/mem/globalalloc.h index 9471264..5a4921a 100644 --- a/src/mem/globalalloc.h +++ b/src/mem/globalalloc.h @@ -49,6 +49,7 @@ namespace snmalloc } } +#ifdef USE_SNMALLOC_STATS void print_all_stats(std::ostream& o, uint64_t dumpid = 0) { auto alloc = Parent::iterate(); @@ -59,6 +60,12 @@ namespace snmalloc alloc = Parent::iterate(alloc); } } +#else + void print_all_stats(void*& o, uint64_t dumpid = 0) + { + UNUSED(o); UNUSED(dumpid); + } +#endif void cleanup_unused() { diff --git a/src/mem/largealloc.h b/src/mem/largealloc.h index ab89d11..6ac38cf 100644 --- a/src/mem/largealloc.h +++ b/src/mem/largealloc.h @@ -8,8 +8,6 @@ #include "baseslab.h" #include "sizeclass.h" -#include - namespace snmalloc { template @@ -61,7 +59,7 @@ namespace snmalloc size_t bump; size_t remaining; - std::pair reserve_block() noexcept + void new_block() { size_t size = SUPERSLAB_SIZE; void* r = reserve(&size, SUPERSLAB_SIZE); @@ -70,14 +68,9 @@ namespace snmalloc error("out of memory"); ((PAL*)this)->template notify_using(r, OS_PAGE_SIZE); - return std::make_pair(r, size); - } - void new_block() - { - auto r_size = reserve_block(); - bump = (size_t)r_size.first; - remaining = r_size.second; + bump = (size_t)r; + remaining = size; } /** diff --git a/src/mem/pagemap.h b/src/mem/pagemap.h index 9fae7a3..1ca4688 100644 --- a/src/mem/pagemap.h +++ b/src/mem/pagemap.h @@ -1,9 +1,10 @@ #pragma once #include "../ds/bits.h" +#include "../ds/helpers.h" -#include #include +#include namespace snmalloc { @@ -274,7 +275,7 @@ namespace snmalloc auto leaf_ix = get_leaf_index(p, success); size_t ix = leaf_ix.second; - auto last = std::min(LEAF_MASK + 1, ix + length); + auto last = bits::min(LEAF_MASK + 1, ix + length); auto diff = last - ix; diff --git a/src/mem/sizeclasstable.h b/src/mem/sizeclasstable.h index 64e0347..877c39d 100644 --- a/src/mem/sizeclasstable.h +++ b/src/mem/sizeclasstable.h @@ -30,7 +30,7 @@ namespace snmalloc bits::from_exp_mant(sizeclass); size_t alignment = - std::min((size_t)1 << bits::ctz_const(size[sizeclass]), OS_PAGE_SIZE); + bits::min((size_t)1 << bits::ctz_const(size[sizeclass]), OS_PAGE_SIZE); cache_friendly_mask[sizeclass] = (alignment - 1); inverse_cache_friendly_mask[sizeclass] = ~(alignment - 1); } diff --git a/src/mem/superslab.h b/src/mem/superslab.h index 9105b8b..1c127a6 100644 --- a/src/mem/superslab.h +++ b/src/mem/superslab.h @@ -3,8 +3,7 @@ #include "../ds/helpers.h" #include "allocslab.h" #include "metaslab.h" - -#include +#include namespace snmalloc { diff --git a/src/mem/threadalloc.h b/src/mem/threadalloc.h index 8914aec..032fa39 100644 --- a/src/mem/threadalloc.h +++ b/src/mem/threadalloc.h @@ -7,6 +7,10 @@ #error At most one out of SNMALLOC_USE_THREAD_CLEANUP and SNMALLOC_USE_THREAD_DESTRUCTOR may be defined. #endif +#if !defined(_WIN32) && !defined(FreeBSD_KERNEL) +# include "pthread.h" +#endif + namespace snmalloc { extern "C" void _malloc_thread_cleanup(void); diff --git a/src/override/malloc.cc b/src/override/malloc.cc index 768b619..18ddab6 100644 --- a/src/override/malloc.cc +++ b/src/override/malloc.cc @@ -2,6 +2,7 @@ #include "../snmalloc.h" #include +#include using namespace snmalloc; @@ -89,7 +90,7 @@ extern "C" if (p != nullptr) { assert(p == Alloc::external_pointer(p)); - sz = (std::min)(size, sz); + sz = bits::min(size, sz); memcpy(p, ptr, sz); SNMALLOC_NAME_MANGLE(free)(ptr); } @@ -135,7 +136,7 @@ extern "C" return nullptr; } - size = (std::max)(size, alignment); + size = bits::max(size, alignment); uint8_t sc = size_to_sizeclass(size); if (sc >= NUM_SIZECLASSES) { diff --git a/src/pal/pal_apple.h b/src/pal/pal_apple.h index 4c5e643..f1c92d2 100644 --- a/src/pal/pal_apple.h +++ b/src/pal/pal_apple.h @@ -5,10 +5,11 @@ # include "../mem/allocconfig.h" # include -# include # include # include +int puts ( const char * str ); + namespace snmalloc { /** diff --git a/src/pal/pal_freebsd.h b/src/pal/pal_freebsd.h index 122b8b9..1c8b04e 100644 --- a/src/pal/pal_freebsd.h +++ b/src/pal/pal_freebsd.h @@ -4,7 +4,6 @@ # include "../ds/bits.h" # include "../mem/allocconfig.h" -# include # include # include # include diff --git a/src/pal/pal_linux.h b/src/pal/pal_linux.h index f1e2d01..5ded0a9 100644 --- a/src/pal/pal_linux.h +++ b/src/pal/pal_linux.h @@ -4,10 +4,11 @@ # include "../ds/bits.h" # include "../mem/allocconfig.h" -# include # include # include +int puts ( const char * str ); + namespace snmalloc { class PALLinux