From 84f393a6cf75df90c7836fc2f3356a5543983779 Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Tue, 22 Mar 2005 23:59:38 +0000 Subject: Fix memory and buffer problems. --- common/buffer.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 52 insertions(+), 11 deletions(-) (limited to 'common/buffer.c') diff --git a/common/buffer.c b/common/buffer.c index a217298..3d427e5 100644 --- a/common/buffer.c +++ b/common/buffer.c @@ -43,6 +43,7 @@ #include #include #include +#include /* ----------------------------------------------------------------------- * Memory Buffer @@ -74,6 +75,19 @@ typedef struct ha_buffer_internal } internal_t; +#ifdef BUF_DEBUG +void buffer_dump(ha_buffer_t* buf) +{ + internal_t* itl; + int i = 0; + + fprintf(stderr, "Buffer DUMP ---------------------------\n"); + for(itl = buf->_ft; itl; itl = itl->next, i++) + fprintf(stderr, "itl %d: 0x%08X bytes\n", i, itl->end - (char*)itl); + fprintf(stderr, "---------------------------------------\n"); +} +#endif /* BUF_DEBUG */ + void buffer_bump(ha_buffer_t* buf, int count) { int allocated; @@ -85,6 +99,7 @@ void buffer_bump(ha_buffer_t* buf, int count) return; ASSERT(BUF_DELTA < BUF_INITIAL); + ASSERT(buf->_dt->next == NULL); /* Handle this common case first for efficiency */ if(buf->_rp + (count + BUF_DELTA) < buf->_dt->end) @@ -94,6 +109,10 @@ void buffer_bump(ha_buffer_t* buf, int count) if(BUF_IN_JOIN(buf) && (buf->_rp + count < buf->_dt->end)) return; +#ifdef BUF_DEBUG + buffer_dump (buf); +#endif + /* Okay now we know we have to extend */ allocated = buf->_dt->end - (char*)(buf->_dt); ASSERT(allocated > 0 && (allocated % BUF_INITIAL) == 0); @@ -133,7 +152,7 @@ void buffer_bump(ha_buffer_t* buf, int count) /* Copy the memory and blank out old */ memcpy(beg, buf->_pp, diff); #ifdef _DEBUG - FILL_BETWEEN(buf->_pp, buf->_rp, 0xDD); + FILL_BETWEEN(buf->_pp, buf->_rp, 0xCD); #endif buf->_pp = beg; @@ -165,27 +184,47 @@ void ha_bufinit(ha_buffer_t* buf) FILL_BETWEEN(INTERNAL_DATA(buf->_ft), buf->_ft->end, 0xCD); #endif - ha_bufreset(buf); - } + buf->_dt = buf->_ft; + ha_bufreset(buf); + } } void ha_bufreset(ha_buffer_t* buf) { -#ifdef _DEBUG - internal_t* intl; -#endif + internal_t* itl; + internal_t* next; ASSERT(buf); ASSERT(buf->_ft); + if(ha_buferr(buf)) + return; + + /* We keep the last buffer, and free the rest */ + itl = buf->_ft; + buf->_ft = buf->_dt; + buf->_rp = buf->_pp = (char*)INTERNAL_DATA(buf->_ft); + +#ifdef BUF_DEBUG + fprintf(stderr, "RESET BUFFER: %d\n", buf->_ft->end - (char*)buf->_ft); +#endif + + for( ; itl != buf->_ft; itl = next) + { + ASSERT(itl != NULL); + next = itl->next; #ifdef _DEBUG - /* Go through all the buffers and set them to 0xCD */ - for(intl = buf->_ft; intl; intl = intl->next) - FILL_BETWEEN(INTERNAL_DATA(intl), intl->end, 0xCD); + FILL_BETWEEN(INTERNAL_DATA(itl), itl->end, 0xDD); +#endif +#ifdef BUF_DEBUG + fprintf(stderr, "FREEING: %d\n", itl->end - (char*)itl); #endif + free(itl); + } - buf->_dt = buf->_ft; - buf->_rp = buf->_pp = (char*)INTERNAL_DATA(buf->_ft); +#ifdef _DEBUG + FILL_BETWEEN(INTERNAL_DATA(buf->_ft), buf->_ft->end, 0xCD); +#endif } void ha_buffree(ha_buffer_t* buf) @@ -199,7 +238,9 @@ void ha_buffree(ha_buffer_t* buf) for(intl = buf->_ft; intl; intl = next) { next = intl->next; +#ifdef _DEBUG FILL_BETWEEN(INTERNAL_DATA(intl), intl->end, 0xDD); +#endif free(intl); } -- cgit v1.2.3