0

I want to make a memory pool manager for a embedded system project. I want to make a separate header and corresponding implementation in a .c file (say mempool.c). My doubt here is, when I call malloc() in the function implementations in this file, which heap memory am I actually using?

Given that there are other files (say foo1.c and foo2.c) that use the header "mempool.h" and the corresponding functions in mempool.c. So, when I compile and execute foo1.c and foo2.c simultaneously, will their calls to the functions in mempool invoke allocations on 2 completely separate heaps or the same one?

3
  • Too broad, but embedded systems typically don't have the standard library available and using malloc & friends is a very bad idea. That's the reason one uses memory pools ... Commented Jul 9, 2016 at 12:56
  • Are foo1.c and foo2.c compiled into the same program or two separate programs? Commented Jul 9, 2016 at 14:10
  • they are individually separate programs all together. Commented Jul 9, 2016 at 14:14

1 Answer 1

2

There are three C source-files in play: mempool, foo1, foo2.

The .h file(s) are header files which describe what the functions "look like," and how they are to be called.

When you compile either foo1 or foo2, the object-code from mempool.o will be statically linked into them.

Having done this, they are now "two entirely separate programs," both of which contain an identical bit of object-code.

When the two programs execute, each of them will have their own private heap. Although both of them contain the same object-code that is used for memory-pool management, they are unrelated to one another.

If you do want the programs to actually "share memory," you must use a named shared-memory segment. (shmget, etc ...), and you must design your memory-pool manager code to find, open, and properly use that shared resource. Once again, both programs will contain their own independent copy of the object-code of your memory-pool manager.

(Hint: "this has already been done." You can find complete libraries that do this, on places like GitHub, instead of embarking on a custom-made implementation, yourself.)

=== Additional Note:

I would also like to comment, in passing, that a third possibility exists: dynamically-loaded libraries.

When you take this approach, the object-code is loaded by the application at runtime, and it does not become "part of" the object-code of the applications that use it. Instead, it becomes accessible to them. Furthermore, the operating system is aware of the sharing, and it may recognize a single instance that is being shared. It may allow a common set of memory-allocations to be "owned by" that single, shared instance. Operating-systems (and versions, "historic vs. modern-day" e.g. of MS-Windows ...) vary on this particular point.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. Very lucid answer!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.