Memory Pool in Nginx

Source: Internet
Author: User

Transferred from: http://www.cnblogs.com/sld666666/archive/2010/06/27/1766255.html

1. Why a memory pool is required

Why a memory pool is required.

A. Faster memory allocation (vs. malloc and free) in the application and release of a large amount of small chunks of memory

B. Reduce memory fragmentation and prevent memory leaks. 2. How the memory pool works

The memory pool principle is very simple, with the application of a large amount of memory to replace N more small memory block, when there is a need to malloc a piece

Relatively small memory is, directly take this large in-memory address to use.

Of course, the disadvantage of such processing is also obvious, the application of a large amount of memory will inevitably lead to a waste of memory space, but

Rather than frequent malloc and free, the cost of doing so is very small, which is typical with space-changing time.

A typical memory pool is shown in the following figure:

Figure one: A typical memory pool.

First define such a struct:

typedef struct MEMORYBLOCK
{
  char *data;			Data
  std::size_t datasize;	The total size
  std::size_t usedsize;	The size that has been used
  memoryblock*next;		
} Memoryblock;

A memory pool is a series of memory blocks. Call this memory pool-defined interface when memory is needed

GetMemory (), while the need to delete freememory ().

And what did GetMemory and Freememory do? GetMemory simply returns the address of the free space in the memory pool.

And Freememory did two things: one: Change the value of Usedsize, and two: reinitialize this memory area.

memory pools in 3.nginx structure representation of 3.1 Nginx memory pool

First, let's look at the definition of nginx memory pool:

struct ngx_pool_s {
ngx_pool_data_t D; Represents a data region
size_t Max; The memory pool can accommodate the size of the data
ngx_pool_t * CURRENT; The current memory pool block (the memory pool in Nginx is a series of memory Chiling)
ngx_chain_t * CHAIN; Mainly to connect the memory pool.
ngx_pool_large_t * LARGE; Chunk of data
ngx_pool_cleanup_t * cleanup; Cleanup function
ngx_log_t * LOG; Write log
};

The memory pool in Nginx is quite different from the normal one. The memory pool in Nginx is a chain list of n memory pools

, when a memory pool is full, the space is extracted from the next memory pool for use.

The definition for ngx_pool_data_t is very simple:

typedef struct {
U_char * LAST;
U_char * END;
ngx_pool_t * NEXT;
Ngx_uint_t failed;
} ngx_pool_data_t;

Where last represents the end of the data already used for the current data region.

End represents the end of the current memory pool.

Next represents the next memory pool, as previously mentioned, and then Nignx, when a memory pool space

When it is insufficient, it does not enlarge its space, but creates a new pool of memory to form a list of memory pools.

The number of times the failed flag failed when applying for memory.

It's very simple to understand the structure behind it.

Current represents a memory pool.

Chain represents a memory pool linked list.

Large represents large chunks of data.

The definition for ngx_pool_large_t is as follows:

struct NGX_POOL_LARGE_S {
ngx_pool_large_t * NEXT;
void * ALLOC;
};

The definition of this struct is also very simple. A pointer to a memory address already pointing to the next address.

This explains why you need to have large data blocks. When the memory size of a request is larger than the size of the memory pool,

malloc a large space, then the memory pool with a pointer to retain this address.


Cleanup maintains a cleanup function when the memory pool is destroyed.

typedef void (*NGX_POOL_CLEANUP_PT) (void *data); struct ngx_pool_cleanup_s {
Ngx_pool_cleanup_pt handler;
void * data;
ngx_pool_cleanup_t * NEXT;
};

NGX_POOL_CLEANUP_PT is a typical use of a function pointer,

In this result, the data pointers that need to be cleaned and the corresponding cleanup functions are saved so that the memory pool is destroyed

or other needs to clean up the memory pool, you can call the handler in this struct.

Here is a schematic diagram of an nginx memory pool that I drew.


< Figure 1. Ngx_pool structure > 3.2 nginx Memory pool source code Analysis

To understand a memory pool in general, you just need to know the creation of its pool, the allocation of memory, and the destruction of the pool. The following is an analysis

This 3 aspect of ngx_pool_t. Note: Some of these codes may differ from the source code in Ngx_pool, but the overall meaning

Absolutely the same, I modify, just for better analysis, for example, I will write all the log process has been removed. 3.2.1 Ngx_create_pool

Create a pool of memory

ngx_pool_t* Ngx_create_poo (size_t size)
{
	ngx_pool_t*		p;

	p = (ngx_pool_t*) malloc (size);
	if (!p) {
		return NULL;
	}
	
	Calculates the data region of the memory pool
	p->d->last = (u_char*) p + sizeof (ngx_pool_t);
	P->d->end = (u_char*) p + size;
	P->d->next = null;//Next memory pool
	p->d->failed = 0;

	Size = size-sizeof (ngx_pool_t);;
	P->max = size;//Max data
	
	//I'm still a single memory pool
	p->current = p;
	P->chain = NULL;
	Allocate large memory areas only when needed
	p->large = NULL;
	P->cleanup = NULL;

	return p;
}


The creation of nginx memory pool is very simple, request to open size of memory, assign it to ngx_poo_t. 3.2.2 Ngx_palloc

Allocates memory from the memory pool.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.