<commit_before>/**
 * @file   AlignedMemory.cpp
 * @brief  Method implementations that support memory alignment.
 * @author zer0
 * @date   2018-01-08
 * @date   2019-03-03 (Change namespace: libtbag::memory -> libtbag::memory::details)
 */

#include <libtbag/memory/details/AlignedMemory.hpp>
#include <libtbag/log/Log.hpp>
#include <libtbag/math/Number.hpp>
#include <libtbag/config-ex.h>

#include <cstdlib>

#if defined(TBAG_COMP_MSVC)
#include <malloc.h>
#endif

/** Throw exception if not aligned memory. */
#define _THROW_MEMORY_ERROR

// -------------------
NAMESPACE_LIBTBAG_OPEN
// -------------------

namespace memory  {
namespace details {

TBAG_CONSTEXPR static bool isThrowMemoryError() TBAG_NOEXCEPT
{
#if defined(_THROW_MEMORY_ERROR)
    return true;
#else
    return false;
#endif
}

TBAG_CONSTEXPR static bool isPowerAltivec() TBAG_NOEXCEPT
{
#if defined(SIMDPP_RUNNABLE_ARCH_POWER_ALTIVEC)
    return true;
#else
    return false;
#endif
}

TBAG_CONSTEXPR static std::size_t _default_aligned_size() TBAG_NOEXCEPT
{
    // [WARNING] On certain architectures, e.g. armv7 NEON,
    // 128 bit vectors are not necessarily aligned to 16 bytes on the stack.
    if (isPowerAltivec()) {
        return TBAG_ALIGNMENT_SIZE_16BYTE;
    } else {
        return TBAG_ALIGNMENT_SIZE_64BYTE;
    }
}

std::size_t getDefaultAlignedSize() TBAG_NOEXCEPT
{
    return _default_aligned_size();
}

void * alignedMemoryAlloc(std::size_t size, std::size_t align)
{
    if (math::isMultiplesOfTwo(align) && align < sizeof(void*)) {
        align = sizeof(void*);
    }

    void * ptr = nullptr;
#if defined(TBAG_COMP_MSVC)
    ptr = _aligned_malloc(size, align);
#else
    int const CODE = ::posix_memalign(&ptr, align, size);
    if (CODE != 0) {<commit_msg>Fixed build error in MSVC.<commit_after>/**
 * @file   AlignedMemory.cpp
 * @brief  Method implementations that support memory alignment.
 * @author zer0
 * @date   2018-01-08
 * @date   2019-03-03 (Change namespace: libtbag::memory -> libtbag::memory::details)
 */

#include <libtbag/memory/details/AlignedMemory.hpp>
#include <libtbag/log/Log.hpp>
#include <libtbag/math/Number.hpp>
#include <libtbag/config-ex.h>

#include <cstdlib>

#if defined(TBAG_COMP_MSVC)
#include <malloc.h>
#endif

/** Throw exception if not aligned memory. */
#define _THROW_MEMORY_ERROR

// -------------------
NAMESPACE_LIBTBAG_OPEN
// -------------------

namespace memory  {
namespace details {

TBAG_CONSTEXPR static bool isThrowMemoryError() TBAG_NOEXCEPT
{
#if defined(_THROW_MEMORY_ERROR)
    return true;
#else
    return false;
#endif
}

TBAG_CONSTEXPR static bool isPowerAltivec() TBAG_NOEXCEPT
{
#if defined(SIMDPP_RUNNABLE_ARCH_POWER_ALTIVEC)
    return true;
#else
    return false;
#endif
}

std::size_t getDefaultAlignedSize() TBAG_NOEXCEPT
{
#if defined(SIMDPP_RUNNABLE_ARCH_POWER_ALTIVEC)
    // [WARNING] On certain architectures, e.g. armv7 NEON,
    // 128 bit vectors are not necessarily aligned to 16 bytes on the stack.
    return TBAG_ALIGNMENT_SIZE_16BYTE;
#else
    return TBAG_ALIGNMENT_SIZE_64BYTE;
#endif
}

void * alignedMemoryAlloc(std::size_t size, std::size_t align)
{
    if (math::isMultiplesOfTwo(align) && align < sizeof(void*)) {
        align = sizeof(void*);
    }

    void * ptr = nullptr;
#if defined(TBAG_COMP_MSVC)
    ptr = _aligned_malloc(size, align);
#else
    int const CODE = ::posix_memalign(&ptr, align, size);
    if (CODE != 0) {
{'fraction_non_alphanumeric': 0.13354763296317942, 'fraction_numerical': 0.01636469900642899, 'mean_word_length': 3.8279266572637516, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6706555', 'n_tokens_mistral': 1262, 'n_tokens_neox': 1192, 'n_words': 278}