From: Amos Jeffries Date: Fri, 6 Mar 2009 13:26:57 +0000 (+1300) Subject: Compat: Shuffle semi-duplicate min/max alternatives all into libcompat X-Git-Tag: SQUID_3_2_0_1~1136 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d85c307876485741590083a3b35d96abb423fb58;p=thirdparty%2Fsquid.git Compat: Shuffle semi-duplicate min/max alternatives all into libcompat --- diff --git a/compat/compat_shared.h b/compat/compat_shared.h index a2bc9a4e9b..63aa8ac38e 100644 --- a/compat/compat_shared.h +++ b/compat/compat_shared.h @@ -94,10 +94,8 @@ struct rusage #endif /* !HAVE_STRUCT_RUSAGE */ -/* templates require C++ */ -#ifdef __cplusplus - #ifndef min +#ifdef __cplusplus /** * min() comparison may not always be provided. * Squid bundles this template for when its needed. @@ -111,10 +109,14 @@ min(A const & lhs, A const & rhs) return rhs; return lhs; } -#endif -#define XMIN(x,y) (min (x,y)) +#else /* !__cplusplus */ +/* for non-C++ we are stuck with the < and ? operator */ +#define min(a,b) ((a) < (b) ? (a) : (b)) +#endif /* __cplusplus */ +#endif /* min */ #ifndef max +#ifdef __cplusplus /** * max() comparison may not always be provided. * Squid bundles this template for when its needed. @@ -128,10 +130,11 @@ max(A const & lhs, A const & rhs) return rhs; return lhs; } -#endif -#define XMAX(a,b) (max (a,b)) - +#else /* !__cplusplus */ +/* for non-C++ we are stuck with the < and ? operator */ +#define max(a,b) ((a) < (b) ? (b) : (a)) #endif /* __cplusplus */ +#endif /* max */ /** * tempnam() not provided by all systems diff --git a/include/Range.h b/include/Range.h index e32b99d782..e23c947d68 100644 --- a/include/Range.h +++ b/include/Range.h @@ -70,7 +70,7 @@ template Range Range::intersection (Range const &rhs) const { - Range result (XMAX(start, rhs.start), XMIN(end, rhs.end)); + Range result (max(start, rhs.start), min(end, rhs.end)); return result; } diff --git a/include/util.h b/include/util.h index b93947fb38..093225d783 100644 --- a/include/util.h +++ b/include/util.h @@ -146,13 +146,6 @@ SQUIDCEXTERN const char *xint64toa(int64_t num); SQUIDCEXTERN double drand48(void); #endif -#ifndef xmax -#define xmax(a,b) ((a) < (b) ? (b) : (a)) -#endif -#ifndef xmin -#define xmin(a,b) ((a) < (b) ? (a) : (b)) -#endif - typedef struct { size_t count; size_t bytes; diff --git a/src/BodyPipe.cc b/src/BodyPipe.cc index 66e0563e2c..0cae9446c1 100644 --- a/src/BodyPipe.cc +++ b/src/BodyPipe.cc @@ -204,10 +204,10 @@ size_t BodyPipe::putMoreData(const char *buf, size_t size) { if (bodySizeKnown()) - size = XMIN((uint64_t)size, unproducedSize()); + size = min((uint64_t)size, unproducedSize()); const size_t spaceSize = static_cast(theBuf.potentialSpaceSize()); - if ((size = XMIN(size, spaceSize))) { + if ((size = min(size, spaceSize))) { theBuf.append(buf, size); postAppend(size); return size; @@ -269,7 +269,7 @@ BodyPipe::getMoreData(MemBuf &buf) if (buf.isNull()) buf.init(); - const size_t size = XMIN(theBuf.contentSize(), buf.potentialSpaceSize()); + const size_t size = min(theBuf.contentSize(), buf.potentialSpaceSize()); buf.append(theBuf.content(), size); theBuf.consume(size); postConsume(size); diff --git a/src/ChunkedCodingParser.cc b/src/ChunkedCodingParser.cc index a3ef03f2dc..ebd2bd781f 100644 --- a/src/ChunkedCodingParser.cc +++ b/src/ChunkedCodingParser.cc @@ -91,8 +91,8 @@ void ChunkedCodingParser::parseChunkBody() { Must(theLeftBodySize > 0); // Should, really - const size_t availSize = XMIN(theLeftBodySize, (uint64_t)theIn->contentSize()); - const size_t safeSize = XMIN(availSize, (size_t)theOut->potentialSpaceSize()); + const size_t availSize = min(theLeftBodySize, (uint64_t)theIn->contentSize()); + const size_t safeSize = min(availSize, (size_t)theOut->potentialSpaceSize()); doNeedMoreData = availSize < theLeftBodySize; // and we may also need more space diff --git a/src/adaptation/icap/ModXact.cc b/src/adaptation/icap/ModXact.cc index 7038f7c3d7..4cb1622ea0 100644 --- a/src/adaptation/icap/ModXact.cc +++ b/src/adaptation/icap/ModXact.cc @@ -208,7 +208,7 @@ void Adaptation::Icap::ModXact::writePreviewBody() Must(virgin.body_pipe != NULL); const size_t sizeMax = (size_t)virgin.body_pipe->buf().contentSize(); - const size_t size = XMIN(preview.debt(), sizeMax); + const size_t size = min(preview.debt(), sizeMax); writeSomeBody("preview body", size); // change state once preview is written @@ -249,7 +249,7 @@ void Adaptation::Icap::ModXact::writeSomeBody(const char *label, size_t size) writeBuf.init(); // note: we assume that last-chunk will fit const size_t writableSize = virginContentSize(virginBodyWriting); - const size_t chunkSize = XMIN(writableSize, size); + const size_t chunkSize = min(writableSize, size); if (chunkSize) { debugs(93, 7, HERE << "will write " << chunkSize << @@ -368,10 +368,10 @@ void Adaptation::Icap::ModXact::virginConsume() " from " << virgin.body_pipe->status()); if (virginBodyWriting.active()) - offset = XMIN(virginBodyWriting.offset(), offset); + offset = min(virginBodyWriting.offset(), offset); if (virginBodySending.active()) - offset = XMIN(virginBodySending.offset(), offset); + offset = min(virginBodySending.offset(), offset); Must(virginConsumed <= offset && offset <= end); @@ -1201,13 +1201,13 @@ void Adaptation::Icap::ModXact::decideOnPreview() Must(wantedSize >= 0); // cannot preview more than we can backup - size_t ad = XMIN(wantedSize, TheBackupLimit); + size_t ad = min(wantedSize, TheBackupLimit); if (!virginBody.expected()) ad = 0; else if (virginBody.knownSize()) - ad = XMIN(static_cast(ad), virginBody.size()); // not more than we have + ad = min(static_cast(ad), virginBody.size()); // not more than we have debugs(93, 5, HERE << "should offer " << ad << "-byte preview " << "(service wanted " << wantedSize << ")"); diff --git a/src/client_side.cc b/src/client_side.cc index 4934d55126..74b68fcadb 100644 --- a/src/client_side.cc +++ b/src/client_side.cc @@ -789,7 +789,7 @@ ClientSocketContext::lengthToSend(Range const &available) if (available.start < http->range_iter.currentSpec()->offset) return 0; - return XMIN(http->range_iter.debt(), (int64_t)maximum); + return min(http->range_iter.debt(), (int64_t)maximum); } void diff --git a/src/comm.cc b/src/comm.cc index 10e9ee70a4..a42c642958 100644 --- a/src/comm.cc +++ b/src/comm.cc @@ -1909,7 +1909,7 @@ comm_init(void) { /* Keep a few file descriptors free so that we don't run out of FD's * after accepting a client but before it opens a socket or a file. * Since Squid_MaxFD can be as high as several thousand, don't waste them */ - RESERVED_FD = XMIN(100, Squid_MaxFD / 4); + RESERVED_FD = min(100, Squid_MaxFD / 4); conn_close_pool = memPoolCreate("close_handler", sizeof(close_handler)); diff --git a/src/fd.cc b/src/fd.cc index 2438e8fbd5..144d36b720 100644 --- a/src/fd.cc +++ b/src/fd.cc @@ -310,12 +310,12 @@ fdAdjustReserved(void) /* * Calculate a new reserve, based on current usage and a small extra */ - newReserve = Squid_MaxFD - Number_FD + XMIN(25, Squid_MaxFD / 16); + newReserve = Squid_MaxFD - Number_FD + min(25, Squid_MaxFD / 16); if (newReserve <= RESERVED_FD) return; - x = Squid_MaxFD - 20 - XMIN(25, Squid_MaxFD / 16); + x = Squid_MaxFD - 20 - min(25, Squid_MaxFD / 16); if (newReserve > x) { /* perhaps this should be fatal()? -DW */ @@ -323,7 +323,7 @@ fdAdjustReserved(void) newReserve = x; } - if (Squid_MaxFD - newReserve < XMIN(256, Squid_MaxFD / 2)) + if (Squid_MaxFD - newReserve < min(256, Squid_MaxFD / 2)) fatalf("Too few filedescriptors available in the system (%d usable of %d).\n", Squid_MaxFD - newReserve, Squid_MaxFD); debugs(51, 0, "Reserved FD adjusted from " << RESERVED_FD << " to " << newReserve << " due to failures"); diff --git a/src/ip/IpAddress.cc b/src/ip/IpAddress.cc index df001986e5..ee7dcd4a8e 100644 --- a/src/ip/IpAddress.cc +++ b/src/ip/IpAddress.cc @@ -978,9 +978,9 @@ char* IpAddress::NtoA(char* buf, const unsigned int blen, int force) const /* thats okay, our default is known */ if ( IsAnyAddr() ) { #if USE_IPV6 - memcpy(buf,"::\0", xmin(3,blen)); + memcpy(buf,"::\0", min((const unsigned int)3,blen)); #else - memcpy(buf,"0.0.0.0\0", xmin(8,blen)); + memcpy(buf,"0.0.0.0\0", min((const unsigned int)8,blen)); #endif return buf; } @@ -991,7 +991,7 @@ char* IpAddress::NtoA(char* buf, const unsigned int blen, int force) const /* However IPv4 CAN. */ if ( force == AF_INET && !IsIPv4() ) { if ( IsIPv6() ) { - memcpy(buf, "{!IPv4}\0", xmin(8,blen)); + memcpy(buf, "{!IPv4}\0", min((const unsigned int)8,blen)); } return buf; } @@ -1015,7 +1015,7 @@ char* IpAddress::NtoA(char* buf, const unsigned int blen, int force) const force << "). accepted={" << AF_UNSPEC << "," << AF_INET << "," << AF_INET6 << "}"); fprintf(stderr,"WARNING: Corrupt IP Address details OR required to display in unknown format (%d). accepted={%d,%d,%d} ", force, AF_UNSPEC, AF_INET, AF_INET6); - memcpy(buf,"dead:beef::\0", xmin(13,blen)); + memcpy(buf,"dead:beef::\0", min((const unsigned int)13,blen)); assert(false); } diff --git a/src/ipcache.cc b/src/ipcache.cc index 5834fbae2e..a82cbda3c0 100644 --- a/src/ipcache.cc +++ b/src/ipcache.cc @@ -1333,7 +1333,7 @@ ipcacheMarkBadAddr(const char *name, IpAddress &addr) if (!ia->bad_mask[k]) { ia->bad_mask[k] = TRUE; ia->badcount++; - i->expires = XMIN(squid_curtime + XMAX((time_t)60, Config.negativeDnsTtl), i->expires); + i->expires = min(squid_curtime + max((time_t)60, Config.negativeDnsTtl), i->expires); debugs(14, 2, "ipcacheMarkBadAddr: " << name << " " << addr ); } diff --git a/src/stmem.cc b/src/stmem.cc index e8dfe64663..b264138f69 100644 --- a/src/stmem.cc +++ b/src/stmem.cc @@ -140,7 +140,7 @@ mem_hdr::writeAvailable(mem_node *aNode, int64_t location, size_t amount, char c /* these two can go I think */ assert (location - aNode->nodeBuffer.offset == (int64_t)aNode->nodeBuffer.length); - size_t copyLen = XMIN (amount, aNode->space()); + size_t copyLen = min(amount, aNode->space()); xmemcpy(aNode->nodeBuffer.data + aNode->nodeBuffer.length, source, copyLen); @@ -216,7 +216,7 @@ mem_hdr::copyAvailable(mem_node *aNode, int64_t location, size_t amount, char *t size_t copyOffset = location - aNode->nodeBuffer.offset; - size_t copyLen = XMIN (amount, aNode->nodeBuffer.length - copyOffset); + size_t copyLen = min(amount, aNode->nodeBuffer.length - copyOffset); xmemcpy(target, aNode->nodeBuffer.data + copyOffset, copyLen); diff --git a/src/store_client.cc b/src/store_client.cc index 077d8681b8..c6ead68240 100644 --- a/src/store_client.cc +++ b/src/store_client.cc @@ -585,7 +585,7 @@ store_client::readHeader(char const *buf, ssize_t len) /* * we have (part of) what they want */ - size_t copy_sz = XMIN(copyInto.length, body_sz); + size_t copy_sz = min(copyInto.length, body_sz); debugs(90, 3, "storeClientReadHeader: copying " << copy_sz << " bytes of body"); xmemmove(copyInto.data, copyInto.data + mem->swap_hdr_sz, copy_sz); diff --git a/src/tests/stub_comm.cc b/src/tests/stub_comm.cc index 83b96c5c5a..56a57ad711 100644 --- a/src/tests/stub_comm.cc +++ b/src/tests/stub_comm.cc @@ -147,7 +147,7 @@ comm_init(void) /* Keep a few file descriptors free so that we don't run out of FD's * after accepting a client but before it opens a socket or a file. * Since Squid_MaxFD can be as high as several thousand, don't waste them */ - RESERVED_FD = XMIN(100, Squid_MaxFD / 4); + RESERVED_FD = min(100, Squid_MaxFD / 4); } /* MinGW needs also a stub of _comm_close() */