Summary of memory alignment issues by memory pool

Source: Internet
Author: User

This problem is indeed quite basic. In order to give yourself a summary, this article is also a note record and strives to be the most streamlined.

Structure alignment keywords:

Alignment coefficient (also called alignment modulus): This coefficient is changed using the pre-compiled command # pragma pack (N), n =, 16.

"Alignment rules": The data members are aligned in two steps, and the overall alignment is as follows:

1: Data member alignment: perform the following operations according to the value specified by # pragma pack and the smaller value in the length of the data member.

Formula: min (n, sizeof (curr_mem ))

2: The overall alignment is performed according to the value specified by # pragma pack and the smaller value in the maximum data member length of the structure (or union ).

Formula: min (n, max (sizeof (mem1), sizeof (MEM2), sizeof (mem3 )......))

Example:

2-byte alignment (# pragma pack (2 ))

Result: sizeof (struct test_t) = 10

Analysis process:

Data member alignment:

# Pragma pack (2) structTest_t {int A;/* length 4> 2 aligned by 2; Start offset = 0; storage location range [0, 3] */Char B; /* length 1 <2 aligned by 1; Start offset = 4; storage location range [4] */short C;/* length 2 = 2 aligned by 2; start offset = 6; storage location range [6, 7] */Char D;/* length 1 <2 aligned by 1; Start offset = 8; storage location range [8] */}; # Pragma pack ()

Overall alignment:

Alignment according to min (2, (max (INT, short, char) Results

 

The starting address in the memory pool is aligned:

# Define ngx_alignment sizeof (unsigned long)/* platform word */

# Define ngx_align_ptr (P, )\
(U_char *) (uintptr_t) (p) + (uintptr_t) A-1 ))&~ (Uintptr_t) A-1 ))

Call: ngx_align_ptr (PTR, ngx_alignment)

The detailed process is not detailed. There are several key points:

1: role of "word" in computer systems (Multi-read in-depth understanding of computers)

2: currently on Linux, the pointer type is the same as that of unsigned long.

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.