atl的全局表原理.

本文介绍了一个使用C++实现的特定内存区域管理方案,通过pragma section指令定义不同的内存段,并展示了如何在这些段中分配静态对象及对象映射表的创建过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

atlbase.h

#include <string>
#pragma section("ATL$_a", read)
#pragma section("ATL$_z", read)
#pragma section("ATL$_m", read)

struct Student {
    std::string name;
    int age;
};

extern "C" {
    __declspec(selectany) __declspec(allocate("ATL$_a")) Student* pst_begin = NULL;
    __declspec(selectany) __declspec(allocate("ATL$_z")) Student* pst_end = NULL;
}

keke.h

#include "atlbase.h"

const Student st_xiaoming = { "xiaoming", 30 };
const Student st_xiaose = { "xiaose", 30 };

extern "C"  __declspec(allocate("ATL$_m")) /*__declspec(selectany)*/ const Student* const pst_xiaose = &st_xiaose;
extern "C"  __declspec(allocate("ATL$_m")) /*__declspec(selectany)*/ const Student* const pst_xiaoming = &st_xiaoming;

main

int _tmain(int argc, _TCHAR* argv[])
{
    for (auto pp = &pst_begin + 1; pp != &pst_end; ++pp) {
        if (*pp) {
            std::cout << (*pp)->name << "\t" << (*pp)->age << "\n";
        }
    }
    func();
    return 0;
}

使用pragma section新建一个section,然后在里边放指针。

#pragma section("ATL$__a", read)
#pragma section("ATL$__z", read)
#pragma section("ATL$__m", read)
extern "C"
{
__declspec(selectany) __declspec(allocate("ATL$__a")) _ATL_OBJMAP_ENTRY_EX* __pobjMapEntryFirst = NULL;
__declspec(selectany) __declspec(allocate("ATL$__z")) _ATL_OBJMAP_ENTRY_EX* __pobjMapEntryLast = NULL;
}

#if !defined(_M_IA64)
#pragma comment(linker, "/merge:ATL=.rdata")
#endif
#define OBJECT_ENTRY_AUTO(clsid, class) \
    __declspec(selectany) ATL::_ATL_OBJMAP_CACHE __objCache__##class = { NULL, 0 }; \
    const ATL::_ATL_OBJMAP_ENTRY_EX __objMap_##class = {&clsid, class::UpdateRegistry, class::_ClassFactoryCreatorClass::CreateInstance, class::_CreatorClass::CreateInstance, &__objCache__##class, class::GetObjectDescription, class::GetCategoryMap, class::ObjectMain }; \
    extern "C" __declspec(allocate("ATL$__m")) __declspec(selectany) const ATL::_ATL_OBJMAP_ENTRY_EX* const __pobjMap_##class = &__objMap_##class; \
    OBJECT_ENTRY_PRAGMA(class)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值