zoukankan      html  css  js  c++  java
  • Python源码学习十一 一个常用的内存分配函数

    void *
    _PyObject_DebugMallocApi(char id, size_t nbytes)
    {
        uchar *p;           /* base address of malloc'ed block */
        uchar *tail;        /* p + 2*SST + nbytes == pointer to tail pad bytes */
        size_t total;       /* nbytes + 4*SST */
    
        bumpserialno();
        total = nbytes + 4*SST;
        if (total < nbytes)
            /* overflow:  can't represent total as a size_t */
            return NULL;
    
        p = (uchar *)PyObject_Malloc(total);
        if (p == NULL)
            return NULL;
    
        /* at p, write size (SST bytes), id (1 byte), pad (SST-1 bytes) */
        write_size_t(p, nbytes);
        p[SST] = (uchar)id;
        memset(p + SST + 1 , FORBIDDENBYTE, SST-1);
    
        if (nbytes > 0)
            memset(p + 2*SST, CLEANBYTE, nbytes);
    
        /* at tail, write pad (SST bytes) and serialno (SST bytes) */
        tail = p + 2*SST + nbytes;
        memset(tail, FORBIDDENBYTE, SST);
        write_size_t(tail + SST, serialno);
    
        return p + 2*SST;
    }
    
    SST是宏定义 4
    执行的实际作用是把nbytes的值(360 in this case)写在内存区的前四个字节,然后是一个uchar型的id , 'o' in this case
    接着是nbytes个浩浩荡荡的0xcb
    然后是4个oxfb, 和hex形式的serialno



    (PyFrameObject*)op0x00b25528
    (*((PyFrameObject*)op)).f_localsplus0x00b25668


    我们看到f_localsplus的值正是 op + offset

    offset is the f_localsplus offset in PyFrameObject definition

  • 相关阅读:
    单据存储过程
    C语言II博客作业04
    C语言II博客作业03
    C语言II博客作业02
    C语言II博客作业01
    学期总结
    C语言I博客作业09
    C语言I博客作业08
    C语言I博客作业07
    C语言I博客作业06
  • 原文地址:https://www.cnblogs.com/riasky/p/3458815.html
Copyright © 2011-2022 走看看