给定一个数组,是否有一种算法可以从中分配内存?
我正在做一些图形编程,并且正在使用顶点池。 我希望能够从池中分配一个范围并将其用于绘图。
我需要的解决方案与 C 分配器的不同之处在于我从不调用 malloc。 相反,我预先分配数组,然后需要一个对象来包装该数组并跟踪可用空间,并从我传入的分配中分配一个范围(一对开始/结束指针)。
非常感谢。
I'm doing some graphics programming and I'm using Vertex pools. I'd like to be able to allocate a range out of the pool and use this for drawing.
Whats different from the solution I need than from a C allocator is that I never call malloc. Instead I preallocate the array and then need an object that wraps that up and keeps track of the free space and allocates a range (a pair of begin/end pointers) from the allocation I pass in.
Much thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一般来说:您正在寻找一个内存管理器,它使用(参见维基百科)内存池 (就像 boost::pool正如 TokenMacGuy 的回答)。 它们有多种口味。 重要考虑因素:
) boost::pool,我认为 简单隔离存储值得一看。
它将允许您配置具有许多不同块大小的内存池,并搜索最佳匹配。
in general: you're looking for a memory mangager, which uses a (see wikipedia) memory pool (like the boost::pool as answered by TokenMacGuy). They come in many flavours. Important considerations:
In case of boost::pool, I think the simple segragated storage is worth a look.
It will allow you to configure a memory pool with many different block sizes for which a best-match is searched for.
boost::pool 为您做到这一点很友好地!
boost::pool does this for you very nicely!
基本上 malloc() 内部执行的操作(不过,如果“预分配数组”已满,malloc() 可以增加该数组的大小)。 所以是的,有一个算法。 事实上,有很多,维基百科给出了基本的概述。 不同的策略在不同的情况下可以发挥更好的作用。 (例如,如果所有块的大小相似,或者是否有某种分配和释放模式)
如果您有许多相同大小的对象,请查看 obstacks。
您可能不想自己编写代码,这不是一件容易的任务,而且错误可能会很痛苦。
That's basically what malloc() does internally (malloc() can increase the size of this "preallocated array" if it gets full, though). So yes, there is an algorithm for it. There are many, in fact, and Wikipedia gives a basic overview. Different strategies can work better in different situations. (E.g. if all the blocks are a similar size, or if there's some pattern to allocation and freeing)
If you have many objects of the same size, look into obstacks.
You probably don't want to write the code yourself, it's not an easy task and bugs can be painful.