Linux kernel data structure bitmap learning

Source: Internet
Author: User

1#include <stdio.h>2#include <stdlib.h>3 4 #defineMax_prio 100005 #defineBits_per_long 326 #defineDiv_round_up (N,d) (((n) + (d)-1)/(d))7 8 #defineBIT (NR) (1UL << (NR))9 #defineBit_mask (NR) (1UL << (NR)% Bits_per_long))Ten #defineBit_word (NR) (NR)/Bits_per_long) One #defineBits_per_byte 8 A  - #defineBits_to_longs (NR) div_round_up (NR, Bits_per_byte * sizeof (LONG)) -  the #defineDeclare_bitmap (Name,bits) -UnsignedLongname[bits_to_longs (BITS)] -  - StaticInlinevoid__set_bit (intNrvolatileUnsignedLong*addr) + { -UnsignedLongMask =Bit_mask (NR); +UnsignedLong*p = ((unsignedLong*) addr) +Bit_word (NR); A  at*p |=Mask; - } -  - StaticInlinevoid__clear_bit (intNrvolatileUnsignedLong*addr) - { -UnsignedLongMask =Bit_mask (NR); inUnsignedLong*p = ((unsignedLong*) addr) +Bit_word (NR); -  to*p &= ~Mask; + } -  the StaticInlinevoid__change_bit (intNrvolatileUnsignedLong*addr) * { $UnsignedLongMask =Bit_mask (NR);Panax NotoginsengUnsignedLong*p = ((unsignedLong*) addr) +Bit_word (NR); -  the*p ^=Mask; + } A  the StaticInlineint__test_and_set_bit (intNrvolatileUnsignedLong*addr) + { -UnsignedLongMask =Bit_mask (NR); $UnsignedLong*p = ((unsignedLong*) addr) +Bit_word (NR); $UnsignedLongOld = *p; -  -*p = Old |Mask; the     return(Old & mask)! =0; - }Wuyi  the StaticInlineint__test_and_clear_bit (intNrvolatileUnsignedLong*addr) - { WuUnsignedLongMask =Bit_mask (NR); -UnsignedLong*p = ((unsignedLong*) addr) +Bit_word (NR); AboutUnsignedLongOld = *p; $  -*p = old & ~Mask; -     return(Old & mask)! =0; - } A  + StaticInlineint__test_and_change_bit (intNrvolatileUnsignedLong*addr) the { -UnsignedLongMask =Bit_mask (NR); $UnsignedLong*p = ((unsignedLong*) addr) +Bit_word (NR); theUnsignedLongOld = *p; the  the*p = old ^Mask; the     return(Old & mask)! =0; - } in  the StaticInlineintTest_bit (intNrConst volatileUnsignedLong*addr) the { About     return 1UL& (Addr[bit_word (NR)] >> (NR & (bits_per_long-1))); the } the  the #ifndef container_of + /** - * Container_of-cast A member of a structure out to the containing structure the * @ptr: The pointer to the member.Bayi * @type: The type of the container struct this was embedded in. the * @member: The name of the member within the struct. the  * -  */ - #defineContainer_of (PTR, type, member) ({ the     Const typeof((Type *)0)->member) * __mptr =(PTR);  the(Type *) ((Char*) __mptr-Offsetof (type, member)); }) the #endif the  - #defineList_entry (PTR, type, member) container_of (PTR, type, member) the  the int_tmain (intARGC, _tchar*argv[]) the {94 Declare_bitmap (Bitmap1, Max_prio); the Declare_bitmap (BITMAP2, Max_prio); the     inti; the 98     /*00000000-99999999 using a bitmap*/ About     intNUM1 =9999; -     intnum2 =9999;101 102     Charbuff[ +] = {0};103 104      for(i =0; i < Max_prio; i++) { the         106 __clear_bit (i, BITMAP1);107 __clear_bit (i, BITMAP2);108     }109  the __set_bit (NUM1, bitmap1);111 __set_bit (num2, BITMAP2); the 113printf"Bitmap size =%d\n",sizeof(BITMAP1)/sizeof(Long)); theprintf"bitmap =%d\n", Test_bit (NUM1, Bitmap1)); theprintf"bitmap =%d\n", Test_bit (num2, BITMAP2)); theprintf"atoi:%d\n", Atoi ("0010"));117printf"%4d%4d\n", NUM1, num2);118 119sprintf (Buff,"%.4d%.4d\n", NUM1,1); - printf (buff);121     return 0;122}

Linux kernel data structure bitmap learning

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.