Search code examples
cdictionarymacroscpython

what 's meanning of the marco `DK_ENTRIES` in cpython code dictobject.c


I read the code of dictobject.c , can not figure out this

#define DK_ENTRIES(dk) \
    ((PyDictKeyEntry*)(&((int8_t*)((dk)->dk_indices))[DK_SIZE(dk) * DK_IXSIZE(dk)]))

any one can explain this macro ,hep me to read this macro and what this for ?


Solution

  • After some struggle,I find something below:

    First dk is PyDictKeysObject:

    typedef struct _dictkeysobject PyDictKeysObject;
    struct _dictkeysobject {
        Py_ssize_t dk_refcnt;
        Py_ssize_t dk_size;
        dict_lookup_func dk_lookup;
        Py_ssize_t dk_usable;
        Py_ssize_t dk_nentries;
        char dk_indices[];
    };
    

    note the last elementdk_indices , flexible array member

    simply,

    DK_SIZE is the dk_size in struct;

    DK_IXSIZE can be 1 or 2 or 4 or 8

    ((int8_t*)((dk)->dk_indices))[DK_SIZE(dk) * DK_IXSIZE(dk)]

    is a pointer with n(dk_size*1?) offset

    and the end return a pointer to PyDictKeyEntry

    Finally, This macro return a PyDictKeyEntry, which have n(dk_size*1?) offset with dk->dk_indices