Skip to content

Commit 758df56

Browse files
committed
pythongh-128013: Convert unicodeobject.c macros to functions
Convert unicodeobject.c macros to static inline functions. * Add _PyUnicode_SET_UTF8() and _PyUnicode_SET_UTF8_LENGTH() macros. * Add PyUnicode_HASH() and PyUnicode_SET_HASH() macros. * Remove unused _PyUnicode_KIND() and _PyUnicode_GET_LENGTH() macros.
1 parent 335e24f commit 758df56

File tree

1 file changed

+71
-45
lines changed

1 file changed

+71
-45
lines changed

Objects/unicodeobject.c

Lines changed: 71 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -112,47 +112,73 @@ NOTE: In the interpreter's initialization phase, some globals are currently
112112
# define _PyUnicode_CHECK(op) PyUnicode_Check(op)
113113
#endif
114114

115-
#define _PyUnicode_UTF8(op) \
116-
(_PyCompactUnicodeObject_CAST(op)->utf8)
117-
#define PyUnicode_UTF8(op) \
118-
(assert(_PyUnicode_CHECK(op)), \
119-
PyUnicode_IS_COMPACT_ASCII(op) ? \
120-
((char*)(_PyASCIIObject_CAST(op) + 1)) : \
121-
_PyUnicode_UTF8(op))
122-
#define _PyUnicode_UTF8_LENGTH(op) \
123-
(_PyCompactUnicodeObject_CAST(op)->utf8_length)
124-
#define PyUnicode_UTF8_LENGTH(op) \
125-
(assert(_PyUnicode_CHECK(op)), \
126-
PyUnicode_IS_COMPACT_ASCII(op) ? \
127-
_PyASCIIObject_CAST(op)->length : \
128-
_PyUnicode_UTF8_LENGTH(op))
115+
static inline char* _PyUnicode_UTF8(PyObject *op)
116+
{
117+
return (_PyCompactUnicodeObject_CAST(op)->utf8);
118+
}
119+
120+
static inline char* PyUnicode_UTF8(PyObject *op) {
121+
assert(_PyUnicode_CHECK(op));
122+
if (PyUnicode_IS_COMPACT_ASCII(op)) {
123+
return ((char*)(_PyASCIIObject_CAST(op) + 1));
124+
}
125+
else {
126+
return _PyUnicode_UTF8(op);
127+
}
128+
}
129+
130+
static inline void PyUnicode_SET_UTF8(PyObject *op, char *utf8)
131+
{
132+
_PyCompactUnicodeObject_CAST(op)->utf8 = utf8;
133+
}
134+
135+
static inline Py_ssize_t PyUnicode_UTF8_LENGTH(PyObject *op) {
136+
assert(_PyUnicode_CHECK(op));
137+
if (PyUnicode_IS_COMPACT_ASCII(op)) {
138+
return _PyASCIIObject_CAST(op)->length;
139+
}
140+
else {
141+
return _PyCompactUnicodeObject_CAST(op)->utf8_length;
142+
}
143+
}
144+
145+
static inline void PyUnicode_SET_UTF8_LENGTH(PyObject *op, Py_ssize_t length) {
146+
_PyCompactUnicodeObject_CAST(op)->utf8_length = length;
147+
}
129148

130149
#define _PyUnicode_LENGTH(op) \
131150
(_PyASCIIObject_CAST(op)->length)
132151
#define _PyUnicode_STATE(op) \
133152
(_PyASCIIObject_CAST(op)->state)
134153
#define _PyUnicode_HASH(op) \
135154
(_PyASCIIObject_CAST(op)->hash)
136-
#define _PyUnicode_KIND(op) \
137-
(assert(_PyUnicode_CHECK(op)), \
138-
_PyASCIIObject_CAST(op)->state.kind)
139-
#define _PyUnicode_GET_LENGTH(op) \
140-
(assert(_PyUnicode_CHECK(op)), \
141-
_PyASCIIObject_CAST(op)->length)
155+
156+
static inline Py_hash_t PyUnicode_HASH(PyObject *op) {
157+
assert(_PyUnicode_CHECK(op));
158+
return FT_ATOMIC_LOAD_SSIZE_RELAXED(_PyASCIIObject_CAST(op)->hash);
159+
}
160+
161+
static inline void PyUnicode_SET_HASH(PyObject *op, Py_hash_t hash) {
162+
FT_ATOMIC_STORE_SSIZE_RELAXED(_PyASCIIObject_CAST(op)->hash, hash);
163+
}
164+
142165
#define _PyUnicode_DATA_ANY(op) \
143166
(_PyUnicodeObject_CAST(op)->data.any)
144167

