1515#include "pycore_typeobject.h" // _PyStaticType_GetState()
1616
1717
18- // This is a union because MSVC doesn't support flexible array member in
19- // the middle of a struct and we use a char array to reserve space for the
20- // actual hashtable entries of the empty cache.
21- static union {
22- struct type_cache cache ;
23- char storage [sizeof (struct type_cache )
24- + _Py_TYPECACHE_MINSIZE * sizeof (struct type_cache_entry )];
25- } empty_cache_storage = {
26- .cache = {
27- .mask = _Py_TYPECACHE_MINSIZE - 1 ,
28- .available = 0 ,
29- .used = 0 ,
30- .version_tag = 0 ,
31- },
32- };
3318// The empty cache is statically allocated and shared across all the types,
3419// when a type is modified, the cache of type is set to the empty cache
3520// and when a cache entry is inserted to the empty cache, a new cache is
3621// allocated for the type and the entry is inserted to the new cache.
37- #define empty_cache (empty_cache_storage.cache)
22+ static struct type_cache empty_cache = {
23+ .mask = _Py_TYPECACHE_MINSIZE - 1 ,
24+ .version_tag = 0 ,
25+ .available = 0 ,
26+ .used = 0 ,
27+ };
3828
3929static inline uint32_t
4030cache_size (struct type_cache * cache )
@@ -45,7 +35,7 @@ cache_size(struct type_cache *cache)
4535static inline size_t
4636cache_nbytes (struct type_cache * cache )
4737{
48- return sizeof (struct type_cache )
38+ return offsetof (struct type_cache , hashtable )
4939 + (size_t )cache_size (cache ) * sizeof (struct type_cache_entry );
5040}
5141
@@ -54,7 +44,7 @@ cache_allocate(uint32_t size)
5444{
5545 // size must be a power of two
5646 assert ((size & (size - 1 )) == 0 );
57- size_t nbytes = sizeof (struct type_cache )
47+ size_t nbytes = offsetof (struct type_cache , hashtable )
5848 + (size_t )size * sizeof (struct type_cache_entry );
5949 struct type_cache * cache = PyMem_Calloc (1 , nbytes );
6050 if (cache == NULL ) {
@@ -158,7 +148,7 @@ cache_resize(PyTypeObject *type, struct type_cache *cache)
158148 new_size = old_size * 2 ;
159149 }
160150 if (new_size > _Py_TYPECACHE_MAXSIZE ) {
161- // The cache is too big, don't resize and just return.
151+ // The new size is too big, don't resize and just return.
162152 return -1 ;
163153 }
164154 struct type_cache * new_cache = cache_allocate (new_size );
@@ -176,7 +166,7 @@ cache_resize(PyTypeObject *type, struct type_cache *cache)
176166 return 0 ;
177167}
178168
179- // Insert a new entry to the type cache. If the cache is full, resize it before inserting the new entry.
169+ // Insert a new entry to the type cache.
180170// The TYPE_LOCK should be held while calling this function.
181171void
182172_PyTypeCache_Insert (PyTypeObject * type , PyObject * name , PyObject * value )
0 commit comments