diff --git a/cmdline/portable.h b/cmdline/portable.h index f0f92989..feb41e55 100644 --- a/cmdline/portable.h +++ b/cmdline/portable.h @@ -227,6 +227,13 @@ #include #endif +/** + * Enable thread use. + */ +#if HAVE_PTHREAD_H && HAVE_PTHREAD_CREATE +#define HAVE_PTHREAD 1 +#endif + /** * Disable case check in Windows. */ @@ -419,7 +426,7 @@ struct devinfo_struct { struct devinfo_struct* parent; /**< Pointer at the parent if any. */ uint64_t smart[SMART_COUNT]; /**< SMART raw attributes. */ char smart_serial[SMART_MAX]; /**< SMART serial number. */ -#if HAVE_PTHREAD_CREATE +#if HAVE_PTHREAD pthread_t thread; #endif tommy_node node; diff --git a/cmdline/snapraid.c b/cmdline/snapraid.c index 1c79091a..7ef48888 100644 --- a/cmdline/snapraid.c +++ b/cmdline/snapraid.c @@ -76,7 +76,7 @@ void usage(void) void memory(void) { - log_tag("memory:used:%" PRIu64 "\n", (uint64_t)malloc_counter()); + log_tag("memory:used:%" PRIu64 "\n", (uint64_t)malloc_counter_get()); /* size of the block */ log_tag("memory:block:%" PRIu64 "\n", (uint64_t)(sizeof(struct snapraid_block))); @@ -85,7 +85,7 @@ void memory(void) log_tag("memory:link:%" PRIu64 "\n", (uint64_t)(sizeof(struct snapraid_link))); log_tag("memory:dir:%" PRIu64 "\n", (uint64_t)(sizeof(struct snapraid_dir))); - msg_progress("Using %u MiB of memory.\n", (unsigned)(malloc_counter() / (1024 * 1024))); + msg_progress("Using %u MiB of memory.\n", (unsigned)(malloc_counter_get() / (1024 * 1024))); } /****************************************************************************/ diff --git a/cmdline/state.c b/cmdline/state.c index 2c2fe22c..eec04cf8 100644 --- a/cmdline/state.c +++ b/cmdline/state.c @@ -42,7 +42,7 @@ * Multi thread for verify is instead always generally faster, * so we enable it if possible. */ -#if HAVE_PTHREAD_CREATE +#if HAVE_PTHREAD /* #define HAVE_MT_WRITE 1 */ #define HAVE_MT_VERIFY 1 #endif diff --git a/cmdline/support.c b/cmdline/support.c index c5a17075..06d8edb2 100644 --- a/cmdline/support.c +++ b/cmdline/support.c @@ -607,9 +607,35 @@ int fmtime(int f, int64_t mtime_sec, int mtime_nsec) */ static size_t mcounter; -size_t malloc_counter(void) +/** + * Mutex protection for the global memory counter. + */ +#if HAVE_PTHREAD +pthread_mutex_t mcounter_mutex = PTHREAD_MUTEX_INITIALIZER; +#endif + +size_t malloc_counter_get(void) { - return mcounter; + size_t ret; +#if HAVE_PTHREAD + pthread_mutex_lock(&mcounter_mutex); +#endif + ret = mcounter; +#if HAVE_PTHREAD + pthread_mutex_unlock(&mcounter_mutex); +#endif + return ret; +} + +void malloc_counter_inc(size_t inc) +{ +#if HAVE_PTHREAD + pthread_mutex_lock(&mcounter_mutex); +#endif + mcounter += inc; +#if HAVE_PTHREAD + pthread_mutex_unlock(&mcounter_mutex); +#endif } /* LCOV_EXCL_START */ @@ -652,7 +678,7 @@ void malloc_fail(size_t size) malloc_printn(f, size); malloc_print(f, " bytes.\n"); malloc_print(f, "Already allocated "); - malloc_printn(f, malloc_counter()); + malloc_printn(f, malloc_counter_get()); malloc_print(f, " bytes.\n"); if (sizeof(void*) == 4) { malloc_print(f, "You are currently using a 32 bits executable.\n"); @@ -682,7 +708,7 @@ void* malloc_nofail(size_t size) memset(ptr, 0xA5, size); #endif - mcounter += size; + malloc_counter_inc(size); return ptr; } @@ -705,7 +731,7 @@ void* calloc_nofail(size_t count, size_t size) memset(ptr, 0, size); - mcounter += size; + malloc_counter_inc(size); return ptr; } @@ -728,7 +754,7 @@ char* strdup_nofail(const char* str) memcpy(ptr, str, size); - mcounter += size; + malloc_counter_inc(size); return ptr; } diff --git a/cmdline/support.h b/cmdline/support.h index f7b9ff20..bae27543 100644 --- a/cmdline/support.h +++ b/cmdline/support.h @@ -249,7 +249,7 @@ int fmtime(int f, int64_t mtime_sec, int mtime_nsec); /** * Return the size of the allocated memory. */ -size_t malloc_counter(void); +size_t malloc_counter_get(void); /** * Safe malloc. diff --git a/cmdline/unix.c b/cmdline/unix.c index 743a5170..b313a97b 100644 --- a/cmdline/unix.c +++ b/cmdline/unix.c @@ -951,7 +951,7 @@ static int device_thread(tommy_list* list, void* (*func)(void* arg)) int fail = 0; tommy_node* i; -#if HAVE_PTHREAD_CREATE +#if HAVE_PTHREAD /* start all threads */ for (i = tommy_list_head(list); i != 0; i = i->next) { devinfo_t* devinfo = i->data; diff --git a/valgrind.supp b/valgrind.supp index 014e3195..76817cc4 100644 --- a/valgrind.supp +++ b/valgrind.supp @@ -1,15 +1,3 @@ -{ - Global counter of allocated memory - Helgrind:Race - fun:malloc_nofail -} - -{ - Global counter of allocated memory - drd:ConflictingAccess - fun:malloc_nofail -} - { printf reports false positves when printing more than one variables at time exp-sgcheck:SorG