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.

This commit is contained in:
Jonathan Watt 2011-10-28 19:33:28 +01:00
Родитель 30960abdf5
Коммит 66f54fe5a4
22 изменённых файлов: 93 добавлений и 70 удалений

Просмотреть файл

@ -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;

Просмотреть файл

@ -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<PRUint8>(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<PRUint8>(NS_MIN(NS_MAX(0.f, result), 255.f));
static_cast<PRUint8>(clamped(result, 0.f, 255.f));
}
}
}

Просмотреть файл

@ -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

Просмотреть файл

@ -478,7 +478,7 @@ GDIFontFamily::FamilyAddStylesProc(const ENUMLOGFONTEXW *lpelfe,
GDIFontFamily *ff = reinterpret_cast<GDIFontFamily*>(data);
// Some fonts claim to support things > 900, but we don't so clamp the sizes
logFont.lfWeight = NS_MAX<LONG>(NS_MIN<LONG>(logFont.lfWeight, 900), 100);
logFont.lfWeight = clamped(logFont.lfWeight, LONG(100), LONG(900));
gfxWindowsFontType feType = GDIFontEntry::DetermineFontType(metrics, fontType);

Просмотреть файл

@ -40,6 +40,7 @@
#include "mozilla/gfx/BasePoint4D.h"
#include "gfx3DMatrix.h"
#include "nsAlgorithm.h"
struct THEBES_API gfxQuaternion : public mozilla::gfx::BasePoint4D<gfxFloat, gfxQuaternion> {
typedef mozilla::gfx::BasePoint4D<gfxFloat, gfxQuaternion> Super;
@ -62,7 +63,7 @@ struct THEBES_API gfxQuaternion : public mozilla::gfx::BasePoint4D<gfxFloat, gfx
}
gfxQuaternion Slerp(const gfxQuaternion &aOther, gfxFloat aCoeff) {
gfxFloat dot = NS_CLAMP(DotProduct(aOther), -1.0, 1.0);
gfxFloat dot = mozilla::clamped(DotProduct(aOther), -1.0, 1.0);
if (dot == 1.0) {
return *this;
}
@ -91,11 +92,11 @@ struct THEBES_API gfxQuaternion : public mozilla::gfx::BasePoint4D<gfxFloat, gfx
temp[1][2] = 2 * (y * z + w * x);
temp[2][0] = 2 * (x * z + w * y);
temp[2][1] = 2 * (y * z - w * x);
temp[2][2] = 1 - 2 * (x * x + y * y);
temp[2][2] = 1 - 2 * (x * x + y * y);
return temp;
}
};
#endif /* GFX_QUATERNION_H */
#endif /* GFX_QUATERNION_H */

Просмотреть файл

@ -42,6 +42,7 @@
#include <limits.h>
#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;

Просмотреть файл

@ -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

Просмотреть файл

@ -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,

Просмотреть файл

@ -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)

Просмотреть файл

@ -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);

Просмотреть файл

@ -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?

Просмотреть файл

@ -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;

Просмотреть файл

@ -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();

Просмотреть файл

@ -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;

4
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.

Просмотреть файл

@ -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")) {

Просмотреть файл

@ -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;

Просмотреть файл

@ -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);
}
//

Просмотреть файл

@ -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);
}
}

Просмотреть файл

@ -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

Просмотреть файл

@ -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:

Просмотреть файл

@ -80,6 +80,19 @@ NS_MAX( const T& a, const T& b )
return a > b ? a : b;
}
namespace mozilla {
template <class T>
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 <class T>
inline
T