Commit a72050aa authored by malenov's avatar malenov
Browse files

re-factor update_mem() for better clarity

parent 907e7767
Loading
Loading
Loading
Loading
Loading
+58 −40
Original line number Diff line number Diff line
@@ -1361,11 +1361,11 @@ void mem_free( const char *func_name, int func_lineno, void *ptr )
void update_mem( void )
{
    int i, j, flag_alloc = -1, i_record;
    int32_t size_current_intra_frame_heap;
    int size_current_intra_frame_heap;
    int *list_current_intra_frame_heap = NULL, n_items_current_intra_frame_heap;
    allocator_record *ptr_record;

    /* process the heap allocation call tree */
    /* process the heap allocation call tree and prepare lists of intra-frame and inter-frame heap memory blocks for this frame */
    n_items_current_intra_frame_heap = 0;
    size_current_intra_frame_heap = 0;
    for ( i = 0; i < heap_allocation_call_tree_size; i++ )
@@ -1413,23 +1413,7 @@ void update_mem( void )
                list_current_intra_frame_heap[n_items_current_intra_frame_heap++] = i_record;
                size_current_intra_frame_heap += ptr_record->block_size;

                /* check, if this is the new worst-case */
                if ( size_current_intra_frame_heap > size_wc_intra_frame_heap )
                {
                    if ( n_items_current_intra_frame_heap >= max_items_wc_intra_frame_heap )
                    {
                        /* resize list, if needed */
                        max_items_wc_intra_frame_heap = n_items_current_intra_frame_heap + MAX_NUM_RECORDS_REALLOC_STEP;
                        list_wc_intra_frame_heap = realloc( list_wc_intra_frame_heap, max_items_wc_intra_frame_heap * sizeof( int ) );
                    }

                    /* save to wc list */
                    memmove( list_wc_intra_frame_heap, list_current_intra_frame_heap, n_items_current_intra_frame_heap * sizeof( int ) );
                    n_items_wc_intra_frame_heap = n_items_current_intra_frame_heap;
                    size_wc_intra_frame_heap = size_current_intra_frame_heap;
                    location_wc_intra_frame_heap = update_cnt;
                    ptr_record->wc_heap_size_intra_frame = ptr_record->block_size;
                }
                /* no need to re-size the list -> the initially allocated size should be large enough */
            }
            else
            {
@@ -1480,23 +1464,6 @@ void update_mem( void )

                list_current_inter_frame_heap[n_items_current_inter_frame_heap++] = i_record;
                size_current_inter_frame_heap += ptr_record->block_size;

                /* check, if this is the new worst-case */
                if ( size_current_inter_frame_heap > size_wc_inter_frame_heap )
                {
                    if ( n_items_current_inter_frame_heap >= max_items_wc_inter_frame_heap )
                    {
                        /* resize list, if needed */
                        max_items_wc_inter_frame_heap = n_items_current_inter_frame_heap + MAX_NUM_RECORDS_REALLOC_STEP;
                        list_wc_inter_frame_heap = realloc( list_wc_inter_frame_heap, max_items_wc_inter_frame_heap * sizeof( int ) );
                    }

                    memmove( list_wc_inter_frame_heap, list_current_inter_frame_heap, n_items_current_inter_frame_heap * sizeof( int ) );
                    n_items_wc_inter_frame_heap = n_items_current_inter_frame_heap;
                    size_wc_inter_frame_heap = size_current_inter_frame_heap;
                    location_wc_inter_frame_heap = update_cnt;
                    ptr_record->wc_heap_size_inter_frame = ptr_record->block_size;
                }
            }
            else
            {
@@ -1519,9 +1486,60 @@ void update_mem( void )
        }
    }

    /* check, if this is the new worst-case for intra-frame heap memory */
    if ( size_current_intra_frame_heap > size_wc_intra_frame_heap )
    {
        if ( n_items_current_intra_frame_heap >= max_items_wc_intra_frame_heap )
        {
            /* resize the list, if needed */
            max_items_wc_intra_frame_heap = n_items_current_intra_frame_heap + MAX_NUM_RECORDS_REALLOC_STEP;
            list_wc_intra_frame_heap = realloc( list_wc_intra_frame_heap, max_items_wc_intra_frame_heap * sizeof( int ) );
        }

        /* copy current-frame list to worst-case list */
        memmove( list_wc_intra_frame_heap, list_current_intra_frame_heap, n_items_current_intra_frame_heap * sizeof( int ) );
        n_items_wc_intra_frame_heap = n_items_current_intra_frame_heap;
        size_wc_intra_frame_heap = size_current_intra_frame_heap;
        location_wc_intra_frame_heap = update_cnt;

        /* update the wc numbers in all individual records */
        for ( i = 0; i < n_items_wc_intra_frame_heap; i++ )
        {
            i_record = list_wc_intra_frame_heap[i];
            ptr_record = &( allocation_list[i_record] );
            ptr_record->wc_heap_size_intra_frame = ptr_record->block_size;
        }
    }

    /* check, if this is the new worst-case for inter-frame heap memory */
    if ( size_current_inter_frame_heap > size_wc_inter_frame_heap )
    {
        if ( n_items_current_inter_frame_heap >= max_items_wc_inter_frame_heap )
        {
            /* resize list, if needed */
            max_items_wc_inter_frame_heap = n_items_current_inter_frame_heap + MAX_NUM_RECORDS_REALLOC_STEP;
            list_wc_inter_frame_heap = realloc( list_wc_inter_frame_heap, max_items_wc_inter_frame_heap * sizeof( int ) );
        }

        /* copy current-frame list to worst-case list */
        memmove( list_wc_inter_frame_heap, list_current_inter_frame_heap, n_items_current_inter_frame_heap * sizeof( int ) );
        n_items_wc_inter_frame_heap = n_items_current_inter_frame_heap;
        size_wc_inter_frame_heap = size_current_inter_frame_heap;
        location_wc_inter_frame_heap = update_cnt;

        /* update the wc numbers in all individual records */
        for ( i = 0; i < n_items_wc_inter_frame_heap; i++ )
        {
            i_record = list_wc_inter_frame_heap[i];
            ptr_record = &( allocation_list[i_record] );
            ptr_record->wc_heap_size_inter_frame = ptr_record->block_size;
        }
    }

    /* reset heap allocation call tree */
    heap_allocation_call_tree_size = 0;

    /* de-allocate list of intra-frame heap memory blocks in the current fraeme - it's needed only inside this function */
    if ( list_current_intra_frame_heap )
    {
        free( list_current_intra_frame_heap );
@@ -1665,7 +1683,7 @@ static void mem_count_summary( void )
                continue;
            }
            ptr_record = &( allocation_list[index_record] );
            ptr_record->noccurances = 1; /* reset the counter because som blocks may be both, intra-frame and inter-frame */
            ptr_record->noccurances = 1; /* reset the counter as some blocks may have been both, intra-frame and inter-frame */
            for ( j = i + 1; j < n_items_wc_inter_frame_heap; j++ )
            {
                index = list_wc_inter_frame_heap[j];
@@ -1684,7 +1702,7 @@ static void mem_count_summary( void )
        }

        /* Print Header */
        sprintf( buf, format_str, "Function Name", "Line", "Type", "Function Parameters", "Maximum Size", "Usage" );
        sprintf( buf, format_str, "Function Name", "Line", "Type", "Function Parameters", "Memory Size", "Usage" );
        puts( buf );
        length = strlen( buf );
        sprintf( buf, "%0*d\n", (int) length - 1, 0 );
@@ -1719,7 +1737,7 @@ static void mem_count_summary( void )
                sprintf( line_str, "%d", ptr_record->lineno );

                /* prepare average usage & memory size strings */
                sprintf( usage_str, "%d%%", (int) ( ( (float) ptr_record->total_used_size / ( ptr_record->total_block_size + 1 ) ) * 100.0f ) );
                sprintf( usage_str, "%d%%", (int) ( ( (float) ptr_record->total_used_size / ( ptr_record->total_block_size + 0.1f ) ) * 100.0f + 0.5f ) );

                if ( ptr_record->noccurances > 1 )
                {