WiFiDog Authentication Lighttpd1.4.20 Source Code Analysis Bitset.c (h) Use of--------bit collections

Source: Internet
Author: User
Tags bit set bitset

Use a bit to represent the two states of an event, which is to save memory and increase the speed of the operation. In Lighttpd, a BITSET data structure is provided to manage the use of a bit set.
In bitset.h, the data structure of a bit set is defined as follows:

struct {            *bits;            size_t Nbits;} Bitset;

Bits points to an array of type size_t, storing the bit collection. The size_t type is usually defined as an unsigned integer (int or long) whose length is related to a specific machine, and the reader can refer to the relevant information. Nbits records the number of bits in the Bitset. The diagram is as follows:

+-------------+| Bitset structure     |+-------------+             +-----------------------------+|   Bits           |   --------> | | | | | | | | | | | | | | |  |+-------------+             +-----------------------------+|  Ten   | +-------------+

In order to improve the speed of operation, the main operation with Bitset has four macros to achieve:
The actions of each macro are described in the comments.

Code//calculates how many bits a size_t type has. //Char_bit indicates how much a CHAR type is, defined in/usr/include/limits.h, and defined as 8 in my machine.#defineBitset_bits \(Char_bit*sizeof(size_t))/** * Get a size_t type of mask with POS location of 1, other locations are 0. * The POS position is the position of this bit in the Bitset, so a bitset_bits is the position in the size_t. */#defineBitset_mask (POS) \((size_t)1) << ((POS)%bitset_bits))/** * Calculates the position of the POS bit in the BITS array in set. * That is, the POS bit is in the array bits, which is included in the member of the size_t type. */#defineBitset_word (set, POS) \    ( (Set)->bits[(POS)/Bitset_bits])/** * Since bitset is used to store bit bits in an array of size_t types, the actual open space should be an integer multiple of size_t. * This macro is used to calculate how much memory space to open in cases where nbits bits are required. * That is, the calculation of nbits is the bitset_bits integer multiplier one. */#definebitset_used (nbits) \((nbits)+ (Bitset_bits-1)) /

Operation functions are relatively simple and short, directly posted out.

/** * Initialize a bitset to nbits bit*/Bitset*bitset_init (size_t nbits) {Bitset*Set; Set=malloc(sizeof(*Set)); ASSERT (Set); //allocate space and initialize to 0.     Set->bits =calloc(Bitset_used (Nbits),sizeof(*Set-bits)); Set->nbits =nbits; ASSERT (Set-bits); return Set; } /** * Resets all bits in set to 0*/ voidBitset_reset (Bitset *Set) {memset (Set->bits,0, Bitset_used (Set->nbits) *sizeof(*Set-bits)); } //Release Set voidBitset_free (Bitset *Set) {      Free(Set-bits);  Free(Set); } //set the POS bit to 0. voidBitset_clear_bit (Bitset *Set, size_t POS) {     if(Pos >=Set-nbits)     {Segfault (); } Bitset_word (Set, POS) &= ~Bitset_mask (POS);} //set the POS to 1 voidBitset_set_bit (Bitset *Set, size_t POS) {     if(Pos >=Set-nbits)     {Segfault (); } Bitset_word (Set, POS) |=Bitset_mask (POS);} //test whether the POS location is 1 intBitset_test_bit (Bitset *Set, size_t POS) {     if(Pos >=Set-nbits)     {Segfault (); }     return(Bitset_word (Set, POS) & Bitset_mask (pos))! =0; }

The bit bonding operation in the lighttpd is cut and lean, and all the code has been posted in this article. Of course, the function declaration in the header file is not posted ...

This article by http://www.wifidog.pro/2015/04/15/wifidog%E8%AE%A4%E8%AF%81lighttpd%E4%BD%8D%E9%9B%86%E5%90%88%E4%BD%BF%E7 %94%a8.html compilation, reprint please indicate the source

WiFiDog Authentication Lighttpd1.4.20 Source Code Analysis Bitset.c (h) Use of--------bit collections

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.