diff --git a/CMakeLists.txt b/CMakeLists.txt index 7ecdaee7..f5bd7b07 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -381,6 +381,17 @@ foreach(_m ${PIP_SRC_MODULES}) endforeach() set_target_properties(pip PROPERTIES DEFINE_SYMBOL pip_EXPORTS) +# Override containers minimum bytes allocation +if(NOT "x${PIP_CONTAINERS_MIN_ALLOC}" STREQUAL "x") + target_compile_definitions(pip PRIVATE "-DPIP_CONTAINERS_MIN_ALLOC=${PIP_CONTAINERS_MIN_ALLOC}") + message(STATUS "Attention: Override PIP_CONTAINERS_MIN_ALLOC = ${PIP_CONTAINERS_MIN_ALLOC}") +endif() +# Override containers maximum bytes for power of two expansion, may be bytes or X_KiB, or X_MiB +if(NOT "x${PIP_CONTAINERS_MAX_POT_ALLOC}" STREQUAL "x") + target_compile_definitions(pip PRIVATE "-DPIP_CONTAINERS_MAX_POT_ALLOC=${PIP_CONTAINERS_MAX_POT_ALLOC}") + message(STATUS "Attention: Override PIP_CONTAINERS_MAX_POT_ALLOC = ${PIP_CONTAINERS_MAX_POT_ALLOC}") +endif() + if (NOT CROSSTOOLS) if (NOT PIP_FREERTOS) diff --git a/libs/main/containers/picontainers.cpp b/libs/main/containers/picontainers.cpp index f75eed09..475e3514 100644 --- a/libs/main/containers/picontainers.cpp +++ b/libs/main/containers/picontainers.cpp @@ -22,8 +22,21 @@ #include "piliterals_bytes.h" -const size_t minAlloc = 64; -const size_t maxPoTAlloc = 64_MiB; +#if defined(PIP_CONTAINERS_MIN_ALLOC) +# define ACTUAL_MIN_ALLOC PIP_CONTAINERS_MIN_ALLOC +#else +# define ACTUAL_MIN_ALLOC 64 +#endif + +#if defined(PIP_CONTAINERS_MAX_POT_ALLOC) +# define ACTUAL_MAX_POT_ALLOC PIP_CONTAINERS_MAX_POT_ALLOC +#else +# define ACTUAL_MAX_POT_ALLOC 64_MiB +#endif + + +const size_t minAlloc = ACTUAL_MIN_ALLOC; +const size_t maxPoTAlloc = ACTUAL_MAX_POT_ALLOC; size_t _PIContainerConstantsBase::calcMinCountPoT(size_t szof) { diff --git a/libs/main/containers/picontainers.h b/libs/main/containers/picontainers.h index 98fff3dc..2434f818 100644 --- a/libs/main/containers/picontainers.h +++ b/libs/main/containers/picontainers.h @@ -89,6 +89,26 @@ public: static const size_t ret = _PIContainerConstantsBase::calcStepAfterPoT(sizeof(T)); return ret; } + + static size_t calcNewSize(size_t old_size, size_t new_size) { + if (new_size == 0) return 0; + if (new_size < maxCountForPoT()) { + if (old_size * 2 >= new_size && old_size < new_size) { + return old_size * 2; + } + ssize_t t = minCountPoT(); + new_size -= 1; + while (new_size >> t) + ++t; + return (1 << t); + } else { + size_t ret = old_size; + while (ret < new_size) + ret += stepAfterPoT(); + return ret; + } + return 0; + } }; diff --git a/libs/main/containers/pideque.h b/libs/main/containers/pideque.h index 83fd1526..0e0ba6ff 100644 --- a/libs/main/containers/pideque.h +++ b/libs/main/containers/pideque.h @@ -2575,27 +2575,6 @@ private: pid_data = nullptr; } - inline size_t asize(ssize_t ss) { - if (ss <= 0) return 0; - size_t s = ss; - if (s < _PIContainerConstants::maxCountForPoT()) { - if (pid_rsize * 2 >= s && pid_rsize < s) { - return pid_rsize * 2; - } - ssize_t t = _PIContainerConstants::minCountPoT(); - s -= 1; - while (s >> t) - ++t; - return (1 << t); - } else { - size_t ret = pid_rsize; - while (ret < s) - ret += _PIContainerConstants::stepAfterPoT(); - return ret; - } - return 0; - } - template::value, int>::type = 0> inline void newT(T * dst, const T * src, size_t s) { PIINTROSPECTION_CONTAINER_USED(T, s) @@ -2690,35 +2669,38 @@ private: if (pid_start > 0) checkMove(); return; } - pid_size = new_size; - const size_t as = asize(pid_start + new_size); - if (as != pid_rsize) { + pid_size = new_size; + const size_t new_rsize = _PIContainerConstants::calcNewSize(pid_rsize, pid_start + new_size); + if (new_rsize != pid_rsize) { PIINTROSPECTION_CONTAINER_ALLOC(T, (as - pid_rsize)) - T * p_d = reinterpret_cast(realloc(reinterpret_cast(pid_data), as * sizeof(T))); + T * new_data = reinterpret_cast(realloc(reinterpret_cast(pid_data), new_rsize * sizeof(T))); #ifndef NDEBUG - if (!p_d) { + if (!new_data) { fprintf(stderr, "error with PIDeque<%s>::alloc\n", __PIP_TYPENAME__(T)); } #endif assert(p_d); - pid_data = p_d; - pid_rsize = as; + pid_data = new_data; + pid_rsize = new_rsize; } } inline void alloc_backward(size_t new_size, ssize_t start_offset = 0) { - const size_t as = ssize_t(pid_start) + start_offset < 0 ? asize(pid_rsize - start_offset) : pid_rsize; - if (as > pid_rsize) { - T * td = reinterpret_cast(malloc(as * sizeof(T))); - const size_t ns = pid_start + as - pid_rsize; + const size_t new_rsize = + ssize_t(pid_start) + start_offset < 0 ? _PIContainerConstants::calcNewSize(pid_rsize, pid_rsize - start_offset) : pid_rsize; + if (new_rsize > pid_rsize) { + T * tmp_data = reinterpret_cast(malloc(new_rsize * sizeof(T))); + const size_t new_start = pid_start + new_rsize - pid_rsize; PIINTROSPECTION_CONTAINER_ALLOC(T, (as - pid_rsize)) - if (pid_rsize > 0 && pid_data != 0) { - memcpy(reinterpret_cast(td + ns), reinterpret_cast(pid_data + pid_start), pid_size * sizeof(T)); + if (pid_rsize > 0 && pid_data) { + memcpy(reinterpret_cast(tmp_data + new_start), + reinterpret_cast(pid_data + pid_start), + pid_size * sizeof(T)); dealloc(); } - pid_data = td; - pid_rsize = as; - pid_start = ns; + pid_data = tmp_data; + pid_rsize = new_rsize; + pid_start = new_start; } pid_start += start_offset; pid_size = new_size; diff --git a/libs/main/containers/pivector.h b/libs/main/containers/pivector.h index 1e1c7d13..c480988c 100644 --- a/libs/main/containers/pivector.h +++ b/libs/main/containers/pivector.h @@ -2459,26 +2459,6 @@ private: piv_data = nullptr; } - inline size_t asize(size_t s) { - if (s == 0) return 0; - if (s < _PIContainerConstants::maxCountForPoT()) { - if (piv_rsize * 2 >= s && piv_rsize < s) { - return piv_rsize * 2; - } - ssize_t t = _PIContainerConstants::minCountPoT(); - s -= 1; - while (s >> t) - ++t; - return (1 << t); - } else { - size_t ret = piv_rsize; - while (ret < s) - ret += _PIContainerConstants::stepAfterPoT(); - return ret; - } - return 0; - } - template::value, int>::type = 0> inline void newT(T * dst, const T * src, size_t s) { PIINTROSPECTION_CONTAINER_USED(T, s) @@ -2566,19 +2546,19 @@ private: piv_size = new_size; return; } - piv_size = new_size; - const size_t as = asize(new_size); - if (as == piv_rsize) return; + piv_size = new_size; + const size_t new_rsize = _PIContainerConstants::calcNewSize(piv_rsize, new_size); + if (new_rsize == piv_rsize) return; PIINTROSPECTION_CONTAINER_ALLOC(T, (as - piv_rsize)) - T * p_d = reinterpret_cast(realloc(reinterpret_cast(piv_data), as * sizeof(T))); + T * new_data = reinterpret_cast(realloc(reinterpret_cast(piv_data), new_rsize * sizeof(T))); #ifndef NDEBUG - if (!p_d) { + if (!new_data) { fprintf(stderr, "error with PIVector<%s>::alloc\n", __PIP_TYPENAME__(T)); } #endif assert(p_d); - piv_data = p_d; - piv_rsize = as; + piv_data = new_data; + piv_rsize = new_rsize; } T * piv_data = nullptr;