设计:如何声明专门的内存处理程序类

发布于 2024-08-29 03:29:07 字数 523 浏览 6 评论 0原文

在嵌入式类型系统上,我创建了一个小对象分配器,它搭载在标准内存分配系统之上。该分配器是一个 Boost::simple_segreated_storage<> 分配器。类,它完全满足了我的需要 - 在小对象上分配/释放时间为 O(1),但代价是产生了一些内部碎片。我的问题是如何最好地声明它。现在,它在我们的 mem 代码模块中声明为静态作用域,这可能没问题,但感觉有点暴露在那里,而且现在也永远链接到该模块。通常,我将其声明为单态或单例,但这使用动态内存分配器(它所在的位置)。此外,我们的动态内存分配器在我们的系统上发生静态对象初始化之前被初始化和使用(同样,内存管理器几乎是引擎最基本的组件。)为了解决这个问题 22,我添加了一个额外的“如果小内存分配器存在”来查看小对象分配器是否存在。如果现在必须在每个小对象分配上运行。从整体上看,这几乎可以忽略不计,但它仍然困扰着我。

所以问题是,是否有更好的方法来声明内存管理器的这一部分,以帮助将其与内存模块解耦,并且可能不会花费额外的 isinitialized() if 语句?如果此方法使用动态内存,请解释如何解决管理器小对象部分缺乏初始化的问题。

On an embedded type system, I have created a Small Object Allocator that piggy backs on top of a standard memory allocation system. This allocator is a Boost::simple_segregated_storage<> class and it does exactly what I need - O(1) alloc/dealloc time on small objects at the cost of a touch of internal fragmentation. My question is how best to declare it. Right now, it's scope static declared in our mem code module, which is probably fine, but it feels a bit exposed there and is also now linked to that module forever. Normally, I declare it as a monostate or a singleton, but this uses the dynamic memory allocator (where this is located.) Furthermore, our dynamic memory allocator is being initialized and used before static object initialization occurs on our system (as again, the memory manager is pretty much the most fundamental component of an engine.) To get around this catch 22, I added an extra 'if the small memory allocator exists' to see if the small object allocator exists yet. That if that now must be run on every small object allocation. In the scheme of things, this is nearly negligable, but it still bothers me.

So the question is, is there a better way to declare this portion of the memory manager that helps decouple it from the memory module and perhaps not costing that extra isinitialized() if statement? If this method uses dynamic memory, please explain how to get around lack of initialization of the small object portion of the manager.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

歌枕肩 2024-09-05 03:29:07

一个好的指导原则是:说出你的意思,除非有充分的理由不这样做。该分配器是一个全局静态对象,应该这样声明。现在,如果它的状态需要初始化,我会在初始化动态内存分配器的代码中执行此操作——因为这实际上是初始化内存分配系统工作的一部分,这又属于说出你的意思的标题。这将避免每次调用时进行不优雅的条件检查。

A good guideline is: say what you mean, unless there is good reason to do otherwise. This allocator is a global static object, and should be declared as such. Now if its state needs initializing, I would do that in the code that initializes the dynamic memory allocator -- since this is in fact part of the work of initializing the memory allocation system, this again falls under the heading of saying what you mean. That would avoid the inelegant conditional check on every call.

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