Visual C++ Runtime:为内核实现 C++ 运行时
来源:OSDev Wiki· 阅读原文 ↗
文章目录
由于你无法将标准 C++ 运行时链接到内核,因此需要若干函数来替代其功能。本文提供有关如何为 Visual C++ 编译器实现自己的 C++ 运行时的信息。
为全局静态变量调用构造函数
此代码将帮助调用所有全局静态变量的构造函数。
// Constructor prototypes typedef void (__cdecl *_PVFV)(void); typedef int (__cdecl *_PIFV)(void); // Linker puts constructors between these sections, and we use them to locate constructor pointers. #pragma section(".CRT$XIA",long,read) #pragma section(".CRT$XIZ",long,read) #pragma section(".CRT$XCA",long,read) #pragma section(".CRT$XCZ",long,read) // Put .CRT data into .rdata section #pragma comment(linker, "/merge:.CRT=.rdata") // Pointers surrounding constructors __declspec(allocate(".CRT$XIA")) _PIFV __xi_a[] = { 0 }; __declspec(allocate(".CRT$XIZ")) _PIFV __xi_z[] = { 0 }; __declspec(allocate(".CRT$XCA")) _PVFV __xc_a[] = { 0 }; __declspec(allocate(".CRT$XCZ")) _PVFV __xc_z[] = { 0 }; extern __declspec(allocate(".CRT$XIA")) _PIFV __xi_a[]; extern __declspec(allocate(".CRT$XIZ")) _PIFV __xi_z[]; // C initializers extern __declspec(allocate(".CRT$XCA")) _PVFV __xc_a[]; extern __declspec(allocate(".CRT$XCZ")) _PVFV __xc_z[]; // C++ initializers // Call C constructors static int _initterm_e(_PIFV * pfbegin, _PIFV * pfend) { int ret = 0; // walk the table of function pointers from the bottom up, until // the end is encountered. Do not skip the first entry. The initial // value of pfbegin points to the first valid entry. Do not try to // execute what pfend points to. Only entries before pfend are valid. while ( pfbegin < pfend && ret == 0) { // if current table entry is non-NULL, call thru it. if ( *pfbegin != 0 ) ret = (**pfbegin)(); ++pfbegin; } return ret; } // Call C++ constructors static void _initterm (_PVFV * pfbegin, _PVFV * pfend) { // walk the table of function pointers from the bottom up, until // the end is encountered. Do not skip the first entry. The initial // value of pfbegin points to the first valid entry. Do not try to // execute what pfend points to. Only entries before pfend are valid. while ( pfbegin < pfend ) { // if current table entry is non-NULL, call thru it. if ( *pfbegin != 0 ) (**pfbegin)(); ++pfbegin; } } // Call this function as soon as possible. Basically should be at the moment you // jump into your C/C++ kernel. But keep in mind that kernel is not yet initialized, // and you can't use a lot of stuff in your constructors! bool CallConstructors() { // Do C initialization int initret = _initterm_e(__xi_a, __xi_z); if ( initret != 0 ) { return false; } // Do C++ initialization _initterm(__xc_a, __xc_z); return true; }
new 和 delete 运算符
你首先想做的事情之一就是实现 new 和 delete 运算符。起初,你无法真正实现它们,只需要提供它们的存根。之后,当你的内存管理器可以工作时,就可以完整实现它们。下面是这些存根:
void* __cdecl operator new(size_t size) { // Allocate memory return 0; } void* __cdecl operator new[](size_t size) { // Allocate memory return 0; } void __cdecl operator delete(void *p) { if (p == 0) { return; } // Release allocated memory } void __cdecl operator delete[](void *p) { if (p == 0) { return; } // Release allocated memory }
如果你想使用 placement new,则需要将以下实现放入头文件中,并在需要时包含它。
inline void* __cdecl operator new(size_t size, void* address) { return address; }
另请参阅
文章
外部链接
来源与翻译说明
本文根据原文快照翻译,保留原有代码、数值、技术限定及历史背景。原文中的命令与运行结果不代表本站已实际执行验证。
原文:Visual C++ Runtime;离线修订号:28135。对应原文版本

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论