Skip to content
  • Ondřej Surý's avatar
    Return non-NULL pointer on zero-sized allocations and reallocations · 4cdb3abf
    Ondřej Surý authored
    Previously, the zero-sized allocations would return NULL pointer and the
    caller had to make sure to not dereference such pointer.  The C standard
    defines the zero-sized calls to malloc() as implementation specific and
    jemalloc mallocx() with zero size would be undefined behaviour.  This
    complicated the code as it had to handle such cases in a special manner
    in all allocator and deallocator functions.
    
    Now, for realloc(), the situation is even more complicated.  In C
    standard up to C11, the behavior would be implementation defined, and
    actually some implementation would free to orig ptr and some would not.
    Since C17 (via DR400) would deprecate such usage and since C23, the
    behaviour would be undefined.
    
    This commits changes helper mem_get(), mem_put() and mem_realloc()
    functions to grow the zero-allocation from 0 to sizeof(void *).
    
    This way we get a predicable behaviour that all the allocations will
    always return valid pointer.
    4cdb3abf