替换默认的STL分配器 [英] Replace default STL allocator

查看:109
本文介绍了替换默认的STL分配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大(> 250个文件)库的源,大量使用STL容器和字符串。我需要在有限堆的嵌入式环境中运行它,所以我想确保这个库本身在堆使用中受到限制。

I have the source for a large (>250 files) library that makes heavy use of STL containers and strings. I need to run it in an embedded environment with limited heap, so I want to ensure that this library itself is limited in its heap usage.

显而易见的解决方案是创建一个分配器,但是修改整个代码库以包含分配器模板参数是一个最后的手段,如果我想要获取新版本的源,这是不可取的。全局替换新的和删除是不可行的,因为这影响整个图像,而不只是这个库。

The obvious solution is to create an allocator, but modifying the entire code base to include the allocator template parameter is a big job of last resort, and undesirable in case I ever want to take a new version of the source. Globally replacing new and delete is not feasible since that affects the entire image, not just this library.

我的下一个想法是一个愚蠢的C宏技巧,但不是似乎有可能,虽然我承认不是一个聪明的宏作者。

My next thought was a stupid C macro trick, but that doesn't seem like it would be possible, although I admit to not being a clever macro author.

所以我想有一个编译器或pragma开关来指定分配器<类在编译时?

So I thought "is there a compiler or pragma switch to specify the allocator<> class at compile time"? But I'm open for anything.

我会问的下一个问题,如果有人能提出一个解决方案,是如何做同样的新/删除

The next question I'll ask, if anyone can come up with a solution, is how to do the same for new/delete within the set of files comprising this library.

我使用gcc 3.4.4工具链在Cygwin下运行这个文件,使用VxWorks的目标文件

I'm using the gcc 3.4.4 toolchain to run this under Cygwin, with a target of VxWorks, if that sparks any ideas.

推荐答案

我诉诸预处理器来获得一个可能的解决方案,虽然它目前依赖于GCC 3.4.4实现工作

I resorted to the preprocessor to get a possible solution, although it currently relies upon the GCC 3.4.4 implementation to work.

GCC < memory> 实现包括文件< bits / allocator.h> ,它又包括另一个文件< bits / c ++ allocator.h> ,它定义了一个宏,类实现默认分配器基类。

The GCC <memory> implementation includes the file <bits/allocator.h>, which in turn includes another file <bits/c++allocator.h>, which defines a macro that defines the class implementing the default allocator base class.

因为在平台相关路径中找到 / lib / gcc / i686-pc -cygwin / 3.4.4 / include / c ++ / i686-pc-cygwin / bits ,我不觉得(非常)肮脏的代替它与我自己的平台依赖实现。

Since is found in a platform-dependent path (/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/i686-pc-cygwin/bits), I don't feel (very) dirty in supplanting it with my own "platform-dependent" implementation.

所以我只是在源的include路径的根目录中创建一个文件夹 bits / 在该文件夹中创建文件 c ++ allocator.h 。我定义所需的宏是我的分配器类的名称,它的工作原理像一个charm,因为gcc搜索我的包含路径之前搜索系统包括。

So I just create a folder bits/ in the root of my source's include path, and then create the file c++allocator.h in that folder. I define the required macro to be the name of my allocator class and it works like a charm, since gcc searches my include paths prior to searching the system includes.

感谢所有您的回复。我想我可以用这个解决方案,这将只有工作,只要我使用3.4.4可能。

Thanks for all your responses. I think I can go with this "solution", which will only work as long as I'm using 3.4.4 probably.

这篇关于替换默认的STL分配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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