Add Py_MEMBER_SIZE macro

Issue #27350: use Py_MEMBER_SIZE() macro to get the size of
PyDictKeyEntry.dk_indices, rather than hardcoding 8.
This commit is contained in:
Victor Stinner 2016-09-08 09:33:56 -07:00
parent 742da040db
commit 98ee9d5b73
2 changed files with 15 additions and 8 deletions

View file

@ -18,6 +18,9 @@
by "__LINE__". */ by "__LINE__". */
#define Py_STRINGIFY(x) _Py_XSTRINGIFY(x) #define Py_STRINGIFY(x) _Py_XSTRINGIFY(x)
/* Get the size of a structure member in bytes */
#define Py_MEMBER_SIZE(type, member) sizeof(((type *)0)->member)
/* Argument must be a char or an int in [-128, 127] or [0, 255]. */ /* Argument must be a char or an int in [-128, 127] or [0, 255]. */
#define Py_CHARMASK(c) ((unsigned char)((c) & 0xff)) #define Py_CHARMASK(c) ((unsigned char)((c) & 0xff))

View file

@ -431,9 +431,10 @@ static PyDictKeysObject *new_keys_object(Py_ssize_t size)
dk = keys_free_list[--numfreekeys]; dk = keys_free_list[--numfreekeys];
} }
else { else {
dk = PyObject_MALLOC(sizeof(PyDictKeysObject) - 8 + dk = PyObject_MALLOC(sizeof(PyDictKeysObject)
es * size + - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
sizeof(PyDictKeyEntry) * usable); + es * size
+ sizeof(PyDictKeyEntry) * usable);
if (dk == NULL) { if (dk == NULL) {
PyErr_NoMemory(); PyErr_NoMemory();
return NULL; return NULL;
@ -2786,17 +2787,20 @@ _PyDict_SizeOf(PyDictObject *mp)
/* If the dictionary is split, the keys portion is accounted-for /* If the dictionary is split, the keys portion is accounted-for
in the type object. */ in the type object. */
if (mp->ma_keys->dk_refcnt == 1) if (mp->ma_keys->dk_refcnt == 1)
res += sizeof(PyDictKeysObject) - 8 + DK_IXSIZE(mp->ma_keys) * size + res += (sizeof(PyDictKeysObject)
sizeof(PyDictKeyEntry) * usable; - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
+ DK_IXSIZE(mp->ma_keys) * size
+ sizeof(PyDictKeyEntry) * usable);
return res; return res;
} }
Py_ssize_t Py_ssize_t
_PyDict_KeysSize(PyDictKeysObject *keys) _PyDict_KeysSize(PyDictKeysObject *keys)
{ {
return sizeof(PyDictKeysObject) - 8 return (sizeof(PyDictKeysObject)
+ DK_IXSIZE(keys) * DK_SIZE(keys) - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
+ USABLE_FRACTION(DK_SIZE(keys)) * sizeof(PyDictKeyEntry); + DK_IXSIZE(keys) * DK_SIZE(keys)
+ USABLE_FRACTION(DK_SIZE(keys)) * sizeof(PyDictKeyEntry));
} }
static PyObject * static PyObject *