-
Notifications
You must be signed in to change notification settings - Fork 857
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How can mimalloc manage a piece of allocated memory? #922
Comments
Yes, mimalloc has bool mi_manage_os_memory(void* start, size_t size, bool is_committed, bool is_large, bool is_zero, int numa_node); This will add the memory area to the mimalloc arena's from which it will allocate its memory (use Hope this helps, ps. There is also an extended api, |
Thanks very much for your help! It really does me a favor. mimalloc is very useful, and my project cannot be done without it. |
@daanx I am a bit confused about how the mimalloc uses the process memory initially. As far as I know, when the process starts, it already reserves some memory area for the heap, and libc uses sbrk to extend. But I can track mimalloc down, I understand that it does mmap do manage the memory, right? Also, what is the alignment-requirement for the mimalloc? |
Ah, mimalloc generally does not use |
thank you for the clarification! |
Hi @daanx Thank you for having responded in the issue and similar issues. I have a similar problem to the OP, and I can't get mimalloc to allocate from the mmapped file, even with Here's the code (I've removed sanity and error message checks for brevity) #define FILE_SIZE (16 * 1024 * 1024) // 16MB
fd = open("mmapped_file.txt", O_RDWR | O_CREAT, 0666);
char *file_in_memory = mmap(NULL, FILE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_32BIT, fd, 0);
int reserveRet = mi_reserve_os_memory(FILE_SIZE, false, true);
int manageRet = mi_manage_os_memory(file_in_memory, FILE_SIZE, true, false, false, -1);
void *p = mi_malloc(1 * 1024 * 1024); // 1MB
void *q = mi_malloc(1 * 1024 * 1024); // 1MB
void *r = mi_malloc(1 * 1024 * 1024); // 1MB
void *s = mi_malloc(1 * 1024 * 1024); // 1MB
printf("base: %p\n", file_in_memory);
printf("p : %p\n", p);
printf("q : %p\n", q);
printf("r : %p\n", r);
printf("s : %p\n", s); Outputs:
How do I make sure I'll try your solution in #730 next, but I'm not sure where to find Edit: The #730 solution didn't work either, sadly. Iused |
I have already allocated a piece of big memory by
mmap
, the memory size (asMEM_SIZE
) is large, so I want to use it as a memory pool, or a heap, so that I can use the memory efficiently. For example, every time I want a piece of memory (large but not larger thanMEM_SIZE
), I can just easily use a function to allocate and get avoid *
, and release this pointer later, and the pool/heap can collect this memory. An example:And if there exists any memory allocator or memory pool library for C++ that can work as above, it is appreciated if you could tell me and leave some examples or the documents of it.
The text was updated successfully, but these errors were encountered: