Memory pools implementation in C

那年仲夏 提交于 2019-12-18 15:44:26

问题


I am looking for a good memory pool implementation in C.

it should include the following:

  1. Anti fragmentation.
  2. Be super fast :)
  3. Ability to "bundle" several allocations from different sizes under some identifier and delete all the allocations with the given identifier.
  4. Thread safe

回答1:


I think the excellent talloc, developed as part of samba might be what you're looking for. The part I find most interesting is that any pointer returned from talloc is a valid memory context. Their example is:

struct foo *X = talloc(mem_ctx, struct foo);
X->name = talloc_strdup(X, "foo");
// ...
talloc_free(X); // frees memory for both X and X->name

In response to your particular points:

(1) Not sure what anti-fragmentation is in this case. In C you're not going to get compacting garbage collection anyway, so I think your choices are somewhat limited.

(2) It advertises being only 4% slower than plain malloc(3), which is quite fast.

(3) See example above.

(4) It is thread safe as long as different threads use different contexts & the underlying malloc is thread safe.




回答2:


Have you looked into

  • nedmalloc http://www.nedprod.com/programs/portable/nedmalloc/
  • ptmalloc http://www.malloc.de/en/

Both leverage a memory pool but keep it mostly transparent to the user.

In general, you will find best performance in your own custom memory pool (you can optimize for your pattern). I ended up writing a few for different access patterns.




回答3:


For memory pools that have been thoroughly tried and tested you may want to just use the APR ones:

http://apr.apache.org/docs/apr/1.4/apr__pools_8h.html

Mind you, single pools are not thread safe, you'll have to handle that yourself.




回答4:


bget is another choice. It's well tested and production ready.

http://www.fourmilab.ch/bget/



来源:https://stackoverflow.com/questions/7060684/memory-pools-implementation-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!