Hi,
I have wrote a module, which has an output filter. The filter collects all
the buckets of a request and does some processing, then it produces a new
brigade containing the new output data and pass it to the next filter in the
filter chains.
The output filter code is like this:
static apr_status_t TestOutFilter(ap_filter_t *f,
apr_bucket_brigade *pbbIn)
{
request_rec *r = f->r;
conn_rec *c = r->connection;
OutContext *pCtx;
if(!(pCtx = f->ctx)){
f->ctx = pCtx=apr_palloc(r->pool,sizeof *pCtx);
pCtx->pbbTmp = apr_brigade_create(r->pool,c->bucket_alloc);
}
apr_bucket_brigade *pbbOut = pCtx->pbbTmp;
while(!APR_BRIGADE_EMPTY(pbbIn)){
apr_bucket *pbktIn,*tmpbucket;
pbktIn=APR_BRIGADE_FIRST(pbbIn);
APR_BUCKET_REMOVE(pbktIn);
APR_BRIGADE_INSERT_TAIL(pbbOut, pbktIn);
if(APR_BUCKET_IS_FLUSH(pbktIn))
continue;
if(APR_BUCKET_IS_EOS(pbktIn)) {
/* we have all the buckets in pbbTmp;
do some processing and create a new bucket
outputbuffer contains the new data to pass to next filter
*/
tmpbucket=apr_bucket_heap_create(outputbuffer,
lenofbuffer,0,c->bucket_alloc);
APR_BRIGADE_INSERT_TAIL(pbbOut, tmpbucket);
}
APR_BRIGADE_INSERT_TAIL(pbbOut, pbktIn);
ap_remove_output_filter(f);
return ap_pass_brigade(f->next,pbbOut);
}
}
return APR_SUCCESS;
}
The strange thing is when I use "/usr/local/apache2/bin/apxs -i -a -c -Wc,O0
-Wc,-ggdb testmodule.c" to compile the module, it works well, but if I
"/usr/local/apache2/bin/apxs -i -a -c -Wc,O2 -Wc,-ggdb testmodule.c", it
has death cycle in the function. Here '-Wc' means add gcc flag, '-Wc,O0'
means 'gcc -O0' ,which is no optimization. '-Wc,O2' is the default value if
it's not specified.
I use gdb to debug this, and find that in "-Wc,O2" case, the function has
death loop in the while loop. The pbbIn and pbbOut values are not changed
after a loop, which means:
"pbktIn=APR_BRIGADE_FIRST(pbbIn);
APR_BUCKET_REMOVE(pbktIn);
APR_BRIGADE_INSERT_TAIL(pbbOut, pbktIn);"
aren't executed correctly or even optimized out.
By the way, even "-Wc,O1" flat works correctly.
I can't figure out the reason and how to solve it.
Any one knows something about this?
Thanks!