函数fsp_alloc_free_page

 

 从fsp中分配32个碎片页

/**********************************************************************//**
Allocates a single free page from a space. The page is marked as used.
@retval NULL if no page could be allocated
@retval block, rw_lock_x_lock_count(&block->lock) == 1 if allocation succeeded
(init_mtr == mtr, or the page was not previously freed in mtr)
@retval block (not allocated or initialized) otherwise */
static __attribute__((nonnull, warn_unused_result))
buf_block_t*
fsp_alloc_free_page(
/*================*/
    ulint    space,    /*!< in: space id */
    ulint    zip_size,/*!< in: compressed page size in bytes
            or 0 for uncompressed pages */
    ulint    hint,    /*!< in: hint of which page would be desirable */
    mtr_t*    mtr,    /*!< in/out: mini-transaction */
    mtr_t*    init_mtr)/*!< in/out: mini-transaction in which the
            page should be initialized
            (may be the same as mtr) */
{
    fsp_header_t*    header;
    fil_addr_t    first;
    xdes_t*        descr;
    ulint        free;
    ulint        page_no;
    ulint        space_size;

    ut_ad(mtr);
    ut_ad(init_mtr);

    header = fsp_get_space_header(space, zip_size, mtr); //详见     /* Get the hinted descriptor */
/**
*descr 是XDES 页中第N个XDES Entry 的内存地址
*
*/
descr = xdes_get_descriptor_with_space_hdr(header, space, hint, mtr); //详见 /**
*这个desc的类型属于XDES_FREE_FRAG 可拿来使用
*为一个段指定空间时, 先从 FSP中的碎片区中取,若拿不至,再分配extent
*
*/
if
(descr && (xdes_get_state(descr, mtr) == XDES_FREE_FRAG)) { //详见 /* Ok, we can take this extent */
//descr 所在的 xdes entry 的状态属于 free_freg 碎片
} else { /* Else take the first extent in free_frag list */
//#define FSP_FREE_FRAG (24 + FLST_BASE_NODE_SIZE)
//#define FLST_BASE_NODE_SIZE (4 + 2 * FIL_ADDR_SIZE)
//#define FIL_ADDR_SIZE 6

//获得fsp header +40 的 free_frag 空闲碎片
//fsp中的free_frag类型是 flst_base_node_t,真正类型为byte
        first = flst_get_first(header + FSP_FREE_FRAG, mtr); //详见         if (fil_addr_is_null(first)) {
            /* There are no partially full fragments: allocate
            a free extent and add it to the FREE_FRAG list. NOTE
            that the allocation may have as a side-effect that an
            extent containing a descriptor page is added to the
            FREE_FRAG list. But we will allocate our page from the
            the free extent anyway. */

            descr = fsp_alloc_free_extent(space, zip_size,hint, mtr);

            if (descr == NULL) {
                /* No free space left */

                return(NULL);
            }

            xdes_set_state(descr, XDES_FREE_FRAG, mtr);
            flst_add_last(header + FSP_FREE_FRAG,
                      descr + XDES_FLST_NODE, mtr);
        } else {
            descr = xdes_lst_get_descriptor(space, zip_size,
                            first, mtr);
        }

        /* Reset the hint */
        hint = 0;
    }

    /* Now we have in descr an extent with at least one free page. Look
    for a free page in the extent. */

    free = xdes_find_bit(descr, XDES_FREE_BIT, TRUE,hint % FSP_EXTENT_SIZE, mtr); //详见     if (free == ULINT_UNDEFINED) {

        ut_print_buf(stderr, ((byte*)descr) - 500, 1000);
        putc('\n', stderr);

        ut_error;
    }

    page_no = xdes_get_offset(descr) + free; //计算出某一bit的page_no 详见

    space_size = mtr_read_ulint(header + FSP_SIZE, MLOG_4BYTES, mtr);

    if (space_size <= page_no) {
        /* It must be that we are extending a single-table tablespace
        whose size is still < 64 pages */

        ut_a(space != 0);
        if (page_no >= FSP_EXTENT_SIZE) {
            fprintf(stderr,
                "InnoDB: Error: trying to extend a"
                " single-table tablespace %lu\n"
                "InnoDB: by single page(s) though the"
                " space size %lu. Page no %lu.\n",
                (ulong) space, (ulong) space_size,
                (ulong) page_no);
            return(NULL);
        }
        if (!fsp_try_extend_data_file_with_pages(space, page_no,
                             header, mtr)) {
            /* No disk space left */
            return(NULL);
        }
    }

    fsp_alloc_from_free_frag(header, descr, free, mtr);//详见     return(fsp_page_create(space, zip_size, page_no, mtr, init_mtr));
}

 

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