Just the question stated, how can I use mmap() to allocate a memory in heap? This is my only option because malloc() is not a reentrant function.
Just the question stated, how can I use mmap() to allocate a memory in
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Why do you need reentrancy? The only time it’s needed is for calling a function from a signal handler; otherwise, thread-safety is just as good. Both
mallocandmmapare thread-safe. Neither is async-signal-safe per POSIX. In practice,mmapprobably works fine from a signal handler, but the whole idea of allocating memory from a signal handler is a very bad idea.If you want to use
mmapto allocate anonymous memory, you can useMAP_ANON/MAP_ANONYMOUS.At one point this was not strictly portable, and systems differed in which spelling they supported so you should write preprocessor conditionals to use whichever is available, but POSIX has since adopted both spellings as standard.
The traditional but ugly version, from long ago before
MAP_ANONwas a thing, is:Note that
MAP_FAILED, notNULL, is the code for failure.