From 66f54fe5a4bfac703831fd2d7602f014c702c13c Mon Sep 17 00:00:00 2001 From: Jonathan Watt Date: Fri, 28 Oct 2011 19:33:28 +0100 Subject: [PATCH] Bug 695303 - Add a mozilla::clamped function to replace NS_CLAMP (so side affects of args are evaluated no more than once) and NS_MIN(max, NS_MAX(val, min)) (to make code clearer). r=bsmedberg. --- content/canvas/src/WebGLContextGL.cpp | 8 ++--- content/svg/content/src/nsSVGFilters.cpp | 4 +-- dom/workers/RuntimeService.cpp | 2 +- gfx/thebes/gfxGDIFontList.cpp | 2 +- gfx/thebes/gfxQuaternion.h | 7 +++-- gfx/ycbcr/ycbcr_to_rgb565.cpp | 11 +++---- layout/generic/nsColumnSetFrame.cpp | 6 ++-- layout/generic/nsGfxScrollFrame.cpp | 2 +- layout/generic/nsObjectFrame.cpp | 12 ++++---- layout/generic/nsSimplePageSequence.cpp | 8 ++--- layout/style/nsComputedDOMStyle.h | 4 +-- layout/tables/nsTableFrame.cpp | 4 +-- layout/xul/base/src/nsSliderFrame.cpp | 4 +-- layout/xul/base/src/nsStackLayout.cpp | 6 ++-- netwerk/cache/nsDiskCacheBlockFile.cpp | 4 ++- netwerk/protocol/ftp/nsFtpProtocolHandler.cpp | 10 ++++--- netwerk/protocol/http/nsHttpChannel.cpp | 5 +++- netwerk/protocol/http/nsHttpHandler.cpp | 30 ++++++++++--------- .../protocol/websocket/WebSocketChannel.cpp | 12 ++++---- widget/src/android/nsWindow.cpp | 4 +-- xpcom/base/nscore.h | 5 ---- xpcom/string/public/nsAlgorithm.h | 13 ++++++++ 22 files changed, 93 insertions(+), 70 deletions(-) diff --git a/content/canvas/src/WebGLContextGL.cpp b/content/canvas/src/WebGLContextGL.cpp index 169d5c17b2f7..fda015fa2058 100644 --- a/content/canvas/src/WebGLContextGL.cpp +++ b/content/canvas/src/WebGLContextGL.cpp @@ -879,13 +879,13 @@ WebGLContext::CopyTexSubImage2D_base(WebGLenum target, return NS_OK; } - GLint actual_x = NS_MIN(framebufferWidth, NS_MAX(0, x)); - GLint actual_x_plus_width = NS_MIN(framebufferWidth, NS_MAX(0, x + width)); + GLint actual_x = clamped(x, 0, framebufferWidth); + GLint actual_x_plus_width = clamped(x + width, 0, framebufferWidth); GLsizei actual_width = actual_x_plus_width - actual_x; GLint actual_xoffset = xoffset + actual_x - x; - GLint actual_y = NS_MIN(framebufferHeight, NS_MAX(0, y)); - GLint actual_y_plus_height = NS_MIN(framebufferHeight, NS_MAX(0, y + height)); + GLint actual_y = clamped(y, 0, framebufferHeight); + GLint actual_y_plus_height = clamped(y + height, 0, framebufferHeight); GLsizei actual_height = actual_y_plus_height - actual_y; GLint actual_yoffset = yoffset + actual_y - y; diff --git a/content/svg/content/src/nsSVGFilters.cpp b/content/svg/content/src/nsSVGFilters.cpp index 2a111a69ee63..9fcf3ebf6c9e 100644 --- a/content/svg/content/src/nsSVGFilters.cpp +++ b/content/svg/content/src/nsSVGFilters.cpp @@ -1323,7 +1323,7 @@ nsSVGFEColorMatrixElement::Filter(nsSVGFilterInstance *instance, sourceData[targIndex + GFX_ARGB32_OFFSET_B] * colorMatrix[row + 2] + sourceData[targIndex + GFX_ARGB32_OFFSET_A] * colorMatrix[row + 3] + 255 * colorMatrix[row + 4]; - col[i] = NS_MIN(NS_MAX(0.f, col[i]), 255.f); + col[i] = clamped(col[i], 0.f, 255.f); } targetData[targIndex + GFX_ARGB32_OFFSET_R] = static_cast(col[0]); @@ -1565,7 +1565,7 @@ nsSVGFECompositeElement::Filter(nsSVGFilterInstance *instance, PRUint8 i2 = sourceData[targIndex + i]; float result = k1Scaled*i1*i2 + k2*i1 + k3*i2 + k4Scaled; targetData[targIndex + i] = - static_cast(NS_MIN(NS_MAX(0.f, result), 255.f)); + static_cast(clamped(result, 0.f, 255.f)); } } } diff --git a/dom/workers/RuntimeService.cpp b/dom/workers/RuntimeService.cpp index 9c4c34022f63..82e6069d658c 100644 --- a/dom/workers/RuntimeService.cpp +++ b/dom/workers/RuntimeService.cpp @@ -219,7 +219,7 @@ PrefCallback(const char* aPrefName, void* aClosure) #ifdef JS_GC_ZEAL else if (!strcmp(aPrefName, gPrefsToWatch[PREF_gczeal])) { PRInt32 gczeal = Preferences::GetInt(gPrefsToWatch[PREF_gczeal]); - RuntimeService::SetDefaultGCZeal(PRUint8(NS_MIN(NS_MAX(gczeal, 0), 3))); + RuntimeService::SetDefaultGCZeal(PRUint8(clamped(gczeal, 0, 3))); rts->UpdateAllWorkerGCZeal(); } #endif diff --git a/gfx/thebes/gfxGDIFontList.cpp b/gfx/thebes/gfxGDIFontList.cpp index e8063af229d0..18d6f72fecdf 100644 --- a/gfx/thebes/gfxGDIFontList.cpp +++ b/gfx/thebes/gfxGDIFontList.cpp @@ -478,7 +478,7 @@ GDIFontFamily::FamilyAddStylesProc(const ENUMLOGFONTEXW *lpelfe, GDIFontFamily *ff = reinterpret_cast(data); // Some fonts claim to support things > 900, but we don't so clamp the sizes - logFont.lfWeight = NS_MAX(NS_MIN(logFont.lfWeight, 900), 100); + logFont.lfWeight = clamped(logFont.lfWeight, LONG(100), LONG(900)); gfxWindowsFontType feType = GDIFontEntry::DetermineFontType(metrics, fontType); diff --git a/gfx/thebes/gfxQuaternion.h b/gfx/thebes/gfxQuaternion.h index d77fdb2e71de..8c5eb29023d2 100644 --- a/gfx/thebes/gfxQuaternion.h +++ b/gfx/thebes/gfxQuaternion.h @@ -40,6 +40,7 @@ #include "mozilla/gfx/BasePoint4D.h" #include "gfx3DMatrix.h" +#include "nsAlgorithm.h" struct THEBES_API gfxQuaternion : public mozilla::gfx::BasePoint4D { typedef mozilla::gfx::BasePoint4D Super; @@ -62,7 +63,7 @@ struct THEBES_API gfxQuaternion : public mozilla::gfx::BasePoint4D #include "nsDebug.h" #include "ycbcr_to_rgb565.h" +#include "nsAlgorithm.h" @@ -151,9 +152,9 @@ static PRUint16 yu2rgb565(int y, int u, int v, int dither) { int r; int g; int b; - r = NS_CLAMP((74*y+102*v+DITHER_BIAS[dither][0])>>9, 0, 31); - g = NS_CLAMP((74*y-25*u-52*v+DITHER_BIAS[dither][1])>>8, 0, 63); - b = NS_CLAMP((74*y+129*u+DITHER_BIAS[dither][2])>>9, 0, 31); + r = clamped((74*y+102*v+DITHER_BIAS[dither][0])>>9, 0, 31); + g = clamped((74*y-25*u-52*v+DITHER_BIAS[dither][1])>>8, 0, 63); + b = clamped((74*y+129*u+DITHER_BIAS[dither][2])>>9, 0, 31); return (PRUint16)(r<<11 | g<<5 | b); } @@ -458,10 +459,10 @@ NS_GFX_(void) ScaleYCbCrToRGB565(const PRUint8 *y_buf, int source_y; ctx.rgb_row = (PRUint16 *)(rgb_buf + y*rgb_pitch); source_y = source_y0_q16>>16; - source_y = NS_CLAMP(source_y, ymin, ymax); + source_y = clamped(source_y, ymin, ymax); ctx.y_row = y_buf + source_y*y_pitch; source_y = (source_y0_q16+source_uv_yoffs_q16)>>(16+y_shift); - source_y = NS_CLAMP(source_y, uvmin, uvmax); + source_y = clamped(source_y, uvmin, uvmax); source_y0_q16 += source_dy_q16; ctx.u_row = u_buf + source_y*uv_pitch; ctx.v_row = v_buf + source_y*uv_pitch; diff --git a/layout/generic/nsColumnSetFrame.cpp b/layout/generic/nsColumnSetFrame.cpp index 9d100363cf3c..ecf95042fc61 100644 --- a/layout/generic/nsColumnSetFrame.cpp +++ b/layout/generic/nsColumnSetFrame.cpp @@ -52,6 +52,8 @@ #include "nsDisplayList.h" #include "nsCSSRendering.h" +using namespace mozilla; + class nsColumnSetFrame : public nsHTMLContainerFrame { public: NS_DECL_FRAMEARENA_HELPERS @@ -1019,8 +1021,8 @@ nsColumnSetFrame::Reflow(nsPresContext* aPresContext, // 600 twips is arbitrary. It's about two line-heights. nextGuess = colData.mSumHeight/config.mBalanceColCount + 600; // Sanitize it - nextGuess = NS_MIN(NS_MAX(nextGuess, knownInfeasibleHeight + 1), - knownFeasibleHeight - 1); + nextGuess = clamped(nextGuess, knownInfeasibleHeight + 1, + knownFeasibleHeight - 1); } else if (knownFeasibleHeight == NS_INTRINSICSIZE) { // This can happen when we had a next-in-flow so we didn't // want to do an unbounded height measuring step. Let's just increase diff --git a/layout/generic/nsGfxScrollFrame.cpp b/layout/generic/nsGfxScrollFrame.cpp index 483317193d7e..49fd06e5a6f6 100644 --- a/layout/generic/nsGfxScrollFrame.cpp +++ b/layout/generic/nsGfxScrollFrame.cpp @@ -1326,7 +1326,7 @@ public: protected: double ProgressAt(TimeStamp aTime) { - return NS_MIN(1.0, NS_MAX(0.0, (aTime - mStartTime) / mDuration)); + return clamped((aTime - mStartTime) / mDuration, 0.0, 1.0); } nscoord VelocityComponent(double aTimeProgress, diff --git a/layout/generic/nsObjectFrame.cpp b/layout/generic/nsObjectFrame.cpp index fa9e835d85a2..35b1f38b1bba 100644 --- a/layout/generic/nsObjectFrame.cpp +++ b/layout/generic/nsObjectFrame.cpp @@ -582,14 +582,14 @@ nsObjectFrame::GetDesiredSize(nsPresContext* aPresContext, nsIAtom *atom = mContent->Tag(); if (atom == nsGkAtoms::applet || atom == nsGkAtoms::embed) { if (aMetrics.width == NS_UNCONSTRAINEDSIZE) { - aMetrics.width = NS_MIN(NS_MAX(nsPresContext::CSSPixelsToAppUnits(EMBED_DEF_WIDTH), - aReflowState.mComputedMinWidth), - aReflowState.mComputedMaxWidth); + aMetrics.width = clamped(nsPresContext::CSSPixelsToAppUnits(EMBED_DEF_WIDTH), + aReflowState.mComputedMinWidth, + aReflowState.mComputedMaxWidth); } if (aMetrics.height == NS_UNCONSTRAINEDSIZE) { - aMetrics.height = NS_MIN(NS_MAX(nsPresContext::CSSPixelsToAppUnits(EMBED_DEF_HEIGHT), - aReflowState.mComputedMinHeight), - aReflowState.mComputedMaxHeight); + aMetrics.height = clamped(nsPresContext::CSSPixelsToAppUnits(EMBED_DEF_HEIGHT), + aReflowState.mComputedMinHeight, + aReflowState.mComputedMaxHeight); } #if defined (MOZ_WIDGET_GTK2) diff --git a/layout/generic/nsSimplePageSequence.cpp b/layout/generic/nsSimplePageSequence.cpp index 8ea2ebc8c9b3..564045514d3a 100644 --- a/layout/generic/nsSimplePageSequence.cpp +++ b/layout/generic/nsSimplePageSequence.cpp @@ -212,10 +212,10 @@ nsSimplePageSequenceFrame::Reflow(nsPresContext* aPresContext, // sanity check the values. three inches are sometimes needed PRInt32 inchInTwips = NS_INCHES_TO_INT_TWIPS(3.0); - edgeTwips.top = NS_MIN(NS_MAX(edgeTwips.top, 0), inchInTwips); - edgeTwips.bottom = NS_MIN(NS_MAX(edgeTwips.bottom, 0), inchInTwips); - edgeTwips.left = NS_MIN(NS_MAX(edgeTwips.left, 0), inchInTwips); - edgeTwips.right = NS_MIN(NS_MAX(edgeTwips.right, 0), inchInTwips); + edgeTwips.top = clamped(edgeTwips.top, 0, inchInTwips); + edgeTwips.bottom = clamped(edgeTwips.bottom, 0, inchInTwips); + edgeTwips.left = clamped(edgeTwips.left, 0, inchInTwips); + edgeTwips.right = clamped(edgeTwips.right, 0, inchInTwips); mPageData->mEdgePaperMargin = aPresContext->CSSTwipsToAppUnits(edgeTwips + unwriteableTwips); diff --git a/layout/style/nsComputedDOMStyle.h b/layout/style/nsComputedDOMStyle.h index e730a0ff650f..37e95178631b 100644 --- a/layout/style/nsComputedDOMStyle.h +++ b/layout/style/nsComputedDOMStyle.h @@ -444,8 +444,8 @@ private: * the percent value of aCoord is set as a percent value on aValue. aTable, * if not null, is the keyword table to handle eStyleUnit_Enumerated. When * calling SetAppUnits on aValue (for coord or percent values), the value - * passed in will be NS_MAX of the value in aMinAppUnits and the NS_MIN of - * the actual value in aCoord and the value in aMaxAppUnits. + * passed in will be clamped to be no less than aMinAppUnits and no more than + * aMaxAppUnits. * * XXXbz should caller pass in some sort of bitfield indicating which units * can be expected or something? diff --git a/layout/tables/nsTableFrame.cpp b/layout/tables/nsTableFrame.cpp index 3fefd50dcad6..a0451a60f4b5 100644 --- a/layout/tables/nsTableFrame.cpp +++ b/layout/tables/nsTableFrame.cpp @@ -4857,7 +4857,7 @@ struct BCCorners BCCornerInfo& operator [](PRInt32 i) const { NS_ASSERTION((i >= startIndex) && (i <= endIndex), "program error"); - return corners[NS_MAX(NS_MIN(i, endIndex), startIndex) - startIndex]; } + return corners[clamped(i, startIndex, endIndex) - startIndex]; } PRInt32 startIndex; PRInt32 endIndex; @@ -4883,7 +4883,7 @@ struct BCCellBorders BCCellBorder& operator [](PRInt32 i) const { NS_ASSERTION((i >= startIndex) && (i <= endIndex), "program error"); - return borders[NS_MAX(NS_MIN(i, endIndex), startIndex) - startIndex]; } + return borders[clamped(i, startIndex, endIndex) - startIndex]; } PRInt32 startIndex; PRInt32 endIndex; diff --git a/layout/xul/base/src/nsSliderFrame.cpp b/layout/xul/base/src/nsSliderFrame.cpp index 0f16d4926fb1..99e98b5e66c0 100644 --- a/layout/xul/base/src/nsSliderFrame.cpp +++ b/layout/xul/base/src/nsSliderFrame.cpp @@ -406,7 +406,7 @@ nsSliderFrame::DoLayout(nsBoxLayoutState& aState) PRInt32 pageIncrement = GetPageIncrement(scrollbar); maxPos = NS_MAX(minPos, maxPos); - curPos = NS_MAX(minPos, NS_MIN(curPos, maxPos)); + curPos = clamped(curPos, minPos, maxPos); nscoord& availableLength = IsHorizontal() ? clientRect.width : clientRect.height; nscoord& thumbLength = IsHorizontal() ? thumbSize.width : thumbSize.height; @@ -687,7 +687,7 @@ nsSliderFrame::CurrentPositionChanged(nsPresContext* aPresContext, PRInt32 maxPos = GetMaxPosition(scrollbar); maxPos = NS_MAX(minPos, maxPos); - curPos = NS_MAX(minPos, NS_MIN(curPos, maxPos)); + curPos = clamped(curPos, minPos, maxPos); // get the thumb's rect nsIFrame* thumbFrame = mFrames.FirstChild(); diff --git a/layout/xul/base/src/nsStackLayout.cpp b/layout/xul/base/src/nsStackLayout.cpp index 7bffb8b78b32..c44b81739289 100644 --- a/layout/xul/base/src/nsStackLayout.cpp +++ b/layout/xul/base/src/nsStackLayout.cpp @@ -52,6 +52,8 @@ #include "nsIContent.h" #include "nsINameSpaceManager.h" +using namespace mozilla; + nsBoxLayout* nsStackLayout::gInstance = nsnull; #define SPECIFIED_LEFT (1 << NS_SIDE_LEFT) @@ -335,7 +337,7 @@ nsStackLayout::Layout(nsIBox* aBox, nsBoxLayoutState& aState) nsSize min = child->GetMinSize(aState); nsSize max = child->GetMaxSize(aState); nscoord width = clientRect.width - offset.LeftRight() - margin.LeftRight(); - childRect.width = NS_MAX(min.width, NS_MIN(max.width, width)); + childRect.width = clamped(width, min.width, max.width); } else { childRect.width = child->GetPrefSize(aState).width; @@ -352,7 +354,7 @@ nsStackLayout::Layout(nsIBox* aBox, nsBoxLayoutState& aState) nsSize min = child->GetMinSize(aState); nsSize max = child->GetMaxSize(aState); nscoord height = clientRect.height - offset.TopBottom() - margin.TopBottom(); - childRect.height = NS_MAX(min.height, NS_MIN(max.height, height)); + childRect.height = clamped(height, min.height, max.height); } else { childRect.height = child->GetPrefSize(aState).height; diff --git a/netwerk/cache/nsDiskCacheBlockFile.cpp b/netwerk/cache/nsDiskCacheBlockFile.cpp index 0dc15570e791..8c22555a414f 100644 --- a/netwerk/cache/nsDiskCacheBlockFile.cpp +++ b/netwerk/cache/nsDiskCacheBlockFile.cpp @@ -42,6 +42,8 @@ #include "nsDiskCacheBlockFile.h" #include "mozilla/FileUtils.h" +using namespace mozilla; + /****************************************************************************** * nsDiskCacheBlockFile - *****************************************************************************/ @@ -393,7 +395,7 @@ nsDiskCacheBlockFile::Write(PRInt32 offset, const void *buf, PRInt32 amount) if (mFileSize) while(mFileSize < upTo) mFileSize *= 2; - mFileSize = NS_MIN(maxPreallocate, NS_MAX(mFileSize, minPreallocate)); + mFileSize = clamped(mFileSize, minPreallocate, maxPreallocate); } mFileSize = NS_MIN(mFileSize, maxFileSize); // Appears to cause bug 617123? Disabled for now. diff --git a/netwerk/protocol/ftp/nsFtpProtocolHandler.cpp b/netwerk/protocol/ftp/nsFtpProtocolHandler.cpp index 004041ba7aed..d2b9ba599e5f 100644 --- a/netwerk/protocol/ftp/nsFtpProtocolHandler.cpp +++ b/netwerk/protocol/ftp/nsFtpProtocolHandler.cpp @@ -51,6 +51,7 @@ #include "mozilla/net/NeckoChild.h" #include "mozilla/net/FTPChannelChild.h" +using namespace mozilla; using namespace mozilla::net; #include "nsFtpProtocolHandler.h" @@ -68,6 +69,7 @@ using namespace mozilla::net; #include "nsIPrefBranch2.h" #include "nsIObserverService.h" #include "nsEscape.h" +#include "nsAlgorithm.h" //----------------------------------------------------------------------------- @@ -151,14 +153,14 @@ nsFtpProtocolHandler::Init() PRInt32 val; rv = branch->GetIntPref(QOS_DATA_PREF, &val); if (NS_SUCCEEDED(rv)) - mDataQoSBits = (PRUint8) NS_CLAMP(val, 0, 0xff); + mDataQoSBits = (PRUint8) clamped(val, 0, 0xff); rv = branch->AddObserver(QOS_DATA_PREF, this, true); if (NS_FAILED(rv)) return rv; rv = branch->GetIntPref(QOS_CONTROL_PREF, &val); if (NS_SUCCEEDED(rv)) - mControlQoSBits = (PRUint8) NS_CLAMP(val, 0, 0xff); + mControlQoSBits = (PRUint8) clamped(val, 0, 0xff); rv = branch->AddObserver(QOS_CONTROL_PREF, this, true); if (NS_FAILED(rv)) return rv; @@ -415,11 +417,11 @@ nsFtpProtocolHandler::Observe(nsISupports *aSubject, rv = branch->GetIntPref(QOS_DATA_PREF, &val); if (NS_SUCCEEDED(rv)) - mDataQoSBits = (PRUint8) NS_CLAMP(val, 0, 0xff); + mDataQoSBits = (PRUint8) clamped(val, 0, 0xff); rv = branch->GetIntPref(QOS_CONTROL_PREF, &val); if (NS_SUCCEEDED(rv)) - mControlQoSBits = (PRUint8) NS_CLAMP(val, 0, 0xff); + mControlQoSBits = (PRUint8) clamped(val, 0, 0xff); } else if (!strcmp(aTopic, "network:offline-about-to-go-offline")) { ClearAllConnections(); } else if (!strcmp(aTopic, "net:clear-active-logins")) { diff --git a/netwerk/protocol/http/nsHttpChannel.cpp b/netwerk/protocol/http/nsHttpChannel.cpp index 30c91a468f4a..0da95bedf739 100644 --- a/netwerk/protocol/http/nsHttpChannel.cpp +++ b/netwerk/protocol/http/nsHttpChannel.cpp @@ -68,6 +68,9 @@ #include "mozilla/TimeStamp.h" #include "mozilla/Telemetry.h" #include "nsDOMError.h" +#include "nsAlgorithm.h" + +using namespace mozilla; // Device IDs for various cache types const char kDiskDeviceID[] = "disk"; @@ -3791,7 +3794,7 @@ nsHttpChannel::SetupFallbackChannel(const char *aFallbackKey) NS_IMETHODIMP nsHttpChannel::SetPriority(PRInt32 value) { - PRInt16 newValue = NS_CLAMP(value, PR_INT16_MIN, PR_INT16_MAX); + PRInt16 newValue = clamped(value, PR_INT16_MIN, PR_INT16_MAX); if (mPriority == newValue) return NS_OK; mPriority = newValue; diff --git a/netwerk/protocol/http/nsHttpHandler.cpp b/netwerk/protocol/http/nsHttpHandler.cpp index 2516e31fe629..f21598e03140 100644 --- a/netwerk/protocol/http/nsHttpHandler.cpp +++ b/netwerk/protocol/http/nsHttpHandler.cpp @@ -76,6 +76,7 @@ #include "nsIOService.h" #include "nsAsyncRedirectVerifyHelper.h" #include "nsSocketTransportService2.h" +#include "nsAlgorithm.h" #include "nsIXULAppInfo.h" @@ -99,6 +100,7 @@ #endif //----------------------------------------------------------------------------- +using namespace mozilla; using namespace mozilla::net; #include "mozilla/net/HttpChannelChild.h" @@ -833,19 +835,19 @@ nsHttpHandler::PrefsChanged(nsIPrefBranch *prefs, const char *pref) if (PREF_CHANGED(HTTP_PREF("keep-alive.timeout"))) { rv = prefs->GetIntPref(HTTP_PREF("keep-alive.timeout"), &val); if (NS_SUCCEEDED(rv)) - mIdleTimeout = (PRUint16) NS_CLAMP(val, 1, 0xffff); + mIdleTimeout = (PRUint16) clamped(val, 1, 0xffff); } if (PREF_CHANGED(HTTP_PREF("request.max-attempts"))) { rv = prefs->GetIntPref(HTTP_PREF("request.max-attempts"), &val); if (NS_SUCCEEDED(rv)) - mMaxRequestAttempts = (PRUint16) NS_CLAMP(val, 1, 0xffff); + mMaxRequestAttempts = (PRUint16) clamped(val, 1, 0xffff); } if (PREF_CHANGED(HTTP_PREF("request.max-start-delay"))) { rv = prefs->GetIntPref(HTTP_PREF("request.max-start-delay"), &val); if (NS_SUCCEEDED(rv)) { - mMaxRequestDelay = (PRUint16) NS_CLAMP(val, 0, 0xffff); + mMaxRequestDelay = (PRUint16) clamped(val, 0, 0xffff); if (mConnMgr) mConnMgr->UpdateParam(nsHttpConnectionMgr::MAX_REQUEST_DELAY, mMaxRequestDelay); @@ -856,8 +858,8 @@ nsHttpHandler::PrefsChanged(nsIPrefBranch *prefs, const char *pref) rv = prefs->GetIntPref(HTTP_PREF("max-connections"), &val); if (NS_SUCCEEDED(rv)) { - mMaxConnections = (PRUint16) NS_CLAMP((PRUint32)val, - 1, MaxSocketCount()); + mMaxConnections = (PRUint16) clamped((PRUint32)val, + (PRUint32)1, MaxSocketCount()); if (mConnMgr) mConnMgr->UpdateParam(nsHttpConnectionMgr::MAX_CONNECTIONS, @@ -868,7 +870,7 @@ nsHttpHandler::PrefsChanged(nsIPrefBranch *prefs, const char *pref) if (PREF_CHANGED(HTTP_PREF("max-connections-per-server"))) { rv = prefs->GetIntPref(HTTP_PREF("max-connections-per-server"), &val); if (NS_SUCCEEDED(rv)) { - mMaxConnectionsPerServer = (PRUint8) NS_CLAMP(val, 1, 0xff); + mMaxConnectionsPerServer = (PRUint8) clamped(val, 1, 0xff); if (mConnMgr) { mConnMgr->UpdateParam(nsHttpConnectionMgr::MAX_CONNECTIONS_PER_HOST, mMaxConnectionsPerServer); @@ -881,7 +883,7 @@ nsHttpHandler::PrefsChanged(nsIPrefBranch *prefs, const char *pref) if (PREF_CHANGED(HTTP_PREF("max-persistent-connections-per-server"))) { rv = prefs->GetIntPref(HTTP_PREF("max-persistent-connections-per-server"), &val); if (NS_SUCCEEDED(rv)) { - mMaxPersistentConnectionsPerServer = (PRUint8) NS_CLAMP(val, 1, 0xff); + mMaxPersistentConnectionsPerServer = (PRUint8) clamped(val, 1, 0xff); if (mConnMgr) mConnMgr->UpdateParam(nsHttpConnectionMgr::MAX_PERSISTENT_CONNECTIONS_PER_HOST, mMaxPersistentConnectionsPerServer); @@ -891,7 +893,7 @@ nsHttpHandler::PrefsChanged(nsIPrefBranch *prefs, const char *pref) if (PREF_CHANGED(HTTP_PREF("max-persistent-connections-per-proxy"))) { rv = prefs->GetIntPref(HTTP_PREF("max-persistent-connections-per-proxy"), &val); if (NS_SUCCEEDED(rv)) { - mMaxPersistentConnectionsPerProxy = (PRUint8) NS_CLAMP(val, 1, 0xff); + mMaxPersistentConnectionsPerProxy = (PRUint8) clamped(val, 1, 0xff); if (mConnMgr) mConnMgr->UpdateParam(nsHttpConnectionMgr::MAX_PERSISTENT_CONNECTIONS_PER_PROXY, mMaxPersistentConnectionsPerProxy); @@ -901,19 +903,19 @@ nsHttpHandler::PrefsChanged(nsIPrefBranch *prefs, const char *pref) if (PREF_CHANGED(HTTP_PREF("sendRefererHeader"))) { rv = prefs->GetIntPref(HTTP_PREF("sendRefererHeader"), &val); if (NS_SUCCEEDED(rv)) - mReferrerLevel = (PRUint8) NS_CLAMP(val, 0, 0xff); + mReferrerLevel = (PRUint8) clamped(val, 0, 0xff); } if (PREF_CHANGED(HTTP_PREF("redirection-limit"))) { rv = prefs->GetIntPref(HTTP_PREF("redirection-limit"), &val); if (NS_SUCCEEDED(rv)) - mRedirectionLimit = (PRUint8) NS_CLAMP(val, 0, 0xff); + mRedirectionLimit = (PRUint8) clamped(val, 0, 0xff); } if (PREF_CHANGED(HTTP_PREF("connection-retry-timeout"))) { rv = prefs->GetIntPref(HTTP_PREF("connection-retry-timeout"), &val); if (NS_SUCCEEDED(rv)) - mIdleSynTimeout = (PRUint16) NS_CLAMP(val, 0, 3000); + mIdleSynTimeout = (PRUint16) clamped(val, 0, 3000); } if (PREF_CHANGED(HTTP_PREF("fast-fallback-to-IPv4"))) { @@ -980,7 +982,7 @@ nsHttpHandler::PrefsChanged(nsIPrefBranch *prefs, const char *pref) if (PREF_CHANGED(HTTP_PREF("pipelining.maxrequests"))) { rv = prefs->GetIntPref(HTTP_PREF("pipelining.maxrequests"), &val); if (NS_SUCCEEDED(rv)) { - mMaxPipelinedRequests = NS_CLAMP(val, 1, NS_HTTP_MAX_PIPELINED_REQUESTS); + mMaxPipelinedRequests = clamped(val, 1, NS_HTTP_MAX_PIPELINED_REQUESTS); if (mConnMgr) mConnMgr->UpdateParam(nsHttpConnectionMgr::MAX_PIPELINED_REQUESTS, mMaxPipelinedRequests); @@ -1006,7 +1008,7 @@ nsHttpHandler::PrefsChanged(nsIPrefBranch *prefs, const char *pref) if (PREF_CHANGED(HTTP_PREF("qos"))) { rv = prefs->GetIntPref(HTTP_PREF("qos"), &val); if (NS_SUCCEEDED(rv)) - mQoSBits = (PRUint8) NS_CLAMP(val, 0, 0xff); + mQoSBits = (PRUint8) clamped(val, 0, 0xff); } if (PREF_CHANGED(HTTP_PREF("sendSecureXSiteReferrer"))) { @@ -1079,7 +1081,7 @@ nsHttpHandler::PrefsChanged(nsIPrefBranch *prefs, const char *pref) if (PREF_CHANGED(HTTP_PREF("phishy-userpass-length"))) { rv = prefs->GetIntPref(HTTP_PREF("phishy-userpass-length"), &val); if (NS_SUCCEEDED(rv)) - mPhishyUserPassLength = (PRUint8) NS_CLAMP(val, 0, 0xff); + mPhishyUserPassLength = (PRUint8) clamped(val, 0, 0xff); } // diff --git a/netwerk/protocol/websocket/WebSocketChannel.cpp b/netwerk/protocol/websocket/WebSocketChannel.cpp index a97f795ff7d4..65a514c8d4bd 100644 --- a/netwerk/protocol/websocket/WebSocketChannel.cpp +++ b/netwerk/protocol/websocket/WebSocketChannel.cpp @@ -1977,25 +1977,25 @@ WebSocketChannel::AsyncOpen(nsIURI *aURI, rv = prefService->GetIntPref("network.websocket.max-message-size", &intpref); if (NS_SUCCEEDED(rv)) { - mMaxMessageSize = NS_CLAMP(intpref, 1024, 1 << 30); + mMaxMessageSize = clamped(intpref, 1024, 1 << 30); } rv = prefService->GetIntPref("network.websocket.timeout.close", &intpref); if (NS_SUCCEEDED(rv)) { - mCloseTimeout = NS_CLAMP(intpref, 1, 1800) * 1000; + mCloseTimeout = clamped(intpref, 1, 1800) * 1000; } rv = prefService->GetIntPref("network.websocket.timeout.open", &intpref); if (NS_SUCCEEDED(rv)) { - mOpenTimeout = NS_CLAMP(intpref, 1, 1800) * 1000; + mOpenTimeout = clamped(intpref, 1, 1800) * 1000; } rv = prefService->GetIntPref("network.websocket.timeout.ping.request", &intpref); if (NS_SUCCEEDED(rv)) { - mPingTimeout = NS_CLAMP(intpref, 0, 86400) * 1000; + mPingTimeout = clamped(intpref, 0, 86400) * 1000; } rv = prefService->GetIntPref("network.websocket.timeout.ping.response", &intpref); if (NS_SUCCEEDED(rv)) { - mPingResponseTimeout = NS_CLAMP(intpref, 1, 3600) * 1000; + mPingResponseTimeout = clamped(intpref, 1, 3600) * 1000; } rv = prefService->GetBoolPref("network.websocket.extensions.stream-deflate", &boolpref); @@ -2010,7 +2010,7 @@ WebSocketChannel::AsyncOpen(nsIURI *aURI, rv = prefService->GetIntPref ("network.websocket.max-connections", &intpref); if (NS_SUCCEEDED(rv)) { - mMaxConcurrentConnections = NS_CLAMP(intpref, 1, 0xffff); + mMaxConcurrentConnections = clamped(intpref, 1, 0xffff); } } diff --git a/widget/src/android/nsWindow.cpp b/widget/src/android/nsWindow.cpp index 56f0882c8290..3797ac47c01d 100644 --- a/widget/src/android/nsWindow.cpp +++ b/widget/src/android/nsWindow.cpp @@ -801,8 +801,8 @@ nsWindow::OnGlobalAndroidEvent(AndroidGeckoEvent *ae) win->UserActivity(); if (!gTopLevelWindows.IsEmpty()) { nsIntPoint pt(ae->P0()); - pt.x = NS_MIN(NS_MAX(pt.x, 0), gAndroidBounds.width - 1); - pt.y = NS_MIN(NS_MAX(pt.y, 0), gAndroidBounds.height - 1); + pt.x = clamped(pt.x, 0, gAndroidBounds.width - 1); + pt.y = clamped(pt.y, 0, gAndroidBounds.height - 1); nsWindow *target = win->FindWindowForPoint(pt); #if 0 diff --git a/xpcom/base/nscore.h b/xpcom/base/nscore.h index 17d84268a2f6..9cde77dd8ee3 100644 --- a/xpcom/base/nscore.h +++ b/xpcom/base/nscore.h @@ -424,11 +424,6 @@ typedef PRUint32 nsrefcnt; #define NS_STRINGIFY_HELPER(x_) #x_ #define NS_STRINGIFY(x_) NS_STRINGIFY_HELPER(x_) -/* - * Use NS_CLAMP to force a value (such as a preference) into a range. - */ -#define NS_CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x))) - /* * These macros allow you to give a hint to the compiler about branch * probability so that it can better optimize. Use them like this: diff --git a/xpcom/string/public/nsAlgorithm.h b/xpcom/string/public/nsAlgorithm.h index 71cf7dbeb240..2816f3cc1e3d 100644 --- a/xpcom/string/public/nsAlgorithm.h +++ b/xpcom/string/public/nsAlgorithm.h @@ -80,6 +80,19 @@ NS_MAX( const T& a, const T& b ) return a > b ? a : b; } +namespace mozilla { + +template +inline +const T& +clamped( const T& a, const T& min, const T& max ) + { + NS_ABORT_IF_FALSE(max >= min, "clamped(): max must be greater than or equal to min"); + return NS_MIN(NS_MAX(a, min), max); + } + +} + template inline T