@@ -99,6 +99,20 @@ indicate_hugepages(void *p, size_t size) {
99
99
}
100
100
101
101
102
+ /* Do not enable the alloc cache if the GIL is disabled, or if ASAN or MSAN
103
+ * instrumentation is enabled. The cache makes ASAN use-after-free or MSAN
104
+ * use-of-uninitialized-memory warnings less useful. */
105
+ #ifdef Py_GIL_DISABLED
106
+ #define USE_ALLOC_CACHE 0
107
+ #elif defined(__has_feature )
108
+ # if __has_feature (address_sanitizer )|| __has_feature (memory_sanitizer )
109
+ # define USE_ALLOC_CACHE 0
110
+ # endif
111
+ #else
112
+ #define USE_ALLOC_CACHE 1
113
+ #endif
114
+
115
+
102
116
/* as the cache is managed in global variables verify the GIL is held */
103
117
104
118
/*
@@ -115,7 +129,7 @@ _npy_alloc_cache(npy_uintp nelem, npy_uintp esz, npy_uint msz,
115
129
assert ((esz == 1 && cache == datacache )||
116
130
(esz == sizeof (npy_intp )&& cache == dimcache ));
117
131
assert (PyGILState_Check ());
118
- #ifndef Py_GIL_DISABLED
132
+ #if USE_ALLOC_CACHE
119
133
if (nelem < msz ) {
120
134
if (cache [nelem ].available > 0 ) {
121
135
return cache [nelem ].ptrs [-- (cache [nelem ].available )];
@@ -141,7 +155,7 @@ _npy_free_cache(void * p, npy_uintp nelem, npy_uint msz,
141
155
cache_bucket * cache ,void (* dealloc )(void * ))
142
156
{
143
157
assert (PyGILState_Check ());
144
- #ifndef Py_GIL_DISABLED
158
+ #if USE_ALLOC_CACHE
145
159
if (p != NULL && nelem < msz ) {
146
160
if (cache [nelem ].available < NCACHE ) {
147
161
cache [nelem ].ptrs [cache [nelem ].available ++ ]= p ;