[DEV] nginx ngx_hash_t usage, and other questions...

Igor Sysoev is at rambler-co.ru
Wed Sep 26 13:08:16 MSD 2007

On Tue, Sep 25, 2007 at 10:33:12AM +0200, Brice Figureau wrote:

> > For dynamic data I use rbtree.
> OK, it seems that what you are doing in the limit zone module is almost
> what I planned to do. I think I'll study this code in more details.
> You are placing the rbtree in this module in a shm segment. Is it to
> share it between the possibly multiple workers ?


> I was wondering how I could share my global data structure between the
> different workers...

See limit zone module.

A zone in nginx is shared memory segment created by ngx_shared_memory_add().
A zone has name and is inherited between reconfigurations if its size
is not changed. nginx has simple slab allocator to alloc/free memory in zone.

> > > I also have a few more general or basic questions:
> > > 
> > > 1) There are a lot of ngx_str_t used, but all the ngx_string.c functions
> > > are using u_char* . Does it mean the strings are always null terminated
> > > and the ngx_str_t len parameter is used for something else ?
> > > So to use ngx_strcmp with 2 ngx_str_t, I just have to call it with the value?
> > 
> > No, ngx_str_t may be null terminated (and len does count null in the case),
> > but in general it is not null terminated.
> OK, so how do compare strings for instance ?
> ngx_strcmp is acting on null terminated strings, but since I don't know
> beforehand if the string is terminated or not...
> Maybe I have to use ngx_memn2cmp ?

No. use ngx_strNcmp(). The usual pattern to compare two ngx_str_t's is:

   if (s1->len == s2->len && ngx_strncmp(s1->data, s2->data, s1->len) == 0) {

Sometime, if I sure that both ngx_str_t are null terminated strings, then
I use ngx_strcmp() in this pattern. But I always test the lengths before.

> > > 2) Can I register an handler or a phase handler that will be triggered at
> > > the end of a request (and if yes how) ?
> > > This is to do the cleanup in my hash table containing the uploading requests.
> > 
> > Use ngx_pool_cleanup_add(r->pool, size).
> OK, then the handler will be called when the pool is deallocated, is
> that right?
> And the request pool is deallocated when the request is over.


> I don't have to add any memory to the request pool in my case; is it
> possible to pass 0 as the size ?

Yes. Size is intended to allocate some additional memory for callback context.

Igor Sysoev

More information about the nginx mailing list