145-
#define _PyUnicode_SHARE_UTF8(op) \
146-
(assert(_PyUnicode_CHECK(op)), \
147-
assert(!PyUnicode_IS_COMPACT_ASCII(op)), \
148-
(_PyUnicode_UTF8(op) == PyUnicode_DATA(op)))
168+
static inline int _PyUnicode_SHARE_UTF8(PyObject *op) {
169+
assert(_PyUnicode_CHECK(op));
170+
assert(!PyUnicode_IS_COMPACT_ASCII(op));
171+
return (_PyUnicode_UTF8(op) == PyUnicode_DATA(op));
172+
}
149173

150174
/* true if the Unicode object has an allocated UTF-8 memory block
151175
(not shared with other data) */
152-
#define _PyUnicode_HAS_UTF8_MEMORY(op) \
153-
((!PyUnicode_IS_COMPACT_ASCII(op) \
154-
&& _PyUnicode_UTF8(op) \
155-
&& _PyUnicode_UTF8(op) != PyUnicode_DATA(op)))
176+
static inline int _PyUnicode_HAS_UTF8_MEMORY(PyObject *op) {
177+
return (!PyUnicode_IS_COMPACT_ASCII(op)
178+
&& _PyUnicode_UTF8(op) != NULL
179+
&& _PyUnicode_UTF8(op) != PyUnicode_DATA(op));
180+
}
181+
156182

157183
/* Generic helper macro to convert characters of different types.
158184
from_type and to_type have to be valid type names, begin and end
@@ -1123,8 +1149,8 @@ resize_compact(PyObject *unicode, Py_ssize_t length)
11231149

11241150
if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) {
11251151
PyMem_Free(_PyUnicode_UTF8(unicode));
1126-
_PyUnicode_UTF8(unicode) = NULL;
1127-
_PyUnicode_UTF8_LENGTH(unicode) = 0;
1152+
PyUnicode_SET_UTF8(unicode, NULL);
1153+
PyUnicode_SET_UTF8_LENGTH(unicode, 0);
11281154
}
11291155
#ifdef Py_TRACE_REFS
11301156
_Py_ForgetReference(unicode);
@@ -1177,8 +1203,8 @@ resize_inplace(PyObject *unicode, Py_ssize_t length)
11771203
if (!share_utf8 && _PyUnicode_HAS_UTF8_MEMORY(unicode))
11781204
{
11791205
PyMem_Free(_PyUnicode_UTF8(unicode));
1180-
_PyUnicode_UTF8(unicode) = NULL;
1181-
_PyUnicode_UTF8_LENGTH(unicode) = 0;
1206+
PyUnicode_SET_UTF8(unicode, NULL);
1207+
PyUnicode_SET_UTF8_LENGTH(unicode, 0);
11821208
}
11831209

11841210
data = (PyObject *)PyObject_Realloc(data, new_size);
@@ -1188,8 +1214,8 @@ resize_inplace(PyObject *unicode, Py_ssize_t length)
11881214
}
11891215
_PyUnicode_DATA_ANY(unicode) = data;
11901216
if (share_utf8) {
1191-
_PyUnicode_UTF8(unicode) = data;
1192-
_PyUnicode_UTF8_LENGTH(unicode) = length;
1217+
PyUnicode_SET_UTF8(unicode, data);
1218+
PyUnicode_SET_UTF8_LENGTH(unicode, length);
11931219
}
11941220
_PyUnicode_LENGTH(unicode) = length;
11951221
PyUnicode_WRITE(PyUnicode_KIND(unicode), data, length, 0);
@@ -1769,7 +1795,7 @@ unicode_modifiable(PyObject *unicode)
17691795
assert(_PyUnicode_CHECK(unicode));
17701796
if (Py_REFCNT(unicode) != 1)
17711797
return 0;
1772-
if (FT_ATOMIC_LOAD_SSIZE_RELAXED(_PyUnicode_HASH(unicode)) != -1)
1798+
if (PyUnicode_HASH(unicode) != -1)
17731799
return 0;
17741800
if (PyUnicode_CHECK_INTERNED(unicode))
17751801
return 0;
@@ -5862,8 +5888,8 @@ unicode_fill_utf8(PyObject *unicode)
58625888
PyErr_NoMemory();
58635889
return -1;
58645890
}
5865-
_PyUnicode_UTF8(unicode) = cache;
5866-
_PyUnicode_UTF8_LENGTH(unicode) = len;
5891+
PyUnicode_SET_UTF8(unicode, cache);
5892+
PyUnicode_SET_UTF8_LENGTH(unicode, len);
58675893
memcpy(cache, start, len);
58685894
cache[len] = '\0';
58695895
_PyBytesWriter_Dealloc(&writer);
@@ -11434,9 +11460,9 @@ _PyUnicode_EqualToASCIIId(PyObject *left, _Py_Identifier *right)
1143411460
return 0;
1143511461
}
1143611462

11437-
Py_hash_t right_hash = FT_ATOMIC_LOAD_SSIZE_RELAXED(_PyUnicode_HASH(right_uni));
11463+
Py_hash_t right_hash = PyUnicode_HASH(right_uni);
1143811464
assert(right_hash != -1);
11439-
Py_hash_t hash = FT_ATOMIC_LOAD_SSIZE_RELAXED(_PyUnicode_HASH(left));
11465+
Py_hash_t hash = PyUnicode_HASH(left);
1144011466
if (hash != -1 && hash != right_hash) {
1144111467
return 0;
1144211468
}
@@ -11916,14 +11942,14 @@ unicode_hash(PyObject *self)
1191611942
#ifdef Py_DEBUG
1191711943
assert(_Py_HashSecret_Initialized);
1191811944
#endif
11919-
Py_hash_t hash = FT_ATOMIC_LOAD_SSIZE_RELAXED(_PyUnicode_HASH(self));
11945+
Py_hash_t hash = PyUnicode_HASH(self);
1192011946
if (hash != -1) {
1192111947
return hash;
1192211948
}
1192311949
x = Py_HashBuffer(PyUnicode_DATA(self),
1192411950
PyUnicode_GET_LENGTH(self) * PyUnicode_KIND(self));
1192511951

11926-
FT_ATOMIC_STORE_SSIZE_RELAXED(_PyUnicode_HASH(self), x);
11952+
PyUnicode_SET_HASH(self, x);
1192711953
return x;
1192811954
}
1192911955

@@ -15427,8 +15453,8 @@ unicode_subtype_new(PyTypeObject *type, PyObject *unicode)
1542715453
_PyUnicode_STATE(self).compact = 0;
1542815454
_PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii;
1542915455
_PyUnicode_STATE(self).statically_allocated = 0;
15430-
_PyUnicode_UTF8_LENGTH(self) = 0;
15431-
_PyUnicode_UTF8(self) = NULL;
15456+
PyUnicode_SET_UTF8_LENGTH(self, 0);
15457+
PyUnicode_SET_UTF8(self, NULL);
1543215458
_PyUnicode_DATA_ANY(self) = NULL;
1543315459

1543415460
share_utf8 = 0;
@@ -15458,8 +15484,8 @@ unicode_subtype_new(PyTypeObject *type, PyObject *unicode)
1545815484

1545915485
_PyUnicode_DATA_ANY(self) = data;
1546015486
if (share_utf8) {
15461-
_PyUnicode_UTF8_LENGTH(self) = length;
15462-
_PyUnicode_UTF8(self) = data;
15487+
PyUnicode_SET_UTF8_LENGTH(self, length);
15488+
PyUnicode_SET_UTF8(self, data);
1546315489
}
1546415490

1546515491
memcpy(data, PyUnicode_DATA(unicode), kind * (length + 1));

0 commit comments

Comments
 (0)