StrinBuffer now can work both with and without memory context
authorteodor <teodor>
Wed, 17 Mar 2010 15:59:44 +0000 (15:59 +0000)
committerteodor <teodor>
Wed, 17 Mar 2010 15:59:44 +0000 (15:59 +0000)
tmalloc.c

index 8b16322..2d45bc6 100644 (file)
--- a/tmalloc.c
+++ b/tmalloc.c
@@ -298,11 +298,15 @@ mcnstrdup(MemoryContext *cntx, char *src, int len) {
 }
 
 /*********StringBuffer********/
+
+#define SBALLOC(s)             ( ( (s)->mc ) ? mcalloc((s)->mc, (s)->len) : tmalloc((s)->len) )
+#define SBREALLOC(s)   ( ( (s)->mc ) ? mcrealloc((s)->buf, (s)->len) : trealloc((s)->buf, (s)->len) )
+
 StringBuffer*
 initStringBuffer(StringBuffer* state, MemoryContext *mc, int initsize) {
     state->len = (initsize>0) ? initsize : 1024;
        state->mc = mc;
-       state->ptr = state->buf = (char*)mcalloc(state->mc, state->len);
+       state->ptr = state->buf = (char*)SBALLOC(state);
        *(state->ptr) ='\0';
 
        return state;
@@ -323,7 +327,7 @@ appendStringBuffer( StringBuffer *state, char *string, int stringlen) {
                        int     diff = state->ptr - state->buf;
 
                        state->len *= 2;
-                       state->buf = (char*)mcrealloc( (void*)state->buf, state->len );
+                       state->buf = (char*)SBREALLOC(state);
                        state->ptr = state->buf + diff;
        }
 
@@ -354,7 +358,7 @@ printStringBuffer( StringBuffer *state, const char *format, ...) {
                        state->len *= 2;
                } while( state->ptr - state->buf + printedlen >= state->len );
 
-               state->buf = (char*)mcrealloc( (void*)state->buf, state->len );
+               state->buf = (char*)SBREALLOC(state);
                state->ptr = state->buf + curlen;
                va_start(args, format);
                printedlen = vsnprintf(state->ptr, printedlen+1, format, args);