malloc: remove #pragma weak as MacOS does not like

Originally the use of a weak symbol was to provide a fallback for
non-inline supporting compilers.  However all compilers (we care about)
support inline keyword (which was not known at the original time of the
work).  Furthermore GCC have already worked through the solution and make
it easy to implement.

The use of __GNUC_STDC_INLINE__ pattern in this way manages the fallback
and emits a hard symbol this can be tested with:

CFLAGS="-g" ./configure; make; nm */lib*.o | grep freeMagic1

CFLAGS="-O3" ./configure; make; nm */lib*.o | grep freeMagic1

A hard 'T' symbol is emitted (to provide fallback) with all builds, but
in the -O3 all usage is inlined.  So an individual file can decide to
inline or not at the occasion (compile time options) allows.
This commit is contained in:
Darryl L. Miles 2025-10-29 23:34:33 +00:00 committed by R. Timothy Edwards
parent ea1a89b19c
commit e9202c1d29
2 changed files with 22 additions and 39 deletions

View File

@ -176,16 +176,20 @@ callocMagicLegacy(nbytes)
#endif /* SUPPORT_REMOVE_MALLOC_LEGACY */
#ifdef __GNUC_STDC_INLINE__
/* Use of 'extern inline' force an emit of inline code at a symbol */
extern inline free_magic1_t freeMagic1_init(void);
extern inline void freeMagic1(free_magic1_t* m1, void* ptr);
extern inline void freeMagic1_end(free_magic1_t* m1);
#else /* __GNUC_STDC_INLINE__ */
/*
* NOTICE: non-inline form of emitted functions, keep in sync with malloc.h
*/
#pragma weak freeMagic1_init = freeMagic1_init_func
free_magic1_t freeMagic1_init_func() {
free_magic1_t freeMagic1_init(void) {
return NULL;
}
#pragma weak freeMagic1 = freeMagic1_func
void freeMagic1_func(free_magic1_t* m1, void* ptr) {
void freeMagic1(free_magic1_t* m1, void* ptr) {
//if(*m1) /* this if() is here to help inliner remove the call to free() when it can */
/* this is not the inline form here so if() is commented out */
{
@ -199,8 +203,7 @@ void freeMagic1_func(free_magic1_t* m1, void* ptr) {
*m1 = ptr;
}
#pragma weak freeMagic1_end = freeMagic1_end_func
void freeMagic1_end_func(free_magic1_t* m1) {
void freeMagic1_end(free_magic1_t* m1) {
//if(*m1) /* this if() is here to help inliner remove the call to free() when it can */
/* this is not the inline form here so if() is commented out */
{
@ -212,3 +215,4 @@ void freeMagic1_end_func(free_magic1_t* m1) {
#endif
}
}
#endif /* __GNUC_STDC_INLINE__ */

View File

@ -37,7 +37,7 @@
#define callocMagic calloc
#define freeMagic free
#else
#else /* SUPPORT_DIRECT_MALLOC */
extern void *mallocMagicLegacy(size_t);
#define mallocMagic(size) mallocMagicLegacy(size)
@ -56,29 +56,15 @@ extern void freeMagicLegacy(void *);
typedef void* free_magic1_t;
/* TODO this should be moved to autoconf/build/toolchain detection, this does exist
* in autoconf and in another changeset, so I come back and fixup/remove this later.
*/
#if (defined(__STDC__) && defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L))
/* C99 or later */
#ifndef __inline__
/* you'd have thought on linux this was enabled already, TODO check bplane module inlines */
#define __inline__ inline
#endif
#endif
#if (!defined(_MAGIC__UTILS__MALLOC_H__NOINLINE) && defined(__inline__))
/* TODO this (__extern_inline__) should be moved to autoconf/build/toolchain detection */
#define __extern_inline__ inline
#ifdef __GNUC_STDC_INLINE__
/* Provide compiler visibility of STDC 'inline' semantics */
/*
* NOTICE: inline form, keep in sync with malloc.c copied
*/
__extern_inline__ free_magic1_t freeMagic1_init() {
inline free_magic1_t freeMagic1_init(void) {
return NULL;
}
__extern_inline__ void freeMagic1(free_magic1_t* m1, void* ptr) {
inline void freeMagic1(free_magic1_t* m1, void* ptr) {
if(*m1) /* this if() is here to help inliner remove the call to free() when it can */
{
#if (defined(SUPPORT_DIRECT_MALLOC) || defined(SUPPORT_REMOVE_MALLOC_LEGACY))
@ -89,7 +75,7 @@ __extern_inline__ void freeMagic1(free_magic1_t* m1, void* ptr) {
}
*m1 = ptr;
}
__extern_inline__ void freeMagic1_end(free_magic1_t* m1) {
inline void freeMagic1_end(free_magic1_t* m1) {
if(*m1) /* this if() is here to help inliner remove the call to free() when it can */
{
#if (defined(SUPPORT_DIRECT_MALLOC) || defined(SUPPORT_REMOVE_MALLOC_LEGACY))
@ -99,18 +85,11 @@ __extern_inline__ void freeMagic1_end(free_magic1_t* m1) {
#endif
}
}
#else
#define freeMagic1_init() freeMagic1_init_func()
#define freeMagic1(m1, ptr) freeMagic1_func((m1), (ptr))
#define freeMagic1_end(m1) freeMagic1_end_func((m1))
#endif /* !_MAGIC__UTILS__MALLOC_H__NOINLINE && __inline__ */
/* we'll emit a function call interface just in case a platform won't inline and we can redirect */
extern free_magic1_t freeMagic1_init_func(void);
extern void freeMagic1_func(free_magic1_t* m1, void* ptr);
extern void freeMagic1_end_func(free_magic1_t* m1);
#else /* __GNUC_STDC_INLINE__ */
/* To support older compilers (that don't auto emit based on -O level) */
extern free_magic1_t freeMagic1_init(void);
extern void freeMagic1(free_magic1_t* m1, void* ptr);
extern void freeMagic1_end(free_magic1_t* m1);
#endif /* __GNUC_STDC_INLINE__ */
#endif /* _MAGIC__UTILS__MALLOC_H */