Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use mmap to allocate a memory in heap?

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.

like image 336
domlao Avatar asked Jan 24 '11 06:01

domlao


People also ask

Does mmap allocate memory on heap?

mmapped memory is neither heap nor stack. It is mapped into virtual address space of the calling process, but it's not allocated on the heap.

How do I allocate memory with heap?

In C, dynamic memory is allocated from the heap using some standard library functions. The two key dynamic memory functions are malloc() and free(). The malloc() function takes a single parameter, which is the size of the requested memory area in bytes. It returns a pointer to the allocated memory.

How does malloc use mmap?

For very large requests, malloc() uses the mmap() system call to find addressable memory space. This process helps reduce the negative effects of memory fragmentation when large blocks of memory are freed but locked by smaller, more recently allocated blocks lying between them and the end of the allocated space.

Is malloc or mmap faster?

Almost always, memory is much faster than disk, and malloc is not what's costing time. The mmap code is faster because for your program, mmap has resulted in either less disk access, or more efficient disk access, than whatever reads and writes you compared against.


2 Answers

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 malloc and mmap are thread-safe. Neither is async-signal-safe per POSIX. In practice, mmap probably 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 mmap to allocate anonymous memory, you can use (not 100% portable but definitely best):

p = mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);

The portable but ugly version is:

int fd = open("/dev/zero", O_RDWR);
p = mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
close(fd);

Note that MAP_FAILED, not NULL, is the code for failure.

like image 98
R.. GitHub STOP HELPING ICE Avatar answered Nov 11 '22 01:11

R.. GitHub STOP HELPING ICE


Make a simple slab allocator


Although allocating memory in a signal handler1 does seem like something best avoided, it certainly can be done.

No, you can't directly use malloc(). If you want it to be in the heap then mmap won't work either.

My suggestion is that you make a special-purpose slab allocator based on malloc.

Decide exactly what size of object you want and preallocate some number of them. Allocate them initially with malloc() and save them for concurrent use later. There are intrinsically reentrant queue-and-un-queue functions that you can use to obtain and release these blocks. If they only need to be managed from the signal handler then even that isn't necessary.

Problem solved!


1. And if you are not doing that then it seems like you have an embedded system or could just use malloc().

like image 21
DigitalRoss Avatar answered Nov 11 '22 03:11

DigitalRoss