Skip to content

Commit 16ab4f9

Browse files
formatting
1 parent 258c0aa commit 16ab4f9

2 files changed

Lines changed: 34 additions & 15 deletions

File tree

Python/typecache.c

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,27 @@ static struct {
3030
// allocated for the type and the entry is inserted to the new cache.
3131
#define empty_cache (empty_cache_storage.cache)
3232

33-
static inline uint32_t cache_size(struct type_cache *cache)
33+
static inline uint32_t
34+
cache_size(struct type_cache *cache)
3435
{
3536
return cache->mask + 1;
3637
}
3738

38-
static inline size_t cache_nbytes(struct type_cache *cache)
39+
static inline size_t
40+
cache_nbytes(struct type_cache *cache)
3941
{
4042
return sizeof(struct type_cache)
4143
+ (size_t)cache_size(cache) * sizeof(struct type_cache_entry);
4244
}
4345

44-
static struct type_cache *cache_allocate(uint32_t size)
46+
static struct type_cache *
47+
cache_allocate(uint32_t size)
4548
{
4649
// size must be a power of two
4750
assert((size & (size - 1)) == 0);
48-
struct type_cache *cache = PyMem_Calloc(1, sizeof(struct type_cache) + size * sizeof(struct type_cache_entry));
51+
size_t nbytes = sizeof(struct type_cache)
52+
+ (size_t)size * sizeof(struct type_cache_entry);
53+
struct type_cache *cache = PyMem_Calloc(1, nbytes);
4954
if (cache == NULL) {
5055
return NULL;
5156
}
@@ -56,7 +61,8 @@ static struct type_cache *cache_allocate(uint32_t size)
5661
return cache;
5762
}
5863

59-
static void cache_free_delayed(struct type_cache *cache)
64+
static void
65+
cache_free_delayed(struct type_cache *cache)
6066
{
6167
if (cache == NULL || cache == &empty_cache) {
6268
return;
@@ -75,7 +81,8 @@ static void cache_free_delayed(struct type_cache *cache)
7581
}
7682

7783

78-
static inline void **cache_slot(PyTypeObject *type)
84+
static inline void **
85+
cache_slot(PyTypeObject *type)
7986
{
8087
if (type->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) {
8188
PyInterpreterState *interp = _PyInterpreterState_GET();
@@ -86,23 +93,27 @@ static inline void **cache_slot(PyTypeObject *type)
8693
return &type->_tp_cache;
8794
}
8895

89-
static inline struct type_cache *cache_get(PyTypeObject *type)
96+
static inline struct type_cache *
97+
cache_get(PyTypeObject *type)
9098
{
9199
return (struct type_cache *)FT_ATOMIC_LOAD_PTR(*cache_slot(type));
92100
}
93101

94-
static inline void cache_set(PyTypeObject *type, struct type_cache *cache)
102+
static inline void
103+
cache_set(PyTypeObject *type, struct type_cache *cache)
95104
{
96105
FT_ATOMIC_STORE_PTR(*cache_slot(type), cache);
97106
}
98107

99-
void _PyTypeCache_InitType(PyTypeObject *type)
108+
void
109+
_PyTypeCache_InitType(PyTypeObject *type)
100110
{
101111
*cache_slot(type) = &empty_cache;
102112
}
103113

104-
static inline void cache_insert(struct type_cache *cache, PyObject *name,
105-
PyObject *value)
114+
static inline void
115+
cache_insert(struct type_cache *cache, PyObject *name,
116+
PyObject *value)
106117
{
107118
Py_hash_t hash = PyUnstable_Unicode_GET_CACHED_HASH(name);
108119
assert(hash != -1);
@@ -127,7 +138,8 @@ static inline void cache_insert(struct type_cache *cache, PyObject *name,
127138
}
128139
}
129140

130-
static inline int cache_resize(PyTypeObject *type, struct type_cache *cache)
141+
static inline int
142+
cache_resize(PyTypeObject *type, struct type_cache *cache)
131143
{
132144
uint32_t old_size = cache_size(cache);
133145
uint32_t new_size;
@@ -155,7 +167,8 @@ static inline int cache_resize(PyTypeObject *type, struct type_cache *cache)
155167
return 0;
156168
}
157169

158-
void _PyTypeCache_Insert(PyTypeObject *type, PyObject *name, PyObject *value)
170+
void
171+
_PyTypeCache_Insert(PyTypeObject *type, PyObject *name, PyObject *value)
159172
{
160173
struct type_cache *cache = cache_get(type);
161174
// If the cache is full, resize it before inserting the new entry.
@@ -172,7 +185,8 @@ void _PyTypeCache_Insert(PyTypeObject *type, PyObject *name, PyObject *value)
172185
FT_ATOMIC_STORE_UINT_RELAXED(cache->version_tag, FT_ATOMIC_LOAD_UINT_RELAXED(type->tp_version_tag));
173186
}
174187

175-
struct _PyTypeCacheLookupResult _PyTypeCache_Lookup(PyTypeObject *type, PyObject *name)
188+
struct _PyTypeCacheLookupResult
189+
_PyTypeCache_Lookup(PyTypeObject *type, PyObject *name)
176190
{
177191
assert(PyUnicode_CheckExact(name) && PyUnicode_CHECK_INTERNED(name));
178192
struct _PyTypeCacheLookupResult miss = {PyStackRef_NULL, 0, 0};
@@ -212,7 +226,9 @@ struct _PyTypeCacheLookupResult _PyTypeCache_Lookup(PyTypeObject *type, PyObject
212226
}
213227

214228

215-
void _PyTypeCache_Invalidate(PyTypeObject *type) {
229+
void
230+
_PyTypeCache_Invalidate(PyTypeObject *type)
231+
{
216232
struct type_cache *cache = cache_get(type);
217233
// if the type was modified, the cache is set to the empty cache and the old cache is freed after a delay.
218234
cache_set(type, &empty_cache);

Tools/c-analyzer/cpython/ignored.tsv

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ Python/pyhash.c - _Py_HashSecret -
5757
## thread-safe hashtable (internal locks)
5858
Python/parking_lot.c - buckets -
5959

60+
## shared empty sentinel for the per-type method cache
61+
Python/typecache.c - empty_cache_storage -
62+
6063
## data needed for introspecting asyncio state from debuggers and profilers
6164
Modules/_asynciomodule.c - _Py_AsyncioDebug -
6265

0 commit comments

Comments
 (0)