--------------------- PatchSet 10336 Date: 2007/12/28 07:50:21 Author: adri Branch: s27_adri Tag: (none) Log: Include a whole swath of string/buffer debugging magic so I can figure out where all the string/buffer allocations are coming from. This was initially to see if I were leaking all over the shop, but I'm not (phew!) The code has shown where the buffers and strings are being allocated from. Hopefully most of these will suddenly disappear the minute I get referencing in place. Members: libbuf/String.c:1.1.2.16->1.1.2.17 libbuf/String.h:1.1.2.20->1.1.2.21 libbuf/buf.c:1.1.2.8->1.1.2.9 libbuf/buf.h:1.1.2.6->1.1.2.7 Index: squid/libbuf/String.c =================================================================== RCS file: /cvsroot/squid-sf//squid/libbuf/Attic/String.c,v retrieving revision 1.1.2.16 retrieving revision 1.1.2.17 diff -u -r1.1.2.16 -r1.1.2.17 --- squid/libbuf/String.c 22 Dec 2007 14:55:17 -0000 1.1.2.16 +++ squid/libbuf/String.c 28 Dec 2007 07:50:21 -0000 1.1.2.17 @@ -1,6 +1,6 @@ /* - * $Id: String.c,v 1.1.2.16 2007/12/22 14:55:17 adri Exp $ + * $Id: String.c,v 1.1.2.17 2007/12/28 07:50:21 adri Exp $ * * DEBUG: section 67 String * AUTHOR: Duane Wessels @@ -61,13 +61,15 @@ } void -stringInit(String * s, const char *str) +stringInitInt(String * s, const char *str, const char *file, int line) { assert(s); if (str) stringLimitInit(s, str, strlen(str)); else *s = StringNull; + s->nb->create.file = file; + s->nb->create.line = line; } void @@ -81,14 +83,16 @@ } void -stringLimitInit(String * s, const char *str, int len) +stringLimitInitInt(String * s, const char *str, int len, const char *file, int line) { assert(s && str); stringInitBuf(s, len + 1); - buf_append(s->nb, str, len, BF_APPEND_NUL); + buf_append(s->nb, str, len, BF_NONE); //printf("len: %d, buflen: %d, size %d\n", len, buf_len(s->nb), s->nb->size); stringStats.create++; s->len = len; + s->nb->create.file = file; + s->nb->create.line = line; } String Index: squid/libbuf/String.h =================================================================== RCS file: /cvsroot/squid-sf//squid/libbuf/Attic/String.h,v retrieving revision 1.1.2.20 retrieving revision 1.1.2.21 diff -u -r1.1.2.20 -r1.1.2.21 --- squid/libbuf/String.h 22 Dec 2007 14:55:17 -0000 1.1.2.20 +++ squid/libbuf/String.h 28 Dec 2007 07:50:22 -0000 1.1.2.21 @@ -33,8 +33,13 @@ #define strGetPos(s, o) *(strBuf(s) + o) #define strIsNull(s) ((s).nb == NULL) #define strIsNotNull(s) ((s).nb != NULL) -extern void stringInit(String * s, const char *str); -extern void stringLimitInit(String * s, const char *str, int len); + +#define stringInit(a, b) stringInitInt((a), (b), __FILE__, __LINE__) +extern void stringInitInt(String * s, const char *str, const char *, int); + +#define stringLimitInit(a, b, c) stringLimitInitInt((a), (b), (c), __FILE__, __LINE__) +extern void stringLimitInitInt(String * s, const char *str, int len, const char *, int); + extern String stringDup(const String * s); extern void stringClean(String * s); extern void stringReset(String * s, const char *str); Index: squid/libbuf/buf.c =================================================================== RCS file: /cvsroot/squid-sf//squid/libbuf/Attic/buf.c,v retrieving revision 1.1.2.8 retrieving revision 1.1.2.9 diff -u -r1.1.2.8 -r1.1.2.9 --- squid/libbuf/buf.c 27 Dec 2007 12:59:08 -0000 1.1.2.8 +++ squid/libbuf/buf.c 28 Dec 2007 07:50:24 -0000 1.1.2.9 @@ -16,6 +16,8 @@ static int buf_configured = 0; static MemPool *buf_pool = NULL; +dlink_list buf_active_list; + void buf_init(void) { @@ -38,7 +40,8 @@ * to handle it.. */ debug (85, 5) ("buf_changesize: %p: size %d -> %d\n", b, b->size, newsize); - assert(newsize >= b->size); + if (newsize <= b->size) + return 1; assert(b->flags.isfinal == 0); /* can't reallocate a fixed buffer! */ if (b->flags.isfinal != 0) @@ -70,20 +73,23 @@ buf_t * -buf_create(void) +buf_create_int(const char *file, int line) { buf_t *b; b = memPoolAlloc(buf_pool); if (! b) return NULL; + dlinkAddTail(b, &b->node, &buf_active_list); debug (85, 5) ("buf_create: %p\n", b); buf_ref(b); + b->create.file = file; + b->create.line = line; return b; } buf_t * -buf_create_size(int size) +buf_create_size_int(int size, const char *file, int line) { buf_t *b; b = buf_create(); @@ -93,23 +99,28 @@ buf_deref(b); return NULL; } + b->create.file = file; + b->create.line = line; return b; } buf_t * -buf_create_const(const void *data, size_t len) +buf_create_const_int(const void *data, size_t len, const char *file, int line) { buf_t *b; - b = malloc(sizeof(buf_t)); + b = memPoolAlloc(buf_pool); if (! b) return NULL; bzero(b, sizeof(*b)); + dlinkAddTail(b, &b->node, &buf_active_list); debug(85, 5) ("buf_create: %p\n", b); b->b = (char *)data; b->len = b->size = b->sofs = len; b->flags.isconst = 1; b->flags.isfinal = 1; + b->create.file = file; + b->create.line = line; buf_ref(b); return b; } @@ -147,6 +158,7 @@ if (!b->flags.isconst) { free(b->b); b->b = NULL; } + dlinkDelete(&b->node, &buf_active_list); memPoolFree(buf_pool, b); return NULL; } @@ -179,18 +191,23 @@ int buf_append(buf_t *b, const void *src, size_t len, buf_flags_t flags) { - if (!buf_changesize(b, b->size + len + 1)) { + /* only grow the buffer if we need to */ + int bl = 0; + if (flags & BF_APPEND_NUL) + bl = 1; + + if (!buf_changesize(b, b->len + len + bl)) { /* tsk! */ return -1; } - assert(b->len + len < b->size); + assert(b->len + len <= b->size); memcpy(b->b + b->len, src, len); b->len += len; #if 0 if (flags & BF_APPEND_NUL) b->b[b->len] = '\0'; #endif - b->b[b->len] = 'A'; + //b->b[b->len] = 'A'; return len; } Index: squid/libbuf/buf.h =================================================================== RCS file: /cvsroot/squid-sf//squid/libbuf/Attic/buf.h,v retrieving revision 1.1.2.6 retrieving revision 1.1.2.7 diff -u -r1.1.2.6 -r1.1.2.7 --- squid/libbuf/buf.h 27 Dec 2007 12:59:09 -0000 1.1.2.6 +++ squid/libbuf/buf.h 28 Dec 2007 07:50:24 -0000 1.1.2.7 @@ -6,12 +6,17 @@ int len; int size; int sofs; /* how much of the buffer can't be changed */ + dlink_node node; struct { char isactive:1; char isfinal:1; char isfreed:1; char isconst:1; } flags; + struct { + const char *file; + int line; + } create, ref, deref; int nref; }; @@ -22,10 +27,13 @@ typedef struct _buf buf_t; +#define buf_create() buf_create_int(__FILE__, __LINE__) +#define buf_create_size(a) buf_create_size_int(a, __FILE__, __LINE__) +#define buf_create_const(a, b) buf_create_const_int(a, b, __FILE__, __LINE__) extern void buf_init(void); -extern buf_t * buf_create(void); -extern buf_t * buf_create_size(int len); -extern buf_t * buf_create_const(const void *data, size_t len); +extern buf_t * buf_create_int(const char *file, int line); +extern buf_t * buf_create_size_int(int len, const char *file, int line); +extern buf_t * buf_create_const_int(const void *data, size_t len, const char *file, int line); extern buf_t * buf_ref(buf_t *buf); extern buf_t * buf_deref(buf_t *buf); extern void buf_free(buf_t *buf); @@ -42,6 +50,7 @@ */ #define buf_buf(buf) ((buf)->b) #define buf_len(buf) ((buf)->len) +#define buf_capacity(buf) ((buf)->size) #define buf_refcnt(buf) ((buf)->nref) #define buf_remainder(buf) ( (buf)->size - (buf)->len ) @@ -64,4 +73,6 @@ static inline int buf_isfull(buf_t *b) { return (b->size == b->len); } +extern dlink_list buf_active_list; + #endif