[libc-commits] [libc] [libc] Remove hardcoded sizeof in __barrier_type.h (PR #153718)
Mikhail R. Gadelha via libc-commits
libc-commits at lists.llvm.org
Fri Aug 15 08:09:54 PDT 2025
https://github.com/mikhailramalho updated https://github.com/llvm/llvm-project/pull/153718
>From 08a3fee6380fe00d5333ff5e7d4ffa5455dbacdf Mon Sep 17 00:00:00 2001
From: "Mikhail R. Gadelha" <mikhail at igalia.com>
Date: Thu, 14 Aug 2025 21:30:21 -0300
Subject: [PATCH 1/2] Remove hardcoded sizeof
Signed-off-by: Mikhail R. Gadelha <mikhail at igalia.com>
---
libc/include/llvm-libc-types/__barrier_type.h | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/libc/include/llvm-libc-types/__barrier_type.h b/libc/include/llvm-libc-types/__barrier_type.h
index 59712619e917d..23593372af56e 100644
--- a/libc/include/llvm-libc-types/__barrier_type.h
+++ b/libc/include/llvm-libc-types/__barrier_type.h
@@ -9,13 +9,16 @@
#ifndef LLVM_LIBC_TYPES__BARRIER_TYPE_H
#define LLVM_LIBC_TYPES__BARRIER_TYPE_H
+#include "src/__support/threads/CndVar.h"
+#include "src/__support/threads/mutex.h"
+
typedef struct __attribute__((aligned(8 /* alignof (Barrier) */))) {
unsigned expected;
unsigned waiting;
bool blocking;
- char entering[24 /* sizeof (CndVar) */];
- char exiting[24 /* sizeof (CndVar) */];
- char mutex[24 /* sizeof (Mutex) */];
+ char entering[sizeof(LIBC_NAMESPACE::CndVar)];
+ char exiting[sizeof(LIBC_NAMESPACE::CndVar)];
+ char mutex[sizeof(LIBC_NAMESPACE::Mutex)];
} __barrier_type;
#endif // LLVM_LIBC_TYPES__BARRIER_TYPE_H
>From 0da48fd3873eeb1c22ca55fe47373ab3189fe460 Mon Sep 17 00:00:00 2001
From: "Mikhail R. Gadelha" <mikhail at igalia.com>
Date: Fri, 15 Aug 2025 12:09:34 -0300
Subject: [PATCH 2/2] Changed static_assert
Signed-off-by: Mikhail R. Gadelha <mikhail at igalia.com>
---
libc/include/llvm-libc-types/__barrier_type.h | 9 +++------
libc/src/__support/threads/linux/barrier.h | 15 ++++++++++-----
2 files changed, 13 insertions(+), 11 deletions(-)
diff --git a/libc/include/llvm-libc-types/__barrier_type.h b/libc/include/llvm-libc-types/__barrier_type.h
index 23593372af56e..59712619e917d 100644
--- a/libc/include/llvm-libc-types/__barrier_type.h
+++ b/libc/include/llvm-libc-types/__barrier_type.h
@@ -9,16 +9,13 @@
#ifndef LLVM_LIBC_TYPES__BARRIER_TYPE_H
#define LLVM_LIBC_TYPES__BARRIER_TYPE_H
-#include "src/__support/threads/CndVar.h"
-#include "src/__support/threads/mutex.h"
-
typedef struct __attribute__((aligned(8 /* alignof (Barrier) */))) {
unsigned expected;
unsigned waiting;
bool blocking;
- char entering[sizeof(LIBC_NAMESPACE::CndVar)];
- char exiting[sizeof(LIBC_NAMESPACE::CndVar)];
- char mutex[sizeof(LIBC_NAMESPACE::Mutex)];
+ char entering[24 /* sizeof (CndVar) */];
+ char exiting[24 /* sizeof (CndVar) */];
+ char mutex[24 /* sizeof (Mutex) */];
} __barrier_type;
#endif // LLVM_LIBC_TYPES__BARRIER_TYPE_H
diff --git a/libc/src/__support/threads/linux/barrier.h b/libc/src/__support/threads/linux/barrier.h
index f0655bfc52a10..d6264f7d631e8 100644
--- a/libc/src/__support/threads/linux/barrier.h
+++ b/libc/src/__support/threads/linux/barrier.h
@@ -36,15 +36,20 @@ class Barrier {
int wait();
};
-static_assert(
- sizeof(Barrier) == sizeof(pthread_barrier_t),
- "The public pthread_barrier_t type cannot accommodate the internal "
- "barrier type.");
+static_assert(sizeof(Barrier) <= sizeof(pthread_barrier_t),
+ "The public pthread_barrier_t type cannot accommodate the "
+ "internal barrier type.");
-static_assert(alignof(Barrier) == alignof(pthread_barrier_t),
+static_assert(alignof(Barrier) <= alignof(pthread_barrier_t),
"The public pthread_barrier_t type has a different alignment "
"than the internal barrier type.");
+static_assert(sizeof(CndVar) <= 24,
+ "CndVar size exceeds the size in __barrier_type.h");
+
+static_assert(sizeof(Mutex) <= 24,
+ "Mutex size exceeds the size in __barrier_type.h");
+
} // namespace LIBC_NAMESPACE_DECL
#endif // LLVM_LIBC___SUPPORT_SRC_THREADS_LINUX_BARRIER_H
More information about the libc-commits
mailing list