Simd Library Documentation.

Home | Release Notes | Download | Documentation | Issues | GitHub
Memory Utilities

Functions for memory management. More...

Functions

SIMD_API void * SimdAllocate (size_t size, size_t align)
 Allocates an aligned memory block. More...
 
SIMD_API void SimdFree (void *ptr)
 Frees an aligned memory block previously allocated by SimdAllocate. More...
 
SIMD_API size_t SimdAlign (size_t size, size_t align)
 Rounds a size value up to the nearest multiple of a given alignment. More...
 
SIMD_API size_t SimdAlignment (void)
 Returns the optimal memory alignment for the current platform. More...
 
SIMD_API void SimdRelease (void *context)
 Destroys an opaque context object created by the Simd Library API. More...
 
SIMD_INLINE void LitterCpuCache (size_t k=2)
 It creates a large buffer and fills it. More...
 

Detailed Description

Functions for memory management.

Function Documentation

◆ SimdAllocate()

void * SimdAllocate ( size_t  size,
size_t  align 
)

Allocates an aligned memory block.

Allocates a contiguous memory block of at least size bytes whose start address is a multiple of align. The alignment value must be a power of two and, on POSIX platforms (GCC), is rounded up to at least sizeof(void*) internally. The actual allocation is performed via the platform-appropriate aligned allocator: _aligned_malloc (MSVC), __mingw_aligned_malloc (MinGW), posix_memalign (GCC), or plain malloc on platforms that do not support aligned allocation.

The block must be released with SimdFree — passing it to the standard free or delete is undefined behaviour.

Using example:

#include "Simd/SimdLib.h"

int main()
{
    const size_t size  = 1024;
    const size_t align = SimdAlignment();
    uint8_t * data = (uint8_t *)SimdAllocate(size, align);
    if (data)
    {
        // use data ...
        SimdFree(data);
    }
    return 0;
}
Parameters
[in]size- the number of bytes to allocate. Must be greater than zero.
[in]align- the required alignment of the allocated block in bytes. Must be a power of two. Use SimdAlignment to obtain the optimal alignment for the current platform.
Returns
a pointer to the newly allocated aligned memory block, or NULL if the allocation fails.

◆ SimdFree()

void SimdFree ( void *  ptr)

Frees an aligned memory block previously allocated by SimdAllocate.

Releases the memory block pointed to by ptr, which must have been returned by a prior call to SimdAllocate. Passing a pointer obtained from any other allocator (e.g. malloc, new, or _aligned_malloc) is undefined behaviour.

Passing NULL is safe and has no effect, consistent with the behaviour of the standard free function.

The underlying release call matches the allocator used by SimdAllocate for the current platform: _aligned_free (MSVC), __mingw_aligned_free (MinGW), or free (GCC and others).

Parameters
[in]ptr- a pointer to the memory block to free. Must have been returned by SimdAllocate, or NULL (in which case the call has no effect).

◆ SimdAlign()

size_t SimdAlign ( size_t  size,
size_t  align 
)

Rounds a size value up to the nearest multiple of a given alignment.

Returns the smallest value that is both a multiple of align and greater than or equal to size. If size is already a multiple of align, it is returned unchanged.

The function uses the bitwise formula (size + align - 1) & ~(align - 1), which requires align to be a positive power of two.

Parameters
[in]size- the original size in bytes (or elements) to be aligned.
[in]align- the required alignment in bytes. Must be a positive power of two. Use SimdAlignment to obtain the optimal alignment for the current platform.
Returns
the smallest multiple of align that is greater than or equal to size.

◆ SimdAlignment()

size_t SimdAlignment ( void  )

Returns the optimal memory alignment for the current platform.

Returns the byte-width of the widest SIMD register available at runtime, which is the recommended alignment value to pass to SimdAllocate and SimdAlign in order to achieve best performance.

The value is determined once at library initialization time by probing the active SIMD extensions and is constant for the lifetime of the process:

  • 128 bytes — HVX (Qualcomm Hexagon)
  • 64 bytes — AVX-512 (x86, when either AVX-512BW or AVX-512VNNI is available)
  • 32 bytes — AVX2 (x86)
  • 16 bytes — SSE4.1 (x86) or NEON (ARM)
  • sizeof(void*) — scalar fallback (no SIMD extensions detected)
  • SVE vector size for current CPU in bytes — when SVE is available.

The returned value is always a power of two and equals the value of the SIMD_ALIGN compile-time constant used internally by the library.

Returns
the optimal alignment in bytes for the current platform.

◆ SimdRelease()

void SimdRelease ( void *  context)

Destroys an opaque context object created by the Simd Library API.

Releases any context object returned by a Simd Library context-creation function, i.e. any function whose name ends in Init (such as SimdGaussianBlurInit, SimdResizerInit, SimdWarpAffineInit, SimdDescrIntInit, SimdFontInit, SimdSynetConvolution32fInit, and others), as well as SimdDetectionLoadA.

Internally the function performs a polymorphic delete through the virtual destructor of the internal Deletable base class, ensuring that the correct destructor is always invoked regardless of the actual context type.

Passing NULL is safe and has no effect, consistent with the behaviour of a C++ delete expression on a null pointer.

Note
Passing a pointer that was not returned by a Simd Library context-creation function (for example a pointer from SimdAllocate, malloc, or new) is undefined behaviour.
Parameters
[in]context- a pointer to the context to be released, or NULL.

◆ LitterCpuCache()

void LitterCpuCache ( size_t  k = 2)

It creates a large buffer and fills it.

This function litters CPU cache. It is useful for test purposes.

Parameters
[in]k- a boosting coefficient of stub buffer size relative to CPU L3 cache size. Its default value is 2.