[flang-commits] [flang] [flang] Fix excess allocation (PR #96663)
Peter Klausler via flang-commits
flang-commits at lists.llvm.org
Tue Jun 25 09:29:06 PDT 2024
https://github.com/klausler created https://github.com/llvm/llvm-project/pull/96663
A recent patch introduced an error in an aligned byte size calculation that causes an extra word to be allocated when the original byte size is already aligned (including the case of zero). Fix.
>From 7b6158384937ab1818877d72097b58e4137aa5b7 Mon Sep 17 00:00:00 2001
From: Peter Klausler <pklausler at nvidia.com>
Date: Tue, 25 Jun 2024 09:26:42 -0700
Subject: [PATCH] [flang] Fix excess allocation
A recent patch introduced an error in an aligned byte size calculation
that causes an extra word to be allocated when the original byte size
is already aligned (including the case of zero). Fix.
---
flang/runtime/pointer.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/flang/runtime/pointer.cpp b/flang/runtime/pointer.cpp
index aeed879f1a2e2..2979181ddd61b 100644
--- a/flang/runtime/pointer.cpp
+++ b/flang/runtime/pointer.cpp
@@ -127,7 +127,7 @@ void RTDEF(PointerAssociateRemapping)(Descriptor &pointer,
RT_API_ATTRS void *AllocateValidatedPointerPayload(std::size_t byteSize) {
// Add space for a footer to validate during deallocation.
constexpr std::size_t align{sizeof(std::uintptr_t)};
- byteSize = ((byteSize / align) + 1) * align;
+ byteSize = ((byteSize + align - 1) / align) * align;
std::size_t total{byteSize + sizeof(std::uintptr_t)};
void *p{std::malloc(total)};
if (p) {
@@ -197,7 +197,7 @@ static RT_API_ATTRS std::size_t GetByteSize(
bool RT_API_ATTRS ValidatePointerPayload(const ISO::CFI_cdesc_t &desc) {
std::size_t byteSize{GetByteSize(desc)};
constexpr std::size_t align{sizeof(std::uintptr_t)};
- byteSize = ((byteSize / align) + 1) * align;
+ byteSize = ((byteSize + align - 1) / align) * align;
const void *p{desc.base_addr};
const std::uintptr_t *footer{reinterpret_cast<const std::uintptr_t *>(
static_cast<const char *>(p) + byteSize)};
More information about the flang-commits
mailing list