0

I want to initialize a memory region and thus can use malloc (in glibc) to allocate memory only from this region. There will be multiple malloc used and every malloc operation should only use this pre-defined region. This region requires contiguous memory addresses with a specific size.

My thought is to create a region_init function that uses sbrk or shared memory object (using shm_open?) to initialize a memory region that can be used by (multiple) malloc. BTW, I think mmap() doesn't work for this scenario because mmap() doesn't always return contiguous addresses. The problem is that I don't know how to make every malloc use this memory region I created (using sbrk? shared memory? and/or others?). Can I achieve this by modifying the source codes of malloc in glibc? If I can, how to modify glibc (any idea is appreciated)? What should I do for implementing this functionality?

Before this question, I asked a related question on StackOverflow. In the previous question, some answer/comment suggested that I can use a single malloc to allocate a large memory buffer/region and implement my own API to manage this memory (e.g., m_malloc to allocate part of the memory from this memory buffer/region). However, the problem of this design is that it is hard to free especially when there are multiple variables in this region. The free in glibc can only free the whole region that is malloc'd. If I want to free a variable (when there are many variables in this malloc'd region), I can only free the whole region which means all of the variables are freed. I really want to use free to free the memory of any variable I allocated in the memory region created by only one malloc.

I hope my question is clear. Thanks for your help and ideas.

Jeffrey
  • 59
  • 1
  • 5
  • 1
    If you allocate memory out of a buffer you manage yourself, you wouldn't call `free` to release the memory. You'd call e.g. `m_free` which would free the memory in your internal buffer so it can potentially be returned by a later call to `m_malloc`. You need to keep track of what regions of memory are available and which ones are not. – dbush Jul 21 '20 at 03:55
  • Thanks. If so, I must manage the memory using a structure like doubly-linked list? I should manage the free blocks with some method like Best fit and First fit by myself, but how should free the variable in the intermediate space of memory region? using a bit 0/1 to indicate this block is free or not. Is there any efficient way to manage large memory region? Thanks again! – Jeffrey Jul 21 '20 at 04:06
  • You can always look at how glic does it and implement something similar. – dbush Jul 21 '20 at 04:07
  • Thank you. Do I need to modify the source code of `malloc` and `free` in glibc? or I just need to implement a wrapper that directly uses unmodified `malloc` and `free`? – Jeffrey Jul 21 '20 at 05:58
  • You don't want to modify glibc as that will affect all processes on the system. Instead just use that code as a basis for your own. Use the built-in `malloc` to get a large block of memory, then you write `m_malloc` to allocate memory out of that pool and `m_free` to give memory back to that pool. How exactly you do that depends entirely on your specific requirements. – dbush Jul 21 '20 at 11:38

0 Answers0