0

I want to use malloc to allocate memory, but I don't want to use a dynamic allocation way in malloc. Instead, I want to use malloc to allocate a large block/pool so that I can "new" some variables/objects into this large memory block/pool. Therefore, I would like to modify the malloc source code (e.g., malloc/malloc.c) in glibc to make it work for my scenario.

What is the most convenient and efficient way to modify glibc source codes and also test its functionality? One way that comes to my mind is to download the glibc source, modify the code, and then configure/make/make install, but it would be very cumbersome because I have to test my modified version of malloc frequently to make sure it works properly.

Thank you so much for your kind help! All the answers and thoughts are appreciated.

Jeffrey
  • 59
  • 1
  • 5
  • 4
    What you're proposing sounds like a terrible idea. You can implement pooling without modifying malloc. – Retired Ninja Jul 09 '20 at 21:44
  • Thanks for the comment. Do you mean implement a malloc() from scratch? I can use sbrk() to extend the heap with a specific size and thus allocate memory, but it seems that it's not efficient because it is hard to free() and cannot use many well-defined methods for memory management in glibc. I want to make use of the free and other mechanisms in glibc. That is my thought. Thanks again! – Jeffrey Jul 09 '20 at 21:52
  • 1
    No need to implement `malloc` from scratch, or at all. Instead, just implement your own pooled-allocation function (call it something like `pooled_malloc` perhaps). Use regular, unaltered `malloc` and `free` to manage the pool, and use your new function to get blocks for your objects. – lxop Jul 09 '20 at 22:11
  • i have some idea, allocate very large buffer with `malloc`. get the return pointer. save it. and manage the heap allocated buffer with your own `API` (your own functions, e.g. expose your own `malloc` to the user, e.g. `m_malloc()`, and manage the memory buffer). at the end of your program dont forget to `free` the buffer. in this way you are not changing `malloc` and you limits the user to your own `API`. good luck. – Adam Jul 09 '20 at 22:55
  • `malloc` already does this, doesn't it? – user253751 Jul 09 '20 at 22:59
  • Thank you all for the valuable comments. If use the unaltered `malloc` to allocate large memory and assign memory to many different variables, it would be hard to free the memory for some variables (esp. the variables reside in the intermediate space of memory) by using `free`, because `free` will free the whole memory allocated by `malloc`, so the memory of all the variables will be freed (not one variable). If use `malloc` to implement a memory pool, it seems to have same problem - cannot free the variable (partial memory deallocation) as required in the memory. Any ideas? – Jeffrey Jul 10 '20 at 03:29

1 Answers1

1

You can use LD_PRELOAD with your code (assuming it is dynamically linked) to insert your own malloc implementation.

What you want sounds a lot like Arena Allocation. This library might already do what you need: https://github.com/thejefflarson/arena

dogman288
  • 613
  • 3
  • 9