free the last allocation in a pool

Peter Leonov gojpeg at
Tue May 4 16:36:59 MSD 2010

On 04.05.2010, at 15:40, Denis F. Latypoff wrote:

> 04.05.10, 14:19, "Peter Leonov" <gojpeg at>:
>> Hi, again,
>> When a module needs some memory just for a temporary buffer, it comes to this:
>>   buf = ngx_pnalloc(pool, len);
>> When the work is done, it is good to free the memory ;)
>> As far as ngx_pfree() tries to free the “large” blocks only it can't be used for a small allocation.
>> Could you please tell me, is there a way to free the last small block of memory?
Thanks, Denis.

In that thread the ngx_pfree() was mentioned as the way to free memory.
And all the conversation ended up with the decision just to do not free the memory at all.

Now I'm testing this approach:

if (static_pool->d.last == (u_char *) buf + len)
	// reflects p->d.last = m + size; at ngx_palloc.c:159
	static_pool->d.last -= len;
	ngx_pfree(static_pool, buf);

It works well for small allocations.

> -- 
> br, Denis F. Latypoff.

More information about the nginx-devel mailing list