Example #1
0
extern "C" void __cdecl _aligned_free(void* const block)
{
    #ifdef _DEBUG
    _aligned_free_dbg(block);
    #else
    _aligned_free_base(block);
    #endif
}
FREE_CALL void __cdecl _aligned_free( void *memblock )
{
    _aligned_free_base(memblock);
}
Example #3
0
extern "C" void* __cdecl _aligned_offset_realloc_base(
    void*  block,
    size_t size,
    size_t align,
    size_t offset
    )
{
    uintptr_t ptr, retptr, gap, stptr, diff;
    uintptr_t movsz, reqsz;
    int bFree = 0;

    /* special cases */
    if (block == nullptr)
    {
        return _aligned_offset_malloc_base(size, align, offset);
    }
    if (size == 0)
    {
        _aligned_free_base(block);
        return nullptr;
    }

    /* validation section */
    _VALIDATE_RETURN(IS_2_POW_N(align), EINVAL, nullptr);
    _VALIDATE_RETURN(offset == 0 || offset < size, EINVAL, nullptr);

    stptr = (uintptr_t)block;

    /* ptr points to the pointer to starting of the memory block */
    stptr = (stptr & ~(PTR_SZ -1)) - PTR_SZ;

    /* ptr is the pointer to the start of memory block*/
    stptr = *((uintptr_t *)stptr);

    align = (align > PTR_SZ ? align : PTR_SZ) -1;
    /* gap = number of bytes needed to round up offset to align with PTR_SZ*/
    gap = (0 -offset)&(PTR_SZ -1);

    diff = (uintptr_t)block - stptr;
    /* Mov size is min of the size of data available and sizw requested.
     */
    #pragma warning(push)
    #pragma warning(disable: 22018) // Silence prefast about overflow/underflow
    movsz = _msize((void *)stptr) - ((uintptr_t)block - stptr);
    #pragma warning(pop)

    movsz = movsz > size ? size : movsz;
    reqsz = PTR_SZ + gap + align + size;

    _VALIDATE_RETURN_NOEXC(size <= reqsz, ENOMEM, nullptr);

    /* First check if we can expand(reducing or expanding using expand) data
     * safely, ie no data is lost. eg, reducing alignment and keeping size
     * same might result in loss of data at the tail of data block while
     * expanding.
     *
     * If no, use malloc to allocate the new data and move data.
     *
     * If yes, expand and then check if we need to move the data.
     */
    if ((stptr +align +PTR_SZ +gap)<(uintptr_t)block)
    {
        if ((ptr = (uintptr_t)malloc(reqsz)) == (uintptr_t) nullptr)
            return nullptr;
        bFree = 1;
    }
    else
    {
        /* we need to save errno, which can be modified by _expand */
        errno_t save_errno = errno;
        if ((ptr = (uintptr_t)_expand((void *)stptr, reqsz)) == (uintptr_t)nullptr)
        {
            errno = save_errno;
            if ((ptr = (uintptr_t)malloc(reqsz)) == (uintptr_t) nullptr)
                return nullptr;
            bFree = 1;
        }
        else
            stptr = ptr;
    }


    if ( ptr == ((uintptr_t)block - diff)
         && !( ((size_t)block + gap +offset) & ~(align) ))
    {
        return block;
    }

    retptr =((ptr +PTR_SZ +gap +align +offset)&~align)- offset;
    memmove((void *)retptr, (void *)(stptr + diff), movsz);
    if ( bFree)
        free ((void *)stptr);

    ((uintptr_t *)(retptr - gap))[-1] = ptr;
    return (void *)retptr;
}