Skip to content

Commit 0d0da76

Browse files
authored
perf: update free_sized declaration to be compatible with glibc (#8661)
glibc adds `__attribute__((nothrow))` to its declarations, at least for those related to malloc. glibc has yet to introduce `free_sized`, but when it does it would cause compilation errors. This is due to the fact that if a function declarations has `__attribute__((nothrow))` and it is re-declared or implemented in C++ it must also have `__attribute__((nothrow))` or `noexcept`, otherwise the compilation will fail. This is a follow up to #6598. Signed-off-by: Justin King <[email protected]>
1 parent 3feb632 commit 0d0da76

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/include/lean/lean.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,11 @@ void free(void *); // avoid including big `stdlib.h`
416416
#endif
417417

418418
#if !defined(__STDC_VERSION_STDLIB_H__) || __STDC_VERSION_STDLIB_H__ < 202311L
419+
#if defined(__GLIBC__) && (defined(__GNUC__) || defined(__clang__))
420+
// glibc tacks on `__attribute__((nothrow))` to its declarations. In C++ this requires either
421+
// `__attribute__((nothrow))` to be present or `noexcept`.
422+
__attribute__((nothrow))
423+
#endif
419424
void free_sized(void* ptr, size_t);
420425
#endif
421426

src/runtime/object.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,13 @@ Author: Leonardo de Moura
4848
#endif
4949

5050
#if !defined(__STDC_VERSION_STDLIB_H__) || __STDC_VERSION_STDLIB_H__ < 202311L
51-
extern "C" LEAN_EXPORT __attribute__((weak)) void free_sized(void *ptr, size_t) {
51+
extern "C" LEAN_EXPORT
52+
#if defined(__GLIBC__) && (defined(__GNUC__) || defined(__clang__))
53+
// glibc tacks on `__attribute__((nothrow))` to its declarations. In C++ this requires either
54+
// `__attribute__((nothrow))` to be present or `noexcept`.
55+
__attribute__((nothrow))
56+
#endif
57+
__attribute__((weak)) void free_sized(void *ptr, size_t) {
5258
free(ptr);
5359
}
5460
#endif

0 commit comments

Comments
 (0)