ngx_list_free

Y
  • 18 Apr '23
Hi

Ngx_list_t   have create api but no free_list api

Why?

I wrote my own free_list,  is anyone else   face this  issue?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx/attachments/20230418/28a3663e/attachment.htm>
M
  • 18 Apr '23
Hello!

On Tue, Apr 18, 2023 at 06:14:46AM +0000, Yuval Abadi via nginx wrote:

> Ngx_list_t   have create api but no free_list api
> 
> Why?
> 
> I wrote my own free_list,  is anyone else   face this  issue?

Much like many things in nginx, ngx_list_t structure is an 
append-only structure, and it is expected to be used as a list of 
items being added.  Most notably, ngx_list_t is used for request 
headers (these are added to the list when they are parsed), and 
response headers (these are added when they are generated 
internally).

If you have to remove an item for some reason (which shouldn't be 
common case for legitimate usage, but might happen to be needed 
occasionally), the common approach is to use a "skip" flag in the 
structures being listed.  For example, response headers are 
cleared by setting the "hash" field to 0, so such headers are 
ignored by the code iterating over the list.

If you need something more dynamic in your code, ngx_list_t might 
not be the right structure to use.  Something like ngx_queue_t or 
ngx_rbtree_t might be a better choice.

-- 
Maxim Dounin
http://mdounin.ru/
Y
  • 18 Apr '23
Thanks

I just need  list   not queue,

And I want to free the whole list , not one node.

-----Original Message-----
From: nginx <nginx-bounces at nginx.org> On Behalf Of Maxim Dounin
Sent: Tuesday, 18 April 2023 15:36
To: Yuval Abadi via nginx <nginx at nginx.org>
Subject: Re: ngx_list_free

EXTERNAL MAIL: nginx-bounces at nginx.org

Hello!

On Tue, Apr 18, 2023 at 06:14:46AM +0000, Yuval Abadi via nginx wrote:

> Ngx_list_t   have create api but no free_list api
>
> Why?
>
> I wrote my own free_list,  is anyone else   face this  issue?

Much like many things in nginx, ngx_list_t structure is an append-only structure, and it is expected to be used as a list of items being added.  Most notably, ngx_list_t is used for request headers (these are added to the list when they are parsed), and response headers (these are added when they are generated internally).

If you have to remove an item for some reason (which shouldn't be common case for legitimate usage, but might happen to be needed occasionally), the common approach is to use a "skip" flag in the structures being listed.  For example, response headers are cleared by setting the "hash" field to 0, so such headers are ignored by the code iterating over the list.

If you need something more dynamic in your code, ngx_list_t might not be the right structure to use.  Something like ngx_queue_t or ngx_rbtree_t might be a better choice.

--
Maxim Dounin
http://mdounin.ru/
_______________________________________________
nginx mailing list
nginx at nginx.org
https://mailman.nginx.org/mailman/listinfo/nginx
M
  • 18 Apr '23
Hello!

On Tue, Apr 18, 2023 at 03:12:08PM +0000, Yuval Abadi via nginx wrote:

> Thanks
> 
> I just need  list   not queue,

Queue is a double-linked list.

> And I want to free the whole list , not one node.

In nginx, memory allocations use pools, and in most cases you 
cannot free individual allocations.  The only options is to 
destroy the whole pool.

For example, this naturally works with requests: all allocations 
related to a request are done from the request pool, and are 
automatically freed when the request is freed and its pool is 
destroyed.

See here for more details about memory pools:

http://nginx.org/en/docs/dev/development_guide.html#pool

-- 
Maxim Dounin
http://mdounin.ru/