Squid-2.6 source code explanation

Source: Internet
Author: User
Squid-2.6 source code explanation-Linux general technology-Linux programming and kernel information. The following is a detailed description. 1. brk/sbrk system call
Function Description:
Changes the Data Segment Size of a process. When the system has enough memory and the process does not exceed the maximum data segment allowed.

Usage:
# Include
Int brk (void * end_data_segment );
Void * sbrk (intptr_t increment );

Parameters:
End_data_segment: the end point of the Data Segment.
Increment: the amount of data segments that will be added to the current process

Return description:
When the execution is successful, brk () returns 0, and sbrk () returns the pointer to the starting point of the new region. -1 is returned for failure, and errno is set to ENOMEM: insufficient core memory

2. The system calls mallopt (int parameter_number, int parameter_value)
Function Description:
Use mallopt to adjust the value of some parameters (malloc. h), such as M_TRIM_THRESHOLD (a threshold value returned to the OS Memory) and M_MMAP_THRESHOLD (memory allocation requests greater than this value are called by the mmap system.

In fact, the implementation of malloc is not simply calling brk/sbrk. In order to consider the efficiency, glibc has three levels of optimization measures, namely:
Small memory allocation is implemented through the fastbin linked list;
Generally, memory is allocated on the stack. This region is called memory arena;
Memory with a relatively large value (greater than the value set by M_MMAP_THRESHOLD) is allocated by mmap. The advantage of using mmap is that it is immediately handed back to the system after free, instead of being locked, wasting memory;
When a program calls free, glibc does not call sbrk every time. On the contrary, glibc puts it in a temporary memory pool to provide the next malloc operation, in this way, the memory allocation efficiency can be optimized.
The memory allocation control function is mallopt. Its prototype is as follows:
Int mallopt (int cmd, int _ val );
The cmd value is:
M_TRIM_THRESHOLD-1
M_TOP_PAD-2
M_MMAP_THRESHOLD-3
M_MMAP_MAX-4
M_CHECK_ACTION-5
M_PERTURB-6
Its meaning is as follows:
M_TRIM_THRESHOLD
When the memory that can be released is accumulated to this value, the actual release (sbrk) operation is performed;
M_TOP_PAD
The additional length of the header during memory allocation. If the program calls malloc to allocate 1024 k of memory, but glibc does not allocate only k, more memory needs to be allocated (part of memory is used for memory allocation and management of glibc itself ).
M_MMAP_THRESHOLD
If the memory to be allocated exceeds this value, glibc uses mmap to allocate memory.
M_MMAP_MAX
Used to control the maximum number of mmaps;
Instance: mallopt-trim.c

In fact, the above parameters can be set through the corresponding environment variables without modifying the program. For example:
MALLOC_TRIM_THRESHOLD _= 1024./program

3. Description of main files
Structs. h --- main data structure definition: linked list, SSL, memory, authentication, Http-related structure, memory object, etc.
Mem. c ---
MemBuf. c ---
Leakfinder. c ---

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.