malloc implementation from fixed size buffer

2020-05-19 08:02发布

问题:

I need a generic malloc implementation that uses one big fixed-size buffer. Something similar to the "Zero-malloc memory allocator" SQLite has. Do you know of any such implementations? It should be light-weight and portable that can be used for embedded applications.

Thanks in advance.

回答1:

Two suggestions:

  1. IF you need something production quality and well tested, just borrow SQLite's allocator. SQLite's source code is very well-written, documented, extremely well-tested and has a very permissive open-source license.
  2. IF you need something small and simple, either to learn or to use in an embedded environment, consider this implementation [shameless plug!] - just 350 LOC of commented C code.


回答2:

The SQLite source code is freely available. If you like that a particular implementation, why not use it?



回答3:

Most current malloc implementations work by carving up a large chunk of memory they obtained from the OS. If that block runs out, malloc asks the OS for a new large block.

You could base your own implementation on an existing malloc implementation (for example the glibc one), and instead of obtaining a block from the OS, you use a single static buffer. When that runs out, malloc will start failing, just as it does when the OS can't provide any new blocks.



标签: c malloc