/************************************************************************
Return a page pointer from page_array which is in flush_list and it's
oldest_modification is older than that of offered block.*/
buf_page_t*
get_older_page_in_flush_list(
/*==========================*/
                                     /*out: a page pointer which is in flush_list*/
        buf_block_t*    block) /* in: block which is to be compared */
{
        static buf_page_t*   page_array[10000];                               /* contains some pages */
        static ulint         array_size = 0;                                 /* size of the array */
        static ulint         called_time = 0;                                /* be used to update array */
        buf_page_t*          older_flush_page = NULL;

        called_time++;

        if (called_time%1000 == 0)                                   /* update page_array every 1000 called*/
        {
                buf_page_t*     tmp_page = NULL;
                ulint tmp_index = 1;

                tmp_page = UT_LIST_GET_FIRST(buf_pool->flush_list);
                array_size = 0;

                while (tmp_page && tmp_index < 10000000L ) {
                        if (tmp_index%1000 == 0){
                                page_array[tmp_index/1000-1] = tmp_page;
                                array_size++;
                        }

                        tmp_index++;
                        tmp_page = UT_LIST_GET_NEXT(list, tmp_page);
                }
                called_time=0;
        }

        ulint index = 0;

        while(index < array_size){
                if( page_array[index]->oldest_modification > block->page.oldest_modification)
                {
                        older_flush_page = page_array[index];
                }
                else 
                {
                        break ;
                }
                index++;
        }

        return older_flush_page;

}


/************************************************************************
Inserts a modified block into the flush list in the right sorted position.
This function is used by recovery, because there the modifications do not
necessarily come in the order of lsn's. */
UNIV_INTERN
void
buf_flush_insert_sorted_into_flush_list(
/*====================================*/
        buf_block_t*    block)  /* in/out: block which is modified */
{


        buf_page_t*     prev_b;
        buf_page_t*     b;

        //ut_ad(buf_pool_mutex_own());
        ut_ad(mutex_own(&flush_list_mutex));
        ut_ad(buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE);

        ut_ad(block->page.in_LRU_list);
        ut_ad(block->page.in_page_hash);
        ut_ad(!block->page.in_zip_hash);
        ut_ad(!block->page.in_flush_list);
        ut_d(block->page.in_flush_list = TRUE);

        prev_b = get_older_page_in_flush_list( block );
        if (prev_b == NULL) {
                 b = UT_LIST_GET_FIRST(buf_pool->flush_list);
        }else{
                 b = UT_LIST_GET_NEXT(list, prev_b);
        }

        while (b && b->oldest_modification > block->page.oldest_modification) {
                ut_ad(b->in_flush_list);
                prev_b = b;
                b = UT_LIST_GET_NEXT(list, b);
        }

        if (prev_b == NULL) {
                UT_LIST_ADD_FIRST(list, buf_pool->flush_list, &block->page);
        } else {
                UT_LIST_INSERT_AFTER(list, buf_pool->flush_list,
                                     prev_b, &block->page);
        }
}