游戏人生

不积跬步,无以至千里;不积小流,无以成江海。

导航

[原] inline operator delete & DLL boundary

很久以前写在百度空间的这篇文章:

[百度空间] [原] 全局operator delete重载到DLL

首先,纠正一个词“重载”,operator new/delete是替换(replacement),不是重载。只要任意一个编译单元定义了替换函数,并不需要全局声明,不用包含头文件,就会替换掉全局的默认operator new/delete, 这个行为非常像gcc的strong symbol/weak symbol。

 

最近翻看了C++03的标准:

3. The program’s definitions are used instead of the default versions supplied by the implementation (18.6). Such replacement occurs prior to program startup(3.2, 3.6). The program’s definitions shall not be specified as inline. No diagnostic is required.

也就是说标准是不允许inline operator new/delete。原因也很简单, new/delete是replacement,是链接级别的符号替换。如过是inline的话,很有可能没有生成符号,也就无法实现replacement,导致只有包含该头文件,引用声明的编译单元才会有replacement, 这样的行为与“全局替换”相悖。

 

然而标准又说No diagnostic is required, 估计是因为兼容性的原因,毕竟这个标准出来的时候,已经有很多编译器支持这么做了。所以MSVC最多会给一个警告,Clang后面也改成了可关闭的警告。

 

所以使用inline operator new/delete是不符合标准的,可能产生问题的,当然,有把握(运气)的话,并不会出错。比如我之前一直在用, 一些有名的开源项目也在用。

但这个方式给我一个很大的便利,就是可以fall back to default operator new:

默认情况下,只要任何一个文件定义了operator new/delete, 就会replacment,所以默认的new/delete已经被覆盖了,已经没有办法可以访问。
在使用了inline operator delete以后, 在自定义的operator delete里,仍然可以调用默认的operator delete。

//global.h
extern void __declspec( dllexport ) BaldeGlobalDelete(void* ptr);


inline void operator delete(void* ptr)
{
    return BaldeGlobalDelete(ptr);
}

//mem.cxx
#include <global.h>

extern void DefaultDelete(void* ptr);

void BaldeGlobalDelete(void* ptr)
{
    //note this is a demo code that not used in practice
    int* p = (int*)ptr-1;
    int magic = *p;
    if(magic == BLADE_MEM_MAGIC)
        BladeInternalFree(p);
    else
        DefaultDelete(ptr);
}

//defaultdelete.cxx
//this file doesn't include global.h 
//inline operator delete is not visible and no replacement happens!
//if define operator delete without inline(and in .cpp), replacement always happens, whether its decl is visible or not (correct/std conformed behavior)
void DefaultDelete(void* ptr) { //call the built in delete return ::operator delete(ptr); }

 

如过不加inline(并把定义写在cpp里)的话,replacement必然发生,所以DefaultDelete不会调用默认delete,而会递归调用自定义的operator delete,BaldeGlobalDelete,这样栈就溢出了。

有了inline(MSVC的debug build还需要改优化选项启用inline), 诡异的事情就发生了,实际上没有真正的replacment, 只要不包含global.h头文件,就能fall back到默认的delete。

 

这个是非常tricky的方式, 主要为了保证new/delete的一致性,兼容三方默认的new。其实可移植性和稳定性都不好,算是反面教材。最近为了standards-conformation,去掉了这种hack,使用了更标准的方式, 去掉了global operator delete 的替换。

 

关于DLL boundary的问题。

 

有很多方式可以解决这个问题,比如类COM的方式 (virutal destroy), 或者factory + deleter,或者使用统一的allocation routine (比如LocalAlloc之类).

 

Blade使用以下规范来保证:

1.跨DLL类 必须是纯虚基类, 或者,虚析构+类内自定义operator new/delete, 这样的便利是,可以直接跨DLL delete, C++标准保证。

基础类型的分配和释放,比如new int[count], 不允许跨DLL管理,实际上,不允许跨函数管理。个人认为strdup这种需要用户调用free的设计不是良好的设计。

使用shared_ptr + deleter是最好的方式,但目前暂不考虑,因为现在仍然基于C++03.

 

2.公共头文件不允许包含<vector> <string>等容器, 以避免不同版本stl(或者相同版本下debug、release库)对象的内存分布问题。 具体方法可以用纯虚类接口,或者Pimpl。

 

对于跨DLL的情况,不管用以上的哪种方式,对于DLL可卸载的情况,仍然会有问题,比如DLL/so卸载以后, 不管是virtual destory还是deleter,因为析构函数的二进制可执行代码已经unmap出进程地址空间不可访问,所以需要良好的

生命周期管理和cleanup routine。也可以偷懒,禁止卸载,比如使用GET_MODULE_HANDLE_EX_FLAG_PIN/RTLD_NODELETE。

posted on 2017-01-14 15:21  crazii  阅读(388)  评论(0编辑  收藏  举报