fix tracking mem properties under multi-threads

This commit is contained in:
Hideki Miyazaki
2024-04-18 15:14:25 +09:00
parent 1f61ed3536
commit dab6726e58

View File

@@ -177,6 +177,10 @@ static WC_INLINE void* TrackMalloc(size_t sz)
(void)line; (void)line;
#endif #endif
#endif #endif
#if defined(DO_MEM_LIST) || defined(DO_MEM_STATS)
if (pthread_mutex_lock(&memLock) == 0)
{
#endif
#ifdef DO_MEM_STATS #ifdef DO_MEM_STATS
ourMemStats.totalAllocs++; ourMemStats.totalAllocs++;
@@ -197,10 +201,10 @@ static WC_INLINE void* TrackMalloc(size_t sz)
if (ourMemStats.currentBytes > ourMemStats.peakBytes) if (ourMemStats.currentBytes > ourMemStats.peakBytes)
ourMemStats.peakBytes = ourMemStats.currentBytes; ourMemStats.peakBytes = ourMemStats.currentBytes;
} }
#endif /* DO_MEM_STATS */ #endif /* DO_MEM_STATS */
#ifdef DO_MEM_LIST #ifdef DO_MEM_LIST
if (pthread_mutex_lock(&memLock) == 0) {
#ifdef WOLFSSL_DEBUG_MEMORY #ifdef WOLFSSL_DEBUG_MEMORY
header->func = func; header->func = func;
header->line = line; header->line = line;
@@ -218,7 +222,8 @@ static WC_INLINE void* TrackMalloc(size_t sz)
} }
ourMemList.tail = header; /* add to the end either way */ ourMemList.tail = header; /* add to the end either way */
ourMemList.count++; ourMemList.count++;
#endif
#if defined(DO_MEM_LIST) || defined(DO_MEM_STATS)
pthread_mutex_unlock(&memLock); pthread_mutex_unlock(&memLock);
} }
#endif /* DO_MEM_LIST */ #endif /* DO_MEM_LIST */
@@ -245,7 +250,7 @@ static WC_INLINE void TrackFree(void* ptr)
header = &mt->u.hint; header = &mt->u.hint;
sz = header->thisSize; sz = header->thisSize;
#ifdef DO_MEM_LIST #if defined(DO_MEM_LIST) || defined(DO_MEM_STATS)
if (pthread_mutex_lock(&memLock) == 0) if (pthread_mutex_lock(&memLock) == 0)
{ {
#endif #endif
@@ -277,7 +282,9 @@ static WC_INLINE void TrackFree(void* ptr)
prev->next = next; prev->next = next;
} }
ourMemList.count--; ourMemList.count--;
#endif
#if defined(DO_MEM_LIST) || defined(DO_MEM_STATS)
pthread_mutex_unlock(&memLock); pthread_mutex_unlock(&memLock);
} }
#endif #endif