From 732735ed3975ce53d229bc1563f26fb55d6f95d6 Mon Sep 17 00:00:00 2001 From: robertc <> Date: Wed, 22 Jan 2003 17:05:41 +0000 Subject: [PATCH] Convert Stack to template based code, to allow typesafe composing on Vectors --- include/Stack.h | 24 +++++++++++++++--- lib/Makefile.am | 3 +-- lib/Stack.c | 62 --------------------------------------------- src/HttpHdrRange.cc | 10 ++++---- src/HttpHeader.cc | 8 +++--- src/cbdata.cc | 4 +-- src/errorpage.cc | 10 ++++---- 7 files changed, 38 insertions(+), 83 deletions(-) delete mode 100644 lib/Stack.c diff --git a/include/Stack.h b/include/Stack.h index 7248f1d600..c044cbced1 100644 --- a/include/Stack.h +++ b/include/Stack.h @@ -1,5 +1,5 @@ /* - * $Id: Stack.h,v 1.12 2002/10/13 20:34:51 robertc Exp $ + * $Id: Stack.h,v 1.13 2003/01/22 10:05:42 robertc Exp $ * * AUTHOR: Alex Rousskov * @@ -42,9 +42,27 @@ typedef Array Stack; #define stackInit arrayInit #define stackClean arrayClean #define stackDestroy arrayDestroy -SQUIDCEXTERN void *stackPop(Stack * s); #define stackPush arrayAppend #define stackPrePush arrayPreAppend -SQUIDCEXTERN void *stackTop(Stack * s); +template +typename S::value_type +stackPop(S * s) +{ + assert(s); + if (!s->count) + return typename S::value_type(); + typename S::value_type result = s->items[--s->count]; + s->items[s->count] = typename S::value_type(); + return result; +} + +/* todo, fatal on empty Top call */ +template +typename S::value_type +stackTop(S * s) +{ + assert(s); + return s->count ? s->items[s->count - 1] : typename S::value_type(); +} #endif /* SQUID_STACK_H */ diff --git a/lib/Makefile.am b/lib/Makefile.am index 418e85dc78..ea39f86fc5 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -1,6 +1,6 @@ ## Process this file with automake to produce Makefile.in # -# $Id: Makefile.am,v 1.6 2003/01/18 14:06:32 robertc Exp $ +# $Id: Makefile.am,v 1.7 2003/01/22 10:05:41 robertc Exp $ # if ENABLE_XPROF_STATS @@ -50,7 +50,6 @@ libmiscutil_a_SOURCES = \ safe_inet_addr.c \ $(SNPRINTFSOURCE) \ splay.c \ - Stack.c \ stub_memaccount.c \ util.c \ uudecode.c \ diff --git a/lib/Stack.c b/lib/Stack.c deleted file mode 100644 index a8e0703b1e..0000000000 --- a/lib/Stack.c +++ /dev/null @@ -1,62 +0,0 @@ -/* - * $Id: Stack.c,v 1.11 2001/01/12 00:37:12 wessels Exp $ - * - * AUTHOR: Alex Rousskov - * - * SQUID Web Proxy Cache http://www.squid-cache.org/ - * ---------------------------------------------------------- - * - * Squid is the result of efforts by numerous individuals from - * the Internet community; see the CONTRIBUTORS file for full - * details. Many organizations have provided support for Squid's - * development; see the SPONSORS file for full details. Squid is - * Copyrighted (C) 2001 by the Regents of the University of - * California; see the COPYRIGHT file for full details. Squid - * incorporates software developed and/or copyrighted by other - * sources; see the CREDITS file for full details. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. - * - */ - -/* - * Stack is a (void*) stack with unlimited capacity; based on Array - */ - - -#include "config.h" -#if HAVE_ASSERT_H -#include -#endif -#if HAVE_STRING_H -#include -#endif -#include "util.h" -#include "Stack.h" - - -void * -stackPop(Stack * s) -{ - assert(s); - return s->count ? s->items[--s->count] : NULL; -} - -void * -stackTop(Stack * s) -{ - assert(s); - return s->count ? s->items[s->count - 1] : NULL; -} diff --git a/src/HttpHdrRange.cc b/src/HttpHdrRange.cc index d68f65f80f..7abe5eecac 100644 --- a/src/HttpHdrRange.cc +++ b/src/HttpHdrRange.cc @@ -1,6 +1,6 @@ /* - * $Id: HttpHdrRange.cc,v 1.28 2002/10/15 08:03:29 robertc Exp $ + * $Id: HttpHdrRange.cc,v 1.29 2003/01/22 10:05:43 robertc Exp $ * * DEBUG: section 64 HTTP Range Header * AUTHOR: Alex Rousskov @@ -276,7 +276,7 @@ HttpHdrRange * httpHdrRangeDup(const HttpHdrRange * range) { HttpHdrRange *dup; - int i; + size_t i; assert(range); dup = httpHdrRangeCreate(); stackPrePush(&dup->specs, range->specs.count); @@ -309,7 +309,7 @@ httpHdrRangePackInto(const HttpHdrRange * range, Packer * p) int httpHdrRangeCanonize(HttpHdrRange * range, ssize_t clen) { - int i; + size_t i; HttpHdrRangeSpec *spec; HttpHdrRangePos pos = HttpHdrRangeInitPos; Stack goods; @@ -364,9 +364,9 @@ HttpHdrRangeSpec * httpHdrRangeGetSpec(const HttpHdrRange * range, HttpHdrRangePos * pos) { assert(range); - assert(pos && *pos >= -1 && *pos < range->specs.count); + assert(pos && *pos >= -1 && *pos < (ssize_t)range->specs.count); (*pos)++; - if (*pos < range->specs.count) + if (*pos < (ssize_t)range->specs.count) return (HttpHdrRangeSpec *) range->specs.items[*pos]; else return NULL; diff --git a/src/HttpHeader.cc b/src/HttpHeader.cc index 6736de823c..3f4f937f7e 100644 --- a/src/HttpHeader.cc +++ b/src/HttpHeader.cc @@ -1,6 +1,6 @@ /* - * $Id: HttpHeader.cc,v 1.82 2002/12/06 23:19:13 hno Exp $ + * $Id: HttpHeader.cc,v 1.83 2003/01/22 10:05:43 robertc Exp $ * * DEBUG: section 55 HTTP Header * AUTHOR: Alex Rousskov @@ -457,8 +457,8 @@ HttpHeaderEntry * httpHeaderGetEntry(const HttpHeader * hdr, HttpHeaderPos * pos) { assert(hdr && pos); - assert(*pos >= HttpHeaderInitPos && *pos < hdr->entries.count); - for ((*pos)++; *pos < hdr->entries.count; (*pos)++) { + assert(*pos >= HttpHeaderInitPos && *pos < (ssize_t)hdr->entries.count); + for ((*pos)++; *pos < (ssize_t)hdr->entries.count; (*pos)++) { if (hdr->entries.items[*pos]) return (HttpHeaderEntry*)hdr->entries.items[*pos]; } @@ -570,7 +570,7 @@ void httpHeaderDelAt(HttpHeader * hdr, HttpHeaderPos pos) { HttpHeaderEntry *e; - assert(pos >= HttpHeaderInitPos && pos < hdr->entries.count); + assert(pos >= HttpHeaderInitPos && pos < (ssize_t)hdr->entries.count); e = (HttpHeaderEntry*)hdr->entries.items[pos]; hdr->entries.items[pos] = NULL; /* decrement header length, allow for ": " and crlf */ diff --git a/src/cbdata.cc b/src/cbdata.cc index 6c59c608bd..7daa9584a3 100644 --- a/src/cbdata.cc +++ b/src/cbdata.cc @@ -1,6 +1,6 @@ /* - * $Id: cbdata.cc,v 1.49 2002/10/25 03:13:51 robertc Exp $ + * $Id: cbdata.cc,v 1.50 2003/01/22 10:05:43 robertc Exp $ * * DEBUG: section 45 Callback Data Registry * ORIGINAL AUTHOR: Duane Wessels @@ -384,7 +384,7 @@ cbdataDump(StoreEntry * sentry) template T& for_each(Stack const &collection, T& visitor) { - for (int index = 0; index < collection.count; ++index) + for (size_t index = 0; index < collection.count; ++index) visitor(*(typename T::argument_type const *)collection.items[index]); return visitor; }; diff --git a/src/errorpage.cc b/src/errorpage.cc index 5b5423935e..f61315d764 100644 --- a/src/errorpage.cc +++ b/src/errorpage.cc @@ -1,6 +1,6 @@ /* - * $Id: errorpage.cc,v 1.180 2002/10/25 07:36:32 robertc Exp $ + * $Id: errorpage.cc,v 1.181 2003/01/22 10:05:43 robertc Exp $ * * DEBUG: section 4 Error Generation * AUTHOR: Duane Wessels @@ -230,9 +230,9 @@ errorPageId(const char *page_name) if (strcmp(err_type_str[i], page_name) == 0) return i; } - for (i = 0; i < ErrorDynamicPages.count; i++) { - if (strcmp(((ErrorDynamicPageInfo *) ErrorDynamicPages.items[i])->page_name, page_name) == 0) - return i + ERR_MAX; + for (size_t in = 0; in < ErrorDynamicPages.count; in++) { + if (strcmp(((ErrorDynamicPageInfo *) ErrorDynamicPages.items[in])->page_name, page_name) == 0) + return in + ERR_MAX; } return ERR_NONE; } @@ -255,7 +255,7 @@ errorPageName(int pageId) { if (pageId >= ERR_NONE && pageId < ERR_MAX) /* common case */ return err_type_str[pageId]; - if (pageId >= ERR_MAX && pageId - ERR_MAX < ErrorDynamicPages.count) + if (pageId >= ERR_MAX && pageId - ERR_MAX < (ssize_t)ErrorDynamicPages.count) return ((ErrorDynamicPageInfo *) ErrorDynamicPages. items[pageId - ERR_MAX])->page_name; return "ERR_UNKNOWN"; /* should not happen */ -- 2.47.3