在C内存池的实现 [英] Memory pools implementation in C

查看:150
本文介绍了在C内存池的实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要寻找一个在C良好的内存池的实现。

它应该包括以下内容:


  1. 防碎片。

  2. 是超级快:)

  3. 能够捆绑,从不同规模划分几个在一些标识,并删除所有分配给定的标识符。

  4. 线程安全


解决方案

我觉得优秀的 talloc ,发展成为桑巴舞的一部分,可能是你在找什么。我觉得最有趣的部分是从talloc返回的指针是一个有效的存储环境。他们的例子是:

 结构美孚* X = talloc(mem_ctx,结构foo的);
X->名称= talloc_strdup(X,富);
// ...
talloc_free(X); //释放内存为X和X-GT&;名

在针对您的特定点:

(1)不知道什么防碎裂在这种情况下。在C你不会得到压缩反正垃圾收集,所以我觉得你的选择是比较有限的。

(2)通告比普通的要慢只有4%的malloc(3),这是相当快的。

(3)查看上面的例子。

(4)它是线程安全,只要不同的线程使用不同的上下文&放大器;底层的malloc是线程安全的。

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

解决方案

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.

这篇关于在C内存池的实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