Principles behind _ ALIGN Macros in Linux

Source: Internet
Author: User
The principle behind the _ ALIGN macro in Linux-general Linux technology-Linux programming and kernel information. The following is a detailed description. 1. Introduction
Int;
Int size = 8;

What should I do if I set a to an integer multiple of (size = 8) to binary? That is to make this number represent the last three digits of the binary value as 0.
To achieve this goal, we only need to carry out and calculate the following number:
11111111 11111111 11111111 11111000
The above number is actually ~ (Size-1), which can be called the size alignment mask size_mask.

In this case, we can find the maximum multiple of 8, which is smaller than a. If we want to increase the number to 8, will it be enough?
However, if a is a multiple of 8, isn't it wrong to add 8, so add size-1 on the basis of a, and then use the alignment mask with size for calculation.

In this way, we can define the following macro to calculate the two values before and after a number a is multiples of size:
# Define alignment_down (a, size) (&(~ (Size-1 )))
# Define alignment_up (a, size) (a + size-1 )&(~ (Size-1 )))

For example:
A = 0, size = 8, then alignment_down (a, size) = 0, alignment_up (a, size) = 0.
A = 6, size = 8, then alignment_down (a, size) = 0, alignment_up (a, size) = 8.
A = 8, size = 8, then alignment_down (a, size) = 8, alignment_up (a, size) = 8.
A = 14, size = 8, then alignment_down (a, size) = 8, alignment_up (a, size) = 16.

Note: The size should be the N power of 2, that is, 2, 4, 8, 16, 32, 64,128,256,102 4, 2048,409 6 .....

2. Applications in linux
The above calculation method is often seen in linux and other code. The following are several examples.

Example 1: When an addr address is allocated, use the _ ALIGN macro to ALIGN the address with a multiple of size and obtain a value greater than addr:
/* Align addr on a size boundary-adjust address up if needed -- Cort */
# Define _ ALIGN (addr, size) (addr) + (size)-1 )&(~ (Size)-1 )))

Example 2: macros related to page alignment
# Define PAGE_SIZE 4096
# Define PAGE_MASK (~ (PAGE_SIZE-1 ))
/* To align the pointer to the (next) page boundary */
# Define PAGE_ALIGN (addr) + PAGE_SIZE-1) & PAGE_MASK)

Example 3: Macro related to alignment during skb allocation
# Define SKB_DATA_ALIGN (X) + (SMP_CACHE_BYTES-1 ))&~ (SMP_CACHE_BYTES-1 ))

Struct sk_buff * _ alloc_skb (unsigned int size, gfp_t gfp_mask, int fclone)
{
......

/* Get the HEAD */
Skb = kmem_cache_alloc (cache, gfp_mask &~ _ GFP_DMA );
If (! Skb)
Goto out;

/* Get the DATA. Size must match skb_add_mtu ().*/
Size = SKB_DATA_ALIGN (size );
Data = kmalloc_track_caller (size + sizeof (struct skb_shared_info), gfp_mask );
If (! Data)
Goto nodata;
......
}

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.