greenlet source code -- g_save

g_save greenlet存在heap上的数据区间由stack_copystack_copy + stack_saved指定 这里在把greenlet的数据存到heap上时 判断之前的那段区间是否足够存放 如果不够的话 重新申请内存

static int g_save(PyGreenlet* g, char* stop)
{
    /* Save more of g's stack into the heap -- at least up to 'stop'  */

在此输入图片描述

    intptr_t sz1 = g->stack_saved;
    intptr_t sz2 = stop - g->stack_start;
    assert(g->stack_start != NULL);
    if (sz2 > sz1) {
        char* c = (char*)PyMem_Realloc(g->stack_copy, sz2);
        if (!c) {
            PyErr_NoMemory();
            return -1;
        }
        memcpy(c+sz1, g->stack_start+sz1, sz2-sz1);
        g->stack_copy = c;
        g->stack_saved = sz2;
    }
    return 0;
}

你可能感兴趣的:(greenlet source code -- g_save)