mimalloc-override.h does not work correctly with namespaces
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
Research direction
Start with mimalloc-override.h and reproduce the failure using the -include mimalloc-override.h and -lmimalloc flags, then inspect the namespace-qualified calls shown from llvm/Support/MemAlloc.h. Verify the override behavior for std::malloc and std::realloc, and add coverage demonstrating that the affected Clang compilation succeeds.
Written by the indexing model from the issue text.
Description
Hello! Recently, I decided to build a project with Clang using mimalloc. I opted for overriding through header files, as I believe this is the most reliable and controllable way to override calls. The flags are quite simple for this:
C*FLAGS="-include mimalloc-override.h"
LDFLAGS="-lmimalloc"
However, Clang failed to compile with errors
/usr/lib/llvm/21/include/llvm/Support/MemAlloc.h:53:18: error: no member named 'mi_realloc' in namespace 'std'; did you mean simply 'mi_realloc'?
53 | void *Result = std::realloc(Ptr, Sz);
| ^ ~~~~~~~~~~~~~~~~
/usr/include/mimalloc.h:111:40: note: 'mi_realloc' declared here
It seems that the method of overriding using #define does not account for calls that may use namespaces (or their aliases). For now, one option I see is to add definitions of mi_realloc into the std namespace, creating something like this:
mimalloc-override.h
#include <mimalloc.h>
#define malloc(n) mi_malloc(n)
#if defined(__cplusplus)
namespace std {
inline auto mi_malloc(size_t n) { return ::mi_malloc(n); }
}
#endif
out.cpp
#include "mimalloc-override.h"
#include <cstdlib>
int main() {
auto _ = std::malloc(8);
}
preprocessed_out.cpp
# 2 "./mimalloc-override.h" 2
namespace std {
inline auto mi_malloc(size_t n) { return ::mi_malloc(n); }
}
# 2 "out.cpp" 2
int main() {
auto _ = std::mi_malloc(8);
}
Thank you for your time and for the great project! :)
- Dominant language
- C
- Stars
- 13.6k
- Forks
- 1.2k
- Avg merge
- 18h 12m
- Merged PRs (30d)
- 20
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from microsoft/mimalloc
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
Building mimalloc with `-Wundef` compiler flag logs many warnings on undefined macrosPossibly taken @vstinner claimed this 1 day ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
microsoft/mimalloc#1506 · 5 comments ·
Maintainers usually reply within 1 day
-
stale
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
microsoft/mimalloc#1113 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
microsoft/mimalloc#886 · 4 comments · 1 reaction ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
microsoft/mimalloc#368 · 1 comment ·
Maintainers usually reply within 1 day
All issues in microsoft/mimalloc
Similar issues
-
needs triage
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
389ds/389-ds-base#8157 ·
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 67/100
coolsnowwolf/lede#14305 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 1 day
-
Needs-Triage
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
Maintainers usually reply within 2 days