malloc implementation from fixed size buffer

2020-05-19 07:45发布

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.

标签: c malloc
3条回答
聊天终结者
2楼-- · 2020-05-19 08:27

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

查看更多
smile是对你的礼貌
3楼-- · 2020-05-19 08:29

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.

查看更多
祖国的老花朵
4楼-- · 2020-05-19 08:34

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.
查看更多
登录 后发表回答