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

推荐订阅源

M
MIT News - Artificial intelligence
WordPress大学
WordPress大学
GbyAI
GbyAI
S
SegmentFault 最新的问题
量子位
爱范儿
爱范儿
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
MyScale Blog
MyScale Blog
人人都是产品经理
人人都是产品经理
博客园 - 叶小钗
aimingoo的专栏
aimingoo的专栏
V
Visual Studio Blog
U
Unit 42
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
The Cloudflare Blog
Stack Overflow Blog
Stack Overflow Blog
博客园 - 聂微东
J
Java Code Geeks
The GitHub Blog
The GitHub Blog
Y
Y Combinator Blog
IT之家
IT之家
Martin Fowler
Martin Fowler
宝玉的分享
宝玉的分享
雷峰网
雷峰网

博客园 - 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