[Python-checkins] cpython: Add Py_MEMBER_SIZE macro

victor.stinner python-checkins at python.org
Thu Sep 8 12:44:47 EDT 2016


https://hg.python.org/cpython/rev/66a539984c9c
changeset: 103315:66a539984c9c
user: Victor Stinner <victor.stinner at gmail.com>
date: Thu Sep 08 09:33:56 2016 -0700
summary:
 Add Py_MEMBER_SIZE macro
Issue #27350: use Py_MEMBER_SIZE() macro to get the size of
PyDictKeyEntry.dk_indices, rather than hardcoding 8.
files:
 Include/pymacro.h | 3 +++
 Objects/dictobject.c | 20 ++++++++++++--------
 2 files changed, 15 insertions(+), 8 deletions(-)
diff --git a/Include/pymacro.h b/Include/pymacro.h
--- a/Include/pymacro.h
+++ b/Include/pymacro.h
@@ -18,6 +18,9 @@
 by "__LINE__". */
 #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]. */
 #define Py_CHARMASK(c) ((unsigned char)((c) & 0xff))
 
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -431,9 +431,10 @@
 dk = keys_free_list[--numfreekeys];
 }
 else {
- dk = PyObject_MALLOC(sizeof(PyDictKeysObject) - 8 +
- es * size +
- sizeof(PyDictKeyEntry) * usable);
+ dk = PyObject_MALLOC(sizeof(PyDictKeysObject)
+ - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
+ + es * size
+ + sizeof(PyDictKeyEntry) * usable);
 if (dk == NULL) {
 PyErr_NoMemory();
 return NULL;
@@ -2786,17 +2787,20 @@
 /* If the dictionary is split, the keys portion is accounted-for
 in the type object. */
 if (mp->ma_keys->dk_refcnt == 1)
- res += sizeof(PyDictKeysObject) - 8 + DK_IXSIZE(mp->ma_keys) * size +
- sizeof(PyDictKeyEntry) * usable;
+ res += (sizeof(PyDictKeysObject)
+ - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
+ + DK_IXSIZE(mp->ma_keys) * size
+ + sizeof(PyDictKeyEntry) * usable);
 return res;
 }
 
 Py_ssize_t
 _PyDict_KeysSize(PyDictKeysObject *keys)
 {
- return sizeof(PyDictKeysObject) - 8
- + DK_IXSIZE(keys) * DK_SIZE(keys)
- + USABLE_FRACTION(DK_SIZE(keys)) * sizeof(PyDictKeyEntry);
+ return (sizeof(PyDictKeysObject)
+ - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
+ + DK_IXSIZE(keys) * DK_SIZE(keys)
+ + USABLE_FRACTION(DK_SIZE(keys)) * sizeof(PyDictKeyEntry));
 }
 
 static PyObject *
-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /