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;
report_memory_allocation_failure();
}
#else
*ptr = ::_aligned_malloc(size, align); // note the swapped arguments!
#endif
Expand Down