memtest.c

来自「开放源码的编译器open watcom 1.6.0版的源代码」· C语言 代码 · 共 115 行

C
115
字号
/****************************************************************************
*
*                            Open Watcom Project
*
*    Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
*
*  ========================================================================
*
*    This file contains Original Code and/or Modifications of Original
*    Code as defined in and that are subject to the Sybase Open Watcom
*    Public License version 1.0 (the 'License'). You may not use this file
*    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
*    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
*    provided with the Original Code and Modifications, and is also
*    available at www.sybase.com/developer/opensource.
*
*    The Original Code and all software distributed under the License are
*    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
*    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
*    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
*    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
*    NON-INFRINGEMENT. Please see the License for the specific language
*    governing rights and limitations under the License.
*
*  ========================================================================
*
* Description:  Heap management functions test.
*
****************************************************************************/


#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

#ifdef __SW_BW
 #include <wdefwin.h>
#endif

#define MAX_ITER        5000

#define NUM_ALLOCS      211
#define NUM_FREES       51
#define COPRIME         7
#define BIGGER_PRIME    521

#define ROWS    16
#define COLS    64
#define UNIT    ( 0x10000 / ( ROWS * COLS ))

unsigned last_row, last_col;
unsigned failed_mallocs;

void *m[NUM_ALLOCS];

#if defined(__VERBOSE__)

char usage[ROWS][COLS];

static void _dump_heaps( void )
{
    unsigned row, col, siz;
    unsigned prev_seg;
    int heap_status;
    unsigned long free_size;
    unsigned long used_size;
    unsigned largest_free_size;
    size_t old_size;
    struct _heapinfo hinfo;

    used_size = 0;
    free_size = 0;
    old_size = 0;
    largest_free_size = 0;
    hinfo._pentry = NULL;
    heap_status = _heapwalk( &hinfo );
    if( heap_status == _HEAPOK ) {
        prev_seg = FP_SEG( hinfo._pentry );
    }
    while( heap_status == _HEAPOK ) {
        if( prev_seg != FP_SEG( hinfo._pentry )) {
            printf("segment: %x\n", prev_seg );
            memset( &usage[last_row][last_col], ' ',
                    &usage[ROWS][0] - &usage[last_row][last_col] );
            last_row = 0;
            last_col = 0;
            _dump_heap( free_size, used_size, largest_free_size );
            used_size = 0;
            free_size = 0;
            old_size = 0;
            largest_free_size = 0;
            prev_seg = FP_SEG( hinfo._pentry );
        }
        /* we don't want to count the last free block */
        free_size += old_size;
        if( largest_free_size < old_size ) {
            largest_free_size = old_size;
        }
        if( hinfo._useflag == _USEDENTRY ) {
            used_size += hinfo._size;
            old_size = 0;
        } else {
            row = (unsigned) hinfo._pentry;
            row /= ( COLS * UNIT );
            col = (unsigned) hinfo._pentry;
            col %= ( COLS * UNIT );
            col /= UNIT/2;
            siz = hinfo._size / (UNIT/2);
            if( col & 1 ) {
                if( siz == 0 ) {
                    col >>= 1;
                } else {
                    usage[row][col>>1] = '

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?