惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hacker News - Newest:
Hacker News - Newest: "LLM"
N
News and Events Feed by Topic
PCI Perspectives
PCI Perspectives
Help Net Security
Help Net Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
V2EX - 技术
V2EX - 技术
Google Online Security Blog
Google Online Security Blog
Hacker News: Ask HN
Hacker News: Ask HN
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
P
Palo Alto Networks Blog
The Last Watchdog
The Last Watchdog
C
Cyber Attacks, Cyber Crime and Cyber Security
Security Latest
Security Latest
Application and Cybersecurity Blog
Application and Cybersecurity Blog
W
WeLiveSecurity
Cloudbric
Cloudbric
O
OpenAI News
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Webroot Blog
Webroot Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
S
Schneier on Security
罗磊的独立博客
雷峰网
雷峰网
S
Security Affairs
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
L
LINUX DO - 热门话题
NISL@THU
NISL@THU
量子位
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 【当耐特】
博客园 - Franky
S
Security @ Cisco Blogs
Project Zero
Project Zero
AI
AI
T
Troy Hunt's Blog
Latest news
Latest news
Simon Willison's Weblog
Simon Willison's Weblog
S
SegmentFault 最新的问题
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Tailwind CSS Blog
The Cloudflare Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Scott Helme
Scott Helme
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Jina AI
Jina AI
I
Intezer
V
Visual Studio Blog
Cyberwarzone
Cyberwarzone
博客园 - 叶小钗

博客园 - lbq1221119

时间同步算法与Simple Ring-based election algorithm算法分析 SSCLI中GC垃圾回收源码分析(3) - GarbageCollectGeneration()与SuspendEE SSCLI中GC垃圾回收源码分析(2) - GarbageCollect()与Spin Lock SSCLI中GC源码分析(1) - EE与BCL之间的调用接口FCall (北京宇思信德科技公司)诚聘C#软件工程师 , Middle及Senior Level .Net Micro framework在开发过程中的bugs/problems及解决方案。 MF中使用GPRS:如何通过一个串口终端实现GPRS Modem拨号上网 博客园北京俱乐部第三次活动讲义PPT ValueType.Equals(null)的底层实现及CLR虚拟机对其结构支持 WebService传输数据流及数据交互解析 托管线程退出之后Dump文件特征 国外精品研究论文(持续更新) Random()中具体实现(含种子数组的实现) From double Click to Main: PAL initialization - lbq1221119 From double Click to Main: The initialization of Process in OS Microsoft Micro Framework 3.0对Serial Peripheral Interface 的支持 使用CorDbg进行托管调试 CLR内核调试之:Malloc函数实现 Build SSCLI20 under VS2008 full Document (完全手册)
MethodTable内存空间分配中加法运算算法解析
lbq1221119 · 2008-11-05 · via 博客园 - lbq1221119

  在分析MethodTable具体分配内存实现的时候,看到了计算MethodTable的大小,然后分配空间的算法。其中有个加法运算实现的非常赞,特地截取出来。

         所有的MethodTable的分配,都是通过methodtable中的一个static方法AllocagteNewMT来实现的,该方法定义如下:

MethodTable * MethodTable::AllocagteNewMT(EEClass *pClass,

                                         DWORD dwVtableSlots,

                                         DWORD dwGCSize,

                                         DWORD dwNumInterfaces,

                                         DWORD numGenericArgs,

                                         DWORD dwNumDicts,

                                         DWORD cbDict,

                                         ClassLoader *pClassLoader,

                                         BaseDomain *pDomain,

                                         BOOL isInterface,

                                         BOOL fHasGenericsStaticsInfo,

                                         BOOL fNeedsRemotableMethodInfo,

                                         BOOL fNeedsRemotingVtsInfo,

                                         BOOL fHasThreadOrContextStatics

        , AllocMemTracker *pamTracker

)

         下面是该方法中计算大小的一段,采用模板来忽略类型带来的影响:

      DWORD cbTotalSize = 0;

      DWORD cbDicts = 0;

      if (!ClrSafeInt<DWORD>::multiply(dwNumDicts, sizeof(TypeHandle*), cbDicts) ||

          !ClrSafeInt<DWORD>::addition((DWORD)size, cbDicts, cbTotalSize) ||

          !ClrSafeInt<DWORD>::addition(cbTotalSize, dwGCSize, cbTotalSize))

          ThrowHR(COR_E_OVERFLOW);

         然后转到addition((DWORD)size, cbDicts, cbTotalSize)的实现,加法的实现如下,加入了对各种情况的严格考虑:

    // Returns true if safe, false on overflow

    static inline bool addition(T lhs, T rhs, T &result)

{

                   //check for T first.

        if(IsSigned())

        {

            //test for +/- combo

            if(!IsMixedSign(lhs, rhs))

            {

                //either two negatives, or 2 positives, not mixed symbols

                if(rhs < 0)

                {

                    //two negatives

                    if(lhs < (T)(MinInt() - rhs)) //remember rhs < 0

                    {

                        return false;

                    }

                    //ok

                }

                else

                {

                    //two positives

                    if((T)(MaxInt() - lhs) < rhs)

                    {

                        return false;

                    }

                    //OK

                }

            }

            //else overflow not possible

            result = lhs + rhs;

            return true;

        }

        else //unsigned, and two symbols is mixed

        {

            if((T)(MaxInt() - lhs) < rhs)

            {

                return false;               

            }

            result = lhs + rhs;

            return true;

        }

}

其中,涉及到中间调用的几个方法如下:

static bool IsSigned()

{

return( (T)-1 < 0 );

}

//Check if lhs and rhs is mixed Sign symbols

static bool IsMixedSign(T lhs, T rhs)

{

return ((lhs ^ rhs) < 0);

}

//both of the following should optimize away

static T MinInt()

{

if(IsSigned())

                   {

return (T)((T)1 << (BitCount()-1));

                   }

    else

                   {

return ((T)0);

                   }

}

static T MaxInt()

{

if(IsSigned())

                   {

return (T)~((T)1 << (BitCount()-1));

}

//else

return (T)(~(T)0);

}

检查的挺详细的,实现的也挺不错。

lbq1221110@Cnblogs.  11.5 ; first post at sscli.cnblogs.com