函数xdes_find_bit

 

使用方法 

free = xdes_find_bit(descr, XDES_FREE_BIT, TRUE,hint % FSP_EXTENT_SIZE, mtr);

/**********************************************************************//**
Looks for a descriptor bit having the desired value. Starts from hint
and scans upward; at the end of the extent the search is wrapped to
the start of the extent.
@return    bit index of the bit, ULINT_UNDEFINED if not found */
UNIV_INLINE
ulint
xdes_find_bit(
/*==========*/
    xdes_t*    descr,    /*!< in: descriptor */
    ulint    bit,    /*!< in: XDES_FREE_BIT or XDES_CLEAN_BIT */ // 0 or 1
    ibool    val,    /*!< in: desired bit value */
    ulint    hint,    /*!< in: hint of which bit position would be desirable */
    mtr_t*    mtr)    /*!< in: mtr */
{
    ulint    i;

    ut_ad(descr && mtr);
    ut_ad(val <= TRUE);
    ut_ad(hint < FSP_EXTENT_SIZE);
    ut_ad(mtr_memo_contains_page(mtr, descr, MTR_MEMO_PAGE_X_FIX));
    for (i = hint; i < FSP_EXTENT_SIZE; i++) { //宏 #define FSP_EXTENT_SIZE (1 << (20 - UNIV_PAGE_SIZE_SHIFT)) 为1<<(20-14) if (val == xdes_get_bit(descr, bit, i, mtr)) {

            return(i);
        }
    }

    for (i = 0; i < hint; i++) {
        if (val == xdes_get_bit(descr, bit, i, mtr)) {

            return(i);
        }
    }

    return(ULINT_UNDEFINED);
}

 



/**********************************************************************//**
Gets a descriptor bit of a page.
@return    TRUE if free */
UNIV_INLINE
ibool
xdes_get_bit(
/*=========*/
    const xdes_t*  descr,    /*!< in: descriptor */
    ulint          bit,    /*!< in: XDES_FREE_BIT or XDES_CLEAN_BIT */ // 0 or 1 
    ulint          offset,    /*!< in: page offset within extent: 0 ... FSP_EXTENT_SIZE - 1 */
    mtr_t*         mtr)    /*!< in: mtr */
{
    ulint    index;
    ulint    byte_index;
    ulint    bit_index;

    ut_ad(mtr_memo_contains_page(mtr, descr, MTR_MEMO_PAGE_X_FIX));
    ut_ad((bit == XDES_FREE_BIT) || (bit == XDES_CLEAN_BIT));
    ut_ad(offset < FSP_EXTENT_SIZE);

    index = bit + XDES_BITS_PER_PAGE * offset; //#define XDES_BITS_PER_PAGE 2 /* How many bits are there per page */

    byte_index = index / 8;
    bit_index = index % 8;

/**
*descr 是 XDES Entry的入口地址
*descr + 24 即XDES Entry中XDES BITMAP的入口地址
*
*#define XDES_BITMAP (FLST_NODE_SIZE + 12)
*#define FLST_NODE_SIZE (2 * FIL_ADDR_SIZE) /* The physical size of a list node in bytes */
*#define FIL_ADDR_SIZE 6 /* address size is 6 bytes */
*/
return(ut_bit_get_nth(mtr_read_ulint(descr + XDES_BITMAP + byte_index,MLOG_1BYTE, mtr),bit_index)); }

 

/********************************************************//**
Reads 1 - 4 bytes from a file page buffered in the buffer pool.
@return    value read */
UNIV_INTERN
ulint
mtr_read_ulint(
/*===========*/
    const byte*    ptr,    /*!< in: pointer from where to read */
    ulint        type,    /*!< in: MLOG_1BYTE, MLOG_2BYTES, MLOG_4BYTES */
    mtr_t*        mtr __attribute__((unused)))
                /*!< in: mini-transaction handle */
{
    ut_ad(mtr->state == MTR_ACTIVE);
    ut_ad(mtr_memo_contains_page(mtr, ptr, MTR_MEMO_PAGE_S_FIX)
          || mtr_memo_contains_page(mtr, ptr, MTR_MEMO_PAGE_X_FIX));
    if (type == MLOG_1BYTE) {
        return(mach_read_from_1(ptr));
    } else if (type == MLOG_2BYTES) {
        return(mach_read_from_2(ptr));
    } else {
        ut_ad(type == MLOG_4BYTES);
        return(mach_read_from_4(ptr));
    }
}

 

/********************************************************//**
The following function is used to fetch data from one byte.
@return    ulint integer, >= 0, < 256 */
UNIV_INLINE
ulint
mach_read_from_1(
/*=============*/
    const byte*    b)    /*!< in: pointer to byte */
{
    ut_ad(b);
    return((ulint)(b[0]));
}

 

 

/*****************************************************************//**
Gets the nth bit of a ulint.
@return    TRUE if nth bit is 1; 0th bit is defined to be the least significant */
UNIV_INLINE
ibool
ut_bit_get_nth(
/*===========*/
    ulint    a,    /*!< in: ulint */
    ulint    n)    /*!< in: nth bit requested */
{
    ut_ad(n < 8 * sizeof(ulint));
#if TRUE != 1
# error "TRUE != 1"
#endif
    return(1 & (a >> n));
}

 

/*****************************************************************//**
Sets the nth bit of a ulint.
@return    the ulint with the bit set as requested */
UNIV_INLINE
ulint
ut_bit_set_nth(
/*===========*/
    ulint    a,    /*!< in: ulint */
    ulint    n,    /*!< in: nth bit requested */
    ibool    val)    /*!< in: value for the bit to set */
{
    ut_ad(n < 8 * sizeof(ulint));
#if TRUE != 1
# error "TRUE != 1"
#endif
    if (val) {
/**
*n的值此时为 index%8 为 7 ,6 ,5,4,3,2,1,0 来决定属于哪一个
*假设在第5个,1 << 5 的二进制为 100000 最大的1的下标为5
*/
return(((ulint) 1 << n) | a); } else { return(~((ulint) 1 << n) & a); } }

 

                                        

 

你可能感兴趣的:(函数xdes_find_bit)