void* MemoryManager::mallocSmallSizeSlow(uint32_t bytes, unsigned index) { size_t nbytes = smallIndex2Size(index); static constexpr unsigned nContigTab[] = { #define SMALL_SIZE(index, lg_grp, lg_delta, ndelta, lg_delta_lookup, ncontig) \ ncontig, SMALL_SIZES #undef SMALL_SIZE }; unsigned nContig = nContigTab[index]; size_t contigMin = nContig * nbytes; unsigned contigInd = smallSize2Index(contigMin); for (unsigned i = contigInd; i < kNumSmallSizes; ++i) { FTRACE(4, "MemoryManager::mallocSmallSizeSlow({}-->{}, {}): contigMin={}, " "contigInd={}, try i={}\n", bytes, nbytes, index, contigMin, contigInd, i); void* p = m_freelists[i].maybePop(); if (p != nullptr) { FTRACE(4, "MemoryManager::mallocSmallSizeSlow({}-->{}, {}): " "contigMin={}, contigInd={}, use i={}, size={}, p={}\n", bytes, nbytes, index, contigMin, contigInd, i, smallIndex2Size(i), p); // Split tail into preallocations and store them back into freelists. uint32_t availBytes = smallIndex2Size(i); uint32_t tailBytes = availBytes - nbytes; if (tailBytes > 0) { void* tail = (void*)(uintptr_t(p) + nbytes); splitTail(tail, tailBytes, nContig - 1, nbytes, index); } return p; } } // No available free list items; carve new space from the current slab. return slabAlloc(bytes, index); }
/* * Allocate `bytes' from the current slab, aligned to kSmallSizeAlign. */ inline void* MemoryManager::slabAlloc(uint32_t bytes, unsigned index) { FTRACE(3, "slabAlloc({}, {}): m_front={}, m_limit={}\n", bytes, index, m_front, m_limit); uint32_t nbytes = smallIndex2Size(index); assert(bytes <= nbytes); assert(nbytes <= kSlabSize); assert((nbytes & kSmallSizeAlignMask) == 0); assert((uintptr_t(m_front) & kSmallSizeAlignMask) == 0); if (UNLIKELY(m_bypassSlabAlloc)) { // Stats correction; mallocBigSize() pulls stats from jemalloc. m_stats.usage -= bytes; return mallocBigSize<false>(nbytes).ptr; } void* ptr = m_front; { void* next = (void*)(uintptr_t(ptr) + nbytes); if (uintptr_t(next) <= uintptr_t(m_limit)) { m_front = next; } else { ptr = newSlab(nbytes); } } // Preallocate more of the same in order to amortize entry into this method. unsigned nSplit = kNContigTab[index] - 1; uintptr_t avail = uintptr_t(m_limit) - uintptr_t(m_front); if (UNLIKELY(nSplit * nbytes > avail)) { nSplit = avail / nbytes; // Expensive division. } if (nSplit > 0) { void* tail = m_front; uint32_t tailBytes = nSplit * nbytes; m_front = (void*)(uintptr_t(m_front) + tailBytes); splitTail(tail, tailBytes, nSplit, nbytes, index); } FTRACE(4, "slabAlloc({}, {}) --> ptr={}, m_front={}, m_limit={}\n", bytes, index, ptr, m_front, m_limit); return ptr; }