Commit d76f439e7 for imagemagick.org
commit d76f439e7718e155a7680008ab16facc747e37d1
Author: Cristy <urban-warrior@imagemagick.org>
Date: Thu Dec 3 20:50:05 2026 -0500
GetMagickNumberThreads() now returns at least 1
diff --git a/MagickCore/thread-private.h b/MagickCore/thread-private.h
index f64ebf7c4..d7f787583 100644
--- a/MagickCore/thread-private.h
+++ b/MagickCore/thread-private.h
@@ -18,6 +18,8 @@
#ifndef MAGICKCORE_THREAD_PRIVATE_H
#define MAGICKCORE_THREAD_PRIVATE_H
+#include <limits.h>
+#include <string.h>
#include "MagickCore/cache.h"
#include "MagickCore/image-private.h"
#include "MagickCore/resource_.h"
@@ -29,14 +31,12 @@ extern "C" {
#define magick_number_threads(source,destination,chunk,factor) \
num_threads(GetMagickNumberThreads((source),(destination),(chunk),(factor)))
-#if defined(__clang__) || (__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ > 10))
+#if defined(__clang__) || (__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1))
#define MagickCachePrefetch(address,mode,locality) \
__builtin_prefetch(address,mode,locality)
#else
#define MagickCachePrefetch(address,mode,locality) \
- magick_unreferenced(address); \
- magick_unreferenced(mode); \
- magick_unreferenced(locality);
+ ((void) (address), (void) (mode), (void) (locality))
#endif
#if defined(MAGICKCORE_THREAD_SUPPORT)
@@ -59,16 +59,24 @@ static inline int GetMagickNumberThreads(const Image *source,
number_threads = 1UL,
pixels_per_thread = CastDoubleToSizeT(262144.0*factor);
+ if (max_threads == 0)
+ max_threads=1UL;
if (pixels_per_thread != 0)
{
- const size_t total_pixels = chunk*source->columns;
+ const size_t total_pixels = ((source->columns != 0) &&
+ (chunk > (~((size_t) 0))/source->columns)) ? ~((size_t) 0) :
+ chunk*source->columns; /* saturate instead of wrapping */
number_threads=(total_pixels < pixels_per_thread) ? 1UL :
- MagickMin(max_threads,(total_pixels+pixels_per_thread-1)/
- pixels_per_thread);
+ MagickMin(max_threads,total_pixels/pixels_per_thread+
+ ((total_pixels % pixels_per_thread) != 0 ? 1UL : 0UL));
}
if (((source_type != MemoryCache) && (source_type != MapCache)) ||
((destination_type != MemoryCache) && (destination_type != MapCache)))
number_threads=MagickMin(number_threads,4UL);
+ if (number_threads < 1UL)
+ number_threads=1UL; /* num_threads(0) is invalid in OpenMP */
+ if (number_threads > (size_t) INT_MAX)
+ number_threads=(size_t) INT_MAX;
return((int) number_threads);
}
@@ -101,7 +109,8 @@ static inline void GetMagickThreadFilename(const char *filename,
id=GetMagickThreadId();
(void) memcpy(bytes,&id,sizeof(id));
for (i=0; i < (ssize_t) sizeof(bytes); i++)
- (void) FormatLocaleString(thread_id+2*i,MagickPathExtent,"%02x",bytes[i]);
+ (void) FormatLocaleString(thread_id+2*i,sizeof(thread_id)-(size_t) (2*i),
+ "%02x",bytes[i]);
thread_id[sizeof(thread_id)-1]='\0';
(void) FormatLocaleString(thread_filename,MagickPathExtent,"%s|%s",thread_id,
filename);
@@ -167,28 +176,24 @@ static inline int GetOpenMPThreadId(void)
#endif
}
-#if defined(MAGICKCORE_OPENMP_SUPPORT)
static inline void SetOpenMPMaximumThreads(const int threads)
{
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
omp_set_num_threads(threads);
#else
-static inline void SetOpenMPMaximumThreads(const int magick_unused(threads))
-{
magick_unreferenced(threads);
#endif
}
-#if defined(MAGICKCORE_OPENMP_SUPPORT)
static inline void SetOpenMPMaxActiveLevels(const int value)
{
+#if defined(MAGICKCORE_OPENMP_SUPPORT)
#if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__MINGW32__)
omp_set_nested(value);
#else
omp_set_max_active_levels(value ? 2 : 1);
#endif
#else
-static inline void SetOpenMPMaxActiveLevels(const int magick_unused(value))
-{
magick_unreferenced(value);
#endif
}