Reallocs if there is less slack in the buffer, else performs malloc‐copy‐free.

Synopsis

Declared in <folly/memory/Malloc.h>

void*
smartRealloc(
    void* p,
    size_t const currentSize,
    size_t const currentCapacity,
    size_t const newCapacity);

Description

This function tries to reallocate a buffer of which only the first currentSize bytes are used. The problem with using realloc is that if currentSize is relatively small and if realloc decides it needs to move the memory chunk to a new buffer, then realloc ends up copying data that is not used. It's generally not a win to try to hook in to realloc() behavior to avoid copies ‐ at least in jemalloc, realloc() almost always ends up doing a copy, because there is little fragmentation / slack space to take advantage of.

Return Value

pointer to realloc'ed buffer

Parameters

Name

Description

p

Pointer to start of buffer

currentSize

Current used size

currentCapacity

Capacity of buffer

newCapacity

New capacity for the buffer

Created with MrDocs