take out "asize" from vector/dequeue to picontainers.h->calcNewSize()

minAlloc and maxPoTAlloc now can be override from CMake by PIP_CONTAINERS_MIN_ALLOC and PIP_CONTAINERS_MAX_POT_ALLOC variables
This commit is contained in:
2024-11-21 20:15:18 +03:00
parent 53ec75bf0c
commit bf9ad65ff0
5 changed files with 72 additions and 66 deletions

View File

@@ -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)

View File

@@ -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) {

View File

@@ -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;
}
};

View File

@@ -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<T>::maxCountForPoT()) {
if (pid_rsize * 2 >= s && pid_rsize < s) {
return pid_rsize * 2;
}
ssize_t t = _PIContainerConstants<T>::minCountPoT();
s -= 1;
while (s >> t)
++t;
return (1 << t);
} else {
size_t ret = pid_rsize;
while (ret < s)
ret += _PIContainerConstants<T>::stepAfterPoT();
return ret;
}
return 0;
}
template<typename T1 = T, typename std::enable_if<!std::is_trivially_copyable<T1>::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<T>::calcNewSize(pid_rsize, pid_start + new_size);
if (new_rsize != pid_rsize) {
PIINTROSPECTION_CONTAINER_ALLOC(T, (as - pid_rsize))
T * p_d = reinterpret_cast<T *>(realloc(reinterpret_cast<void *>(pid_data), as * sizeof(T)));
T * new_data = reinterpret_cast<T *>(realloc(reinterpret_cast<void *>(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<T *>(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<T>::calcNewSize(pid_rsize, pid_rsize - start_offset) : pid_rsize;
if (new_rsize > pid_rsize) {
T * tmp_data = reinterpret_cast<T *>(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<void *>(td + ns), reinterpret_cast<const void *>(pid_data + pid_start), pid_size * sizeof(T));
if (pid_rsize > 0 && pid_data) {
memcpy(reinterpret_cast<void *>(tmp_data + new_start),
reinterpret_cast<const void *>(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;

View File

@@ -2459,26 +2459,6 @@ private:
piv_data = nullptr;
}
inline size_t asize(size_t s) {
if (s == 0) return 0;
if (s < _PIContainerConstants<T>::maxCountForPoT()) {
if (piv_rsize * 2 >= s && piv_rsize < s) {
return piv_rsize * 2;
}
ssize_t t = _PIContainerConstants<T>::minCountPoT();
s -= 1;
while (s >> t)
++t;
return (1 << t);
} else {
size_t ret = piv_rsize;
while (ret < s)
ret += _PIContainerConstants<T>::stepAfterPoT();
return ret;
}
return 0;
}
template<typename T1 = T, typename std::enable_if<!std::is_trivially_copyable<T1>::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<T>::calcNewSize(piv_rsize, new_size);
if (new_rsize == piv_rsize) return;
PIINTROSPECTION_CONTAINER_ALLOC(T, (as - piv_rsize))
T * p_d = reinterpret_cast<T *>(realloc(reinterpret_cast<void *>(piv_data), as * sizeof(T)));
T * new_data = reinterpret_cast<T *>(realloc(reinterpret_cast<void *>(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;