Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions include/utils.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

#pragma once

#include <errno.h>
#include <stdlib.h> // for posix_memalign

#include "common_includes.h"

Expand Down Expand Up @@ -259,7 +260,15 @@ inline void alloc_aligned(void **ptr, size_t size, size_t align)
if (IS_ALIGNED(size, align) == 0)
report_misalignment_of_requested_size(align);
#ifndef _WINDOWS
*ptr = ::aligned_alloc(align, size);
// Use posix_memalign instead of aligned_alloc for tcmalloc compatibility.
// posix_memalign is more compatible with tcmalloc and avoids "invalid pointer" errors
// when freeing memory allocated with aligned_alloc and freed with tcmalloc's free().
int ret = posix_memalign(ptr, align, size);
if (ret != 0)
{
*ptr = nullptr;
Comment on lines +263 to +269
Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

posix_memalign requires align to be a power-of-two and a multiple of sizeof(void*) (and typically >= sizeof(void*)). There are existing call sites that pass align = 1 (e.g., PQDataStore allocates _quantized_data with align=1), which will now fail with EINVAL and terminate. Consider special-casing small/invalid alignments (e.g., treat align < sizeof(void*) as sizeof(void*), or fall back to malloc when no special alignment is needed), and/or explicitly validate align and surface a clearer error than "Memory Allocation Failed" for EINVAL.

Suggested change
// Use posix_memalign instead of aligned_alloc for tcmalloc compatibility.
// posix_memalign is more compatible with tcmalloc and avoids "invalid pointer" errors
// when freeing memory allocated with aligned_alloc and freed with tcmalloc's free().
int ret = posix_memalign(ptr, align, size);
if (ret != 0)
{
*ptr = nullptr;
// Normalize and validate alignment for posix_memalign.
// posix_memalign requires that the alignment be a power of two and
// a multiple of sizeof(void*), and typically at least sizeof(void*).
size_t effective_align = align;
if (effective_align < sizeof(void *))
effective_align = sizeof(void *);
bool align_is_valid = (effective_align != 0) &&
((effective_align & (effective_align - 1)) == 0) &&
(effective_align % sizeof(void *) == 0);
if (!align_is_valid)
{
std::stringstream stream;
stream << "Invalid alignment (" << align
<< ") requested for aligned allocation. Alignment must be a power-of-two "
<< "multiple of " << sizeof(void *) << ".";
print_error_and_terminate(stream);
}
// Use posix_memalign instead of aligned_alloc for tcmalloc compatibility.
// posix_memalign is more compatible with tcmalloc and avoids "invalid pointer" errors
// when freeing memory allocated with aligned_alloc and freed with tcmalloc's free().
int ret = posix_memalign(ptr, effective_align, size);
if (ret != 0)
{
*ptr = nullptr;
if (ret == EINVAL)
{
std::stringstream stream;
stream << "posix_memalign failed with EINVAL for alignment " << align
<< " (effective alignment " << effective_align
<< "). Alignment must be a power-of-two multiple of " << sizeof(void *) << ".";
print_error_and_terminate(stream);
}

Copilot uses AI. Check for mistakes.
report_memory_allocation_failure();
}
#else
*ptr = ::_aligned_malloc(size, align); // note the swapped arguments!
#endif
Expand Down