/*
    PIP - Platform Independent Primitives
    Base macros for generic containers
    Ivan Pelipenko peri4ko@yandex.ru

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "picontainers.h"

#include "piliterals_bytes.h"


#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) {
	size_t ret = 0;
	size_t elc = 1;
	while (elc * szof < minAlloc) {
		elc *= 2;
		++ret;
	}
	// printf("calcMinCount sizeof = %d, min_count = %d, pot = %d\n", szof, elc, ret);
	return ret;
}


size_t _PIContainerConstantsBase::calcMaxCountForPoT(size_t szof) {
	// printf("calcMaxCountForPoT sizeof = %d, size = %d\n", szof, maxPoTAlloc / szof);
	return maxPoTAlloc / szof;
}


size_t _PIContainerConstantsBase::calcStepAfterPoT(size_t szof) {
	// printf("calcStepAfterPoT sizeof = %d, size = %d\n", szof, calcMaxCountForPoT(szof));
	return calcMaxCountForPoT(szof);
}
