类似的malloc函数中使用自定义的堆 [英] malloc like function using custom heap

查看:475
本文介绍了类似的malloc函数中使用自定义的堆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是用C最好的办法,如果我想构建的malloc像一个自定义的pre-分配的堆的功能?

What would be the best approach in C if I wish to construct malloc like functionality with a custom pre-allocated heap?

我在这里的具体问题是,我有一个已经被放置到我的地址空间的mmap-能(内存等)的设备,但我需要获得使用该内存来存储将被分配和释放对象的更灵活的方式随着时间的推移。

My specific issue here is that I have a mmap-able (memory like) device which has been placed into my address space but I need to attain a more flexible way of using this memory to store objects which will be allocated and freed over time.

我知道malloc的,免费的,其他类似功能用于在堆上执行这种分配,但有什么办法可以使用​​由这种功能为它的动态行为提供逻辑,同时提供我自己的地址空间作为有问题的堆操作?

I know that malloc, free and the other similar functions are used to perform this kind of allocation on the heap but is there any way to use the logic provided by this kind of function for its dynamic behaviour while providing my own address space to operate as the heap in question?

推荐答案

Boost.Interprocess中具有适合于共享存储器段状态分配器:它们可以是可重复使用的其他mmapped地址范围太

Boost.Interprocess has stateful allocators suitable for shared memory segments: they may be re-usable for other mmapped address ranges too.

否则,你可能需要推出自己的。在复杂的递增的顺序,你可以考虑:

Otherwise, you may need to roll your own. In increasing order of complexity, you could consider:


  1. 一个简单的舞台上分配器:这几乎是微不足道的,但没有办法释放单个对象和重用他们的记忆

  2. 一个简单的对象池分配:这适用于固定大小的物体,几乎没有开销(假定对象至少是大到一个指针,可以维持释放的对象的单链表)

  3. 的混合动力系统与不同尺寸的多个对象池(但每个池是单独一个简单的固定大小的实例)

  4. 某种板坯/竹节分配器(多个固定大小的池共享的大型固定大小的砖简单的底层分配器)

  5. SLOB 分配器

  6. 一个完整的malloc /免费实现(一些是开源的,所以你可以采取执行和撕裂任何东西你不需要)。

其中哪些是合适的,将取决于你有没有给出一些信息:

Which of those are suitable will depend on some information you haven't given:


  • 对象大小

    • 对象池工作,如果你只有一个,或只是少数,对象尺寸分配

    • 舞台分配器不关心对象大小

    • 既不支持的realloc

    • object size
      • object pools work if you have only one, or only a few, sizes of object to allocate
      • arena allocators don't care about object size
      • neither support realloc

      • 对象池一般都支持任意的malloc /免费序列

      • 阿里纳斯通常允许释放唯一的全在一次(所以你只要竞技场重置为空状态)。你可以修改这个允许LIFO释放。


      • 全面堆实现将可能是最慢的,最复杂的,但也是最灵活

      • SLOB更容易和更轻的重量,但患有碎片的详细

      这篇关于类似的malloc函数中使用自定义的堆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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