构建组件数据池
我运行的是带有 Core Duo 的 Windows 7。我已经使用 Codeblocks 一段时间了,我想我已经使用了 GNU 编译器。
我有(或将有)许多从抽象类 Component
继承的组件类。我需要帮助建立一个系统来容纳未确定数量的组件类型的内存池,每个组件类型都有未确定数量的组件。也许为了争论,后者是固定的,我可以弄清楚如何在绝对必要时改变大小。
此外,为了方便起见,每个组件类型都有一个 ID(无符号短
),从 0 到任何没有漏洞的值。耶!
那么,假设我有一些管理类来跟踪数组大小和事物,这样的东西会起作用吗?
class Component {};
class FooComponent: public Component {};
class BarComponent: public Component {};
.
.
.
std::vector<Component*> pool_list;
// returns pointer to beginning of pool
Component* foo_pool = MethodToCreateComponentPool(/* Params */)
Component* bar_pool = MethodToCreateComponentPool(/* Params */)
pool_list.push_back(foo_pool);
pool_list.push_back(bar_pool);
也许将 sizeof(FooComponent)
放入参数中。
然后(我真的开始不知道该怎么做)在函数内部执行 malloc( START_COUNT * component_size)
我没有这样分配内存的经验。我可以轻松设置类似
vector<矢量
或map<无符号短整型,向量
但它(对于一个人来说很难看)并没有给我连续的池。我需要缓存友好:)
有想法吗?图案?成语?帮助?
I'm running Windows 7 with a Core Duo. I've been using Codeblocks for a while now, which I think I have using GNU compiler.
I have (or will have) many component classes that inherit from the abstract class Component
. I need help establishing a system to house memory pools for an undetermined amount of component types, each with an undetermined amount of components. Maybe for the sake of argument the latter is fixed, I can figure out how to change size when abolutely necessary.
Also, conveniently so, each component type has an ID (unsigned short
) from 0 to whatever with no holes. Yay!
So, assuming I have some managing clas to keep track of array sizes and things, would something like this work?
class Component {};
class FooComponent: public Component {};
class BarComponent: public Component {};
.
.
.
std::vector<Component*> pool_list;
// returns pointer to beginning of pool
Component* foo_pool = MethodToCreateComponentPool(/* Params */)
Component* bar_pool = MethodToCreateComponentPool(/* Params */)
pool_list.push_back(foo_pool);
pool_list.push_back(bar_pool);
Maybe put sizeof(FooComponent)
in for Params.
Then (Where I really start to not know what to do) inside the function do an malloc( START_COUNT * component_size)
I have no experience allocating memory like this. I can easily set up something like
vector< vector<Component*>* >
ormap<unsigned short, vector<Component*> >
but it (is ugly for one) doesn't give me contiguous pools. I need cache friendly :)
Ideas? Patterns? Idioms? Help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我全神贯注于如何实现这一点,而忘记了我真正需要什么。基本上就是这个发布。
谢谢。
I got wrapped up in how I was implementing this and lost track of what I really needed. Which is basically this post.
Thanks.