给定一个数组,是否有一种算法可以从中分配内存?

发布于 2024-07-30 14:55:43 字数 176 浏览 5 评论 0原文

我正在做一些图形编程,并且正在使用顶点池。 我希望能够从池中分配一个范围并将其用于绘图。

我需要的解决方案与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冰葑 2024-08-06 14:55:43

一般来说:您正在寻找一个内存管理器,它使用(参见维基百科)内存池 (就像 boost::pool正如 TokenMacGuy 的回答)。 它们有多种口味。 重要考虑因素:

  • 块大小(固定或可变;不同块大小的数量;可以(统计)预测块大小的使用情况吗?
  • 效率(一些管理器有 2^n 块大小,即用于搜索最佳拟合的网络堆栈)块;非常好的性能并且没有以浪费内存为代价的碎片)
  • 管理开销(我假设您将有许多非常小的块;因此内存管理器维护的整数和指针的数量对于效率来说非常重要

) 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:

  • block size (fixed or variable; number of different block sizes; can the block size usage be predicted (statistically)?
  • efficiency (some managers have 2^n block sizes, i.e. for use in network stacks where they search for best fit block; very good performance and no fragementation at the cost of wasting memory)
  • administration overhead (I presume that you'll have many, very small blocks; so the number of ints and pointers maintainted by the memory manager is significant for efficiency)

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.

过期以后 2024-08-06 14:55:43

boost::pool 为您做到这一点很友好地!

boost::pool does this for you very nicely!

零度℉ 2024-08-06 14:55:43

相反,我预先分配数组,然后需要一个对象来包装该数组并跟踪可用空间,并从我传入的分配中分配一个范围(一对开始/结束指针)。

基本上 malloc() 内部执行的操作(不过,如果“预分配数组”已满,malloc() 可以增加该数组的大小)。 所以是的,有一个算法。 事实上,有很多,维基百科给出了基本的概述。 不同的策略在不同的情况下可以发挥更好的作用。 (例如,如果所有块的大小相似,或者是否有某种分配和释放模式)

如果您有许多相同大小的对象,请查看 obstacks。

您可能不想自己编写代码,这不是一件容易的任务,而且错误可能会很痛苦。

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文