Line data Source code
1 : #include "Util.h" 2 : 3 : #include <new> 4 : #include "BitUtil.h" 5 : 6 : size_t elsa::mr::util::computeRealSize(size_t requestedSize, size_t granularity) 7 383456 : { 8 : // never return null! allways allocate some space 9 383456 : if (requestedSize == 0) { 10 8 : ++requestedSize; 11 8 : } 12 : 13 383456 : size_t realSize = (requestedSize + granularity - 1) & ~(granularity - 1); 14 383456 : if (unlikely(realSize < requestedSize)) { 15 0 : throw std::bad_alloc(); 16 0 : } 17 383456 : return realSize; 18 383456 : } 19 : 20 : std::pair<size_t, size_t> elsa::mr::util::computeSizeWithAlignment(size_t requestedSize, 21 : size_t requestedAlignment, 22 : size_t granularity) 23 383447 : { 24 383447 : if (!detail::isPowerOfTwo(requestedAlignment)) { 25 12 : throw std::bad_alloc(); 26 12 : } 27 : 28 : // find best-fitting non-empty bin 29 383435 : size_t realSize = computeRealSize(requestedSize, granularity); 30 : 31 : // this overflow check is probably unnecessary, since the log is already compared 32 : // against the max block size 33 : 34 : // minimal size of the free block to carve the allocation out of. must be enough for to 35 : // contain an aligned allocation 36 383435 : size_t blockSize; 37 383435 : if (requestedAlignment <= granularity) { 38 383411 : blockSize = realSize; 39 383411 : } else { 40 24 : blockSize = realSize + requestedAlignment; 41 24 : if (unlikely(blockSize < realSize)) { 42 0 : throw std::bad_alloc(); 43 0 : } 44 383435 : } 45 383435 : return {realSize, blockSize}; 46 383435 : }