--------------------- PatchSet 10191 Date: 2007/12/03 07:59:25 Author: adri Branch: s27_adri Tag: (none) Log: Shuffle the String code out of the src tree and into the libbuf tree. It currently sits alongside the "new" str_t stuff I've ported over from a private branch. The String code and its users currently expects a NUL-terminated string. The str_t code does not. The String code can (probably!) be extended to handle refcounting and copy-on-modify semantics as long as users obey the API and don't fiddle with buf directly. It can't reference part of another buffer as it won't handle non-NUL terminated strings. The str_t code handles this fine. (In fact, the String code could handle non-NUL terminated strings easily. Unfortunately the current users of String expect NUL-terminations.) Members: libbuf/Makefile.am:1.1.2.1->1.1.2.2 libbuf/String.c:1.1->1.1.2.1 libbuf/String.h:1.1->1.1.2.1 src/String.c:1.7->1.7.44.1 src/globals.h:1.31.8.2->1.31.8.3 src/protos.h:1.146.2.4.4.2->1.146.2.4.4.3 src/squid.h:1.36.24.2->1.36.24.3 src/structs.h:1.158.2.5.4.1->1.158.2.5.4.2 src/typedefs.h:1.43.2.3.4.1->1.43.2.3.4.2 Index: squid/libbuf/Makefile.am =================================================================== RCS file: /cvsroot/squid-sf//squid/libbuf/Attic/Makefile.am,v retrieving revision 1.1.2.1 retrieving revision 1.1.2.2 diff -u -r1.1.2.1 -r1.1.2.2 --- squid/libbuf/Makefile.am 2 Dec 2007 09:35:49 -0000 1.1.2.1 +++ squid/libbuf/Makefile.am 3 Dec 2007 07:59:25 -0000 1.1.2.2 @@ -1,17 +1,19 @@ ## Process this file with automake to produce Makefile.in # -# $Id: Makefile.am,v 1.1.2.1 2007/12/02 09:35:49 adri Exp $ +# $Id: Makefile.am,v 1.1.2.2 2007/12/03 07:59:25 adri Exp $ # libbuf_a_SOURCES = \ buf.c \ iobuf.c \ - str.c + str.c \ + String.c libbuf_a_LIBADD = \ buf.o \ iobuf.o \ - str.o + str.o \ + String.o noinst_LIBRARIES = \ libbuf.a --- /dev/null Tue Dec 4 01:19:10 2007 +++ squid/libbuf/String.c Tue Dec 4 01:19:10 2007 @@ -0,0 +1,123 @@ + +/* + * $Id: String.c,v 1.1.2.1 2007/12/03 07:59:25 adri Exp $ + * + * DEBUG: section 67 String + * AUTHOR: Duane Wessels + * + * 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. + * + */ + +#include +#include +#include +#include + +#include "../libcore/tools.h" +#include "../libcore/varargs.h" +#include "../libcore/debug.h" + +#include "String.h" + +const String StringNull = { 0, 0, NULL }; + +static void +stringInitBuf(String * s, size_t sz) +{ + assert(sz < 65536); + s->nbuf = malloc(sz); + s->size = sz; +} + +void +stringInit(String * s, const char *str) +{ + assert(s); + if (str) + stringLimitInit(s, str, strlen(str)); + else + *s = StringNull; +} + +void +stringLimitInit(String * s, const char *str, int len) +{ + assert(s && str); + stringInitBuf(s, len + 1); + s->len = len; + xmemcpy(s->nbuf, str, len); + s->nbuf[len] = '\0'; +} + +String +stringDup(const String * s) +{ + String dup; + assert(s); + stringInit(&dup, s->nbuf); + return dup; +} + +void +stringClean(String * s) +{ + assert(s); + if (s->nbuf) { + free(s->nbuf); s->nbuf = NULL; + } + *s = StringNull; +} + +void +stringReset(String * s, const char *str) +{ + stringClean(s); + stringInit(s, str); +} + +void +stringAppend(String * s, const char *str, int len) +{ + assert(s); + assert(str && len >= 0); + if (s->len + len < s->size) { + strncat(s->nbuf, str, len); + s->len += len; + } else { + String snew = StringNull; + snew.len = s->len + len; + stringInitBuf(&snew, snew.len + 1); + if (s->nbuf) + xmemcpy(snew.nbuf, s->nbuf, s->len); + if (len) + xmemcpy(snew.nbuf + s->len, str, len); + snew.nbuf[snew.len] = '\0'; + stringClean(s); + *s = snew; + } +} --- /dev/null Tue Dec 4 01:19:10 2007 +++ squid/libbuf/String.h Tue Dec 4 01:19:10 2007 @@ -0,0 +1,38 @@ +#ifndef __LIBBUF_STRING_H__ +#define __LIBBUF_STRING_H__ + + +struct _String { + /* never reference these directly! */ + unsigned short int size; /* buffer size; 64K limit */ + unsigned short int len; /* current length */ + char *nbuf; +}; + +typedef struct _String String; + +/* String */ +#define strLen(s) ((/* const */ int)(s).len) +#define strBuf(s) ((const char*)(s).nbuf) +#define strChr(s,ch) ((const char*)strchr(strBuf(s), (ch))) +#define strRChr(s,ch) ((const char*)strrchr(strBuf(s), (ch))) +#define strStr(s,str) ((const char*)strstr(strBuf(s), (str))) +#define strCmp(s,str) strcmp(strBuf(s), (str)) +#define strNCmp(s,str,n) strncmp(strBuf(s), (str), (n)) +#define strCaseCmp(s,str) strcasecmp(strBuf(s), (str)) +#define strNCaseCmp(s,str,n) strncasecmp(strBuf(s), (str), (n)) +#define strSet(s,ptr,ch) (s).nbuf[ptr-(s).nbuf] = (ch) +#define strCut(s,pos) (((s).len = pos) , ((s).nbuf[pos] = '\0')) +#define strCutPtr(s,ptr) (((s).len = (ptr)-(s).nbuf) , ((s).nbuf[(s).len] = '\0')) +#define strCat(s,str) stringAppend(&(s), (str), strlen(str)) +extern void stringInit(String * s, const char *str); +extern void stringLimitInit(String * s, const char *str, int len); +extern String stringDup(const String * s); +extern void stringClean(String * s); +extern void stringReset(String * s, const char *str); +extern void stringAppend(String * s, const char *buf, int len); +/* extern void stringAppendf(String *s, const char *fmt, ...) PRINTF_FORMAT_ARG2; */ + +extern const String StringNull; /* { 0, 0, NULL } */ + +#endif /* __LIBBUF_STRING__H_ */ Index: squid/src/String.c =================================================================== RCS file: /cvsroot/squid-sf//squid/src/String.c,v retrieving revision 1.7 retrieving revision 1.7.44.1 diff -u -r1.7 -r1.7.44.1 --- squid/src/String.c 20 May 2006 21:50:51 -0000 1.7 +++ squid/src/String.c 3 Dec 2007 07:59:25 -0000 1.7.44.1 @@ -1,6 +1,6 @@ /* - * $Id: String.c,v 1.7 2006/05/20 21:50:51 squidadm Exp $ + * $Id: String.c,v 1.7.44.1 2007/12/03 07:59:25 adri Exp $ * * DEBUG: section 67 String * AUTHOR: Duane Wessels @@ -32,80 +32,3 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. * */ - -#include "squid.h" - -static void -stringInitBuf(String * s, size_t sz) -{ - s->buf = memAllocString(sz, &sz); - assert(sz < 65536); - s->size = sz; -} - -void -stringInit(String * s, const char *str) -{ - assert(s); - if (str) - stringLimitInit(s, str, strlen(str)); - else - *s = StringNull; -} - -void -stringLimitInit(String * s, const char *str, int len) -{ - assert(s && str); - stringInitBuf(s, len + 1); - s->len = len; - xmemcpy(s->buf, str, len); - s->buf[len] = '\0'; -} - -String -stringDup(const String * s) -{ - String dup; - assert(s); - stringInit(&dup, s->buf); - return dup; -} - -void -stringClean(String * s) -{ - assert(s); - if (s->buf) - memFreeString(s->size, s->buf); - *s = StringNull; -} - -void -stringReset(String * s, const char *str) -{ - stringClean(s); - stringInit(s, str); -} - -void -stringAppend(String * s, const char *str, int len) -{ - assert(s); - assert(str && len >= 0); - if (s->len + len < s->size) { - strncat(s->buf, str, len); - s->len += len; - } else { - String snew = StringNull; - snew.len = s->len + len; - stringInitBuf(&snew, snew.len + 1); - if (s->buf) - xmemcpy(snew.buf, s->buf, s->len); - if (len) - xmemcpy(snew.buf + s->len, str, len); - snew.buf[snew.len] = '\0'; - stringClean(s); - *s = snew; - } -} Index: squid/src/globals.h =================================================================== RCS file: /cvsroot/squid-sf//squid/src/globals.h,v retrieving revision 1.31.8.2 retrieving revision 1.31.8.3 diff -u -r1.31.8.2 -r1.31.8.3 --- squid/src/globals.h 2 Dec 2007 06:46:41 -0000 1.31.8.2 +++ squid/src/globals.h 3 Dec 2007 07:59:25 -0000 1.31.8.3 @@ -1,6 +1,6 @@ /* - * $Id: globals.h,v 1.31.8.2 2007/12/02 06:46:41 adri Exp $ + * $Id: globals.h,v 1.31.8.3 2007/12/03 07:59:25 adri Exp $ * * * SQUID Web Proxy Cache http://www.squid-cache.org/ @@ -128,7 +128,6 @@ extern int store_hash_buckets; /* 0 */ extern hash_table *store_table; /* NULL */ extern dlink_list ClientActiveRequests; -extern const String StringNull; /* { 0, 0, NULL } */ extern const MemBuf MemBufNull; /* MemBufNULL */ extern int hot_obj_count; /* 0 */ extern const int CacheDigestHashFuncCount; /* 4 */ Index: squid/src/protos.h =================================================================== RCS file: /cvsroot/squid-sf//squid/src/protos.h,v retrieving revision 1.146.2.4.4.2 retrieving revision 1.146.2.4.4.3 diff -u -r1.146.2.4.4.2 -r1.146.2.4.4.3 --- squid/src/protos.h 2 Dec 2007 06:46:42 -0000 1.146.2.4.4.2 +++ squid/src/protos.h 3 Dec 2007 07:59:25 -0000 1.146.2.4.4.3 @@ -1,6 +1,6 @@ /* - * $Id: protos.h,v 1.146.2.4.4.2 2007/12/02 06:46:42 adri Exp $ + * $Id: protos.h,v 1.146.2.4.4.3 2007/12/03 07:59:25 adri Exp $ * * * SQUID Web Proxy Cache http://www.squid-cache.org/ @@ -1222,28 +1222,6 @@ extern void htcpSocketClose(void); #endif -/* String */ -#define strLen(s) ((/* const */ int)(s).len) -#define strBuf(s) ((const char*)(s).buf) -#define strChr(s,ch) ((const char*)strchr(strBuf(s), (ch))) -#define strRChr(s,ch) ((const char*)strrchr(strBuf(s), (ch))) -#define strStr(s,str) ((const char*)strstr(strBuf(s), (str))) -#define strCmp(s,str) strcmp(strBuf(s), (str)) -#define strNCmp(s,str,n) strncmp(strBuf(s), (str), (n)) -#define strCaseCmp(s,str) strcasecmp(strBuf(s), (str)) -#define strNCaseCmp(s,str,n) strncasecmp(strBuf(s), (str), (n)) -#define strSet(s,ptr,ch) (s).buf[ptr-(s).buf] = (ch) -#define strCut(s,pos) (((s).len = pos) , ((s).buf[pos] = '\0')) -#define strCutPtr(s,ptr) (((s).len = (ptr)-(s).buf) , ((s).buf[(s).len] = '\0')) -#define strCat(s,str) stringAppend(&(s), (str), strlen(str)) -extern void stringInit(String * s, const char *str); -extern void stringLimitInit(String * s, const char *str, int len); -extern String stringDup(const String * s); -extern void stringClean(String * s); -extern void stringReset(String * s, const char *str); -extern void stringAppend(String * s, const char *buf, int len); -/* extern void stringAppendf(String *s, const char *fmt, ...) PRINTF_FORMAT_ARG2; */ - /* * ipc.c */ Index: squid/src/squid.h =================================================================== RCS file: /cvsroot/squid-sf//squid/src/squid.h,v retrieving revision 1.36.24.2 retrieving revision 1.36.24.3 diff -u -r1.36.24.2 -r1.36.24.3 --- squid/src/squid.h 2 Dec 2007 06:46:42 -0000 1.36.24.2 +++ squid/src/squid.h 3 Dec 2007 07:59:25 -0000 1.36.24.3 @@ -1,6 +1,6 @@ /* - * $Id: squid.h,v 1.36.24.2 2007/12/02 06:46:42 adri Exp $ + * $Id: squid.h,v 1.36.24.3 2007/12/03 07:59:25 adri Exp $ * * AUTHOR: Duane Wessels * @@ -373,6 +373,11 @@ #include "../libcore/tools.h" #include "../libcore/debug.h" #include "../libcore/ctx.h" +#include "../libcore/syslog.h" + + +/* libbuf */ +#include "../libbuf/String.h" #include "Stack.h" Index: squid/src/structs.h =================================================================== RCS file: /cvsroot/squid-sf//squid/src/structs.h,v retrieving revision 1.158.2.5.4.1 retrieving revision 1.158.2.5.4.2 diff -u -r1.158.2.5.4.1 -r1.158.2.5.4.2 --- squid/src/structs.h 2 Dec 2007 02:01:34 -0000 1.158.2.5.4.1 +++ squid/src/structs.h 3 Dec 2007 07:59:25 -0000 1.158.2.5.4.2 @@ -1,6 +1,6 @@ /* - * $Id: structs.h,v 1.158.2.5.4.1 2007/12/02 02:01:34 adri Exp $ + * $Id: structs.h,v 1.158.2.5.4.2 2007/12/03 07:59:25 adri Exp $ * * * SQUID Web Proxy Cache http://www.squid-cache.org/ @@ -210,13 +210,6 @@ #endif -struct _String { - /* never reference these directly! */ - unsigned short int size; /* buffer size; 64K limit */ - unsigned short int len; /* current length */ - char *buf; -}; - struct _header_mangler { acl_access *access_list; char *replacement; Index: squid/src/typedefs.h =================================================================== RCS file: /cvsroot/squid-sf//squid/src/typedefs.h,v retrieving revision 1.43.2.3.4.1 retrieving revision 1.43.2.3.4.2 diff -u -r1.43.2.3.4.1 -r1.43.2.3.4.2 --- squid/src/typedefs.h 2 Dec 2007 02:01:35 -0000 1.43.2.3.4.1 +++ squid/src/typedefs.h 3 Dec 2007 07:59:25 -0000 1.43.2.3.4.2 @@ -1,6 +1,6 @@ /* - * $Id: typedefs.h,v 1.43.2.3.4.1 2007/12/02 02:01:35 adri Exp $ + * $Id: typedefs.h,v 1.43.2.3.4.2 2007/12/03 07:59:25 adri Exp $ * * * SQUID Web Proxy Cache http://www.squid-cache.org/ @@ -199,7 +199,6 @@ typedef struct _authConfig authConfig; typedef struct _cacheSwap cacheSwap; typedef struct _StatHist StatHist; -typedef struct _String String; typedef struct _MemMeter MemMeter; typedef struct _MemPoolMeter MemPoolMeter; typedef struct _MemPool MemPool;