⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 trmem.c

📁 刚才是说明 现在是安装程序在 LINUX环境下进行编程的MPICH安装文件
💻 C
📖 第 1 页 / 共 2 页
字号:
/* -*- Mode: C; c-basic-offset:4 ; -*- *//*  $Id: trmem.c,v 1.11 2002/05/01 16:52:52 gropp Exp $ * *  (C) 2001 by Argonne National Laboratory. *      See COPYRIGHT in top-level directory. */#include "mpiimpl.h"#include <stdio.h>#include <string.h>/* Temporary.  sig values will change *//* style: allow:malloc:3 sig:0 *//* style: allow:calloc:2 sig:0 *//* style: allow:free:3 sig:0 *//* style: allow:strdup:1 sig:0 *//* style: define:malloc:1 sig:0 *//* style: define:__strdup:1 sig: 0 */#ifdef malloc/* Undefine these in case they were set to 'error' */#undef malloc#undef calloc#undef free#undef strdup/* Some GNU implementations use __strdup for strdup */#if defined(__strdup)#define strdup(s) __strdup(s)#endif#endif/* If you change this, you must change the format spec (%lx) to match */typedef long PointerInt;#if defined(HAVE_STDLIB_H) || defined(STDC_HEADERS)#include <stdlib.h>#else#ifdef __STDC__extern void 	*calloc(/*size_t, size_t*/);extern void	free(/*void * */);extern void	*malloc(/*size_t*/);#elseextern char *malloc();extern char *calloc();extern int free();#endif#endif/*D    MPIU_trspace - Routines for tracing space usage    Description:    MPIU_trmalloc replaces malloc and MPIU_trfree replaces free.      These routines    have the same syntax and semantics as the routines that they replace,    In addition, there are routines to report statistics on the memory    usage, and to report the currently allocated space.  These routines    are built on top of malloc and free, and can be used together with    them as long as any space allocated with MPIU_trmalloc is only freed with    MPIU_trfree.    Note that the malloced data is scrubbed each time; you don't get    random trash (or fortuitous zeros).  What you get is fc (bytes);    this will usually create a "bad" value.    As an aid in developing codes, a maximum memory threshold can     be set with MPIU_TrSetMaxMem. D*//* HEADER_DOUBLES is the number of doubles in a trSPACE header *//* We have to be careful about alignment rules here */#if SIZEOF_VOID_P > 4#define TR_ALIGN_BYTES 8#define TR_ALIGN_MASK  0x7#define TR_FNAME_LEN   16#define HEADER_DOUBLES 12#else#define TR_ALIGN_BYTES 4#define TR_ALIGN_MASK  0x3#define TR_FNAME_LEN   12#define HEADER_DOUBLES 8#endif#define COOKIE_VALUE   0xf0e0d0c9#define ALREADY_FREED  0x0f0e0d9ctypedef struct _trSPACE {    unsigned long   size;    int             id;    int             lineno;    char            fname[TR_FNAME_LEN];    int             freed_lineno;    char            freed_fname[TR_FNAME_LEN];    unsigned long   cookie;            struct _trSPACE *next, *prev;    } TRSPACE;/* This union is used to insure that the block passed to the user is   aligned on a double boundary */typedef union {    TRSPACE sp;    double  v[HEADER_DOUBLES];    } TrSPACE;/* * This package maintains some state about itself.  These globals hold * this information. */static int     world_rank = -1;static long    allocated = 0, frags = 0;static TRSPACE *TRhead = 0;static int     TRid = 0;static int     TRlevel = 0;#define MAX_TR_STACK 20static int     TRstack[MAX_TR_STACK];static int     TRstackp = 0;static int     TRdebugLevel = 0;#define TR_MALLOC 0x1#define TR_FREE   0x2/* Used to keep track of allocations */static long    TRMaxMem = 0;static long    TRMaxMemId = 0;/* Used to limit allocation */static long    TRMaxMemAllow = 0;/*+C   MPIU_trinit - Setup the space package.  Only needed for    error messages and flags.+*/void MPIU_trinit( int rank ){    world_rank = rank;} /*+C    MPIU_trmalloc - Malloc with tracing    Input Parameters:.   a   - number of bytes to allocate.   lineno - line number where used.  Use __LINE__ for this.   fname  - file name where used.  Use __FILE__ for this    Returns:    double aligned pointer to requested storage, or null if not    available. +*/void *MPIU_trmalloc( unsigned int a, int lineno, const char fname[] ){    TRSPACE          *head;    char             *new;    unsigned long    *nend;    unsigned int     nsize;    int              l;    if (TRdebugLevel > 0) {	char buf[256];	sprintf( buf, "Invalid MALLOC arena detected at line %d in %s\n", 		 lineno, fname );	if (MPIU_trvalid( buf )) return 0;    }    nsize = a;    if (nsize & TR_ALIGN_MASK) 	nsize += (TR_ALIGN_BYTES - (nsize & TR_ALIGN_MASK));    if ((allocated + (long)nsize > TRMaxMemAllow) && TRMaxMemAllow) {	/* Return a null when memory would be exhausted */	fprintf( stderr, "Exceeded allowed memory! \n" );	return 0;    }    new = malloc( (unsigned)( nsize + sizeof(TrSPACE) + sizeof(unsigned long) ) );    if (!new) return 0;    memset( new, 0xfc, nsize + sizeof(TrSPACE) + sizeof(unsigned long) );    head = (TRSPACE *)new;    new  += sizeof(TrSPACE);    if (TRhead)	TRhead->prev = head;    head->next     = TRhead;    TRhead         = head;    head->prev     = 0;    head->size     = nsize;    head->id       = TRid;    head->lineno   = lineno;    if ((l = strlen( fname )) > TR_FNAME_LEN-1 ) fname += (l - (TR_FNAME_LEN-1));    strncpy( head->fname, fname, (TR_FNAME_LEN-1) );    head->fname[TR_FNAME_LEN-1]= 0;    head->cookie   = COOKIE_VALUE;    nend           = (unsigned long *)(new + nsize);    nend[0]        = COOKIE_VALUE;    allocated += nsize;    if (allocated > TRMaxMem) {	TRMaxMem   = allocated;	TRMaxMemId = TRid;    }    frags     ++;    if (TRlevel & TR_MALLOC) 	msg_fprintf( stderr, "[%d] Allocating %d bytes at %lx in %s:%d\n", 		     world_rank, a, (PointerInt)new, fname, lineno );    return (void *)new;}/*+C   MPIU_trfree - Free with tracing   Input Parameters:.  a    - pointer to a block allocated with trmalloc.  line - line in file where called.  file - Name of file where called +*/void MPIU_trfree( void *a_ptr, int line, const char file[] ){    TRSPACE  *head;    char     *ahead;    char     *a = (char *)a_ptr;    unsigned long *nend;    int      l, nset;/* Don't try to handle empty blocks */    if (!a) return;    if (TRdebugLevel > 0) {	if (MPIU_trvalid( "Invalid MALLOC arena detected by FREE" )) return;    }    ahead = a;    a     = a - sizeof(TrSPACE);    head  = (TRSPACE *)a;    if (head->cookie != COOKIE_VALUE) {	/* Damaged header */	msg_fprintf( stderr, 		     "[%d] Block at address %lx is corrupted; cannot free;\n\may be block not allocated with MPIU_trmalloc or MALLOC\n\called in %s at line %d\n", world_rank, (PointerInt)a, file, line );	return;    }    nend = (unsigned long *)(ahead + head->size);/* Check that nend is properly aligned */    if ((sizeof(long) == 4 && ((long)nend & 0x3) != 0) ||	(sizeof(long) == 8 && ((long)nend & 0x7) != 0)) {	msg_fprintf( stderr, "[%d] Block at address %lx is corrupted (invalid address or header)\n\called in %s at line %d\n", world_rank, (long)a + sizeof(TrSPACE), 		 file, line );	return;    }    if (*nend != COOKIE_VALUE) {	if (*nend == ALREADY_FREED) {	    msg_fprintf( stderr, 		     "[%d] Block [id=%d(%lu)] at address %lx was already freed\n", 		     world_rank, head->id, head->size, (PointerInt)a + sizeof(TrSPACE) );	    head->fname[TR_FNAME_LEN-1]	  = 0;  /* Just in case */	    head->freed_fname[TR_FNAME_LEN-1] = 0;  /* Just in case */	    msg_fprintf( stderr, 		     "[%d] Block freed in %s[%d]\n", world_rank, head->freed_fname, 		     head->freed_lineno );	    msg_fprintf( stderr, 		     "[%d] Block allocated at %s[%d]\n", 		     world_rank, head->fname, head->lineno );	    return;	}	else {	    /* Damaged tail */	    msg_fprintf( stderr, 		     "[%d] Block [id=%d(%lu)] at address %lx is corrupted (probably write past end)\n", 		     world_rank, head->id, head->size, (PointerInt)a );	    head->fname[TR_FNAME_LEN-1]= 0;  /* Just in case */	    msg_fprintf( stderr, 		     "[%d] Block allocated in %s[%d]\n", world_rank, 		     head->fname, head->lineno );	}    }/* Mark the location freed */    *nend		   = ALREADY_FREED;    head->freed_lineno = line;    if ((l = strlen( file )) > TR_FNAME_LEN-1 ) file += (l - (TR_FNAME_LEN-1));    strncpy( head->freed_fname, file, (TR_FNAME_LEN-1) );    allocated -= head->size;    frags     --;    if (head->prev)	head->prev->next = head->next;    else	TRhead = head->next;    if (head->next)	head->next->prev = head->prev;    if (TRlevel & TR_FREE)	msg_fprintf( stderr, "[%d] Freeing %lu bytes at %lx in %s:%d\n", 		     world_rank, head->size, (PointerInt)a + sizeof(TrSPACE),		     file, line );        /*        Now, scrub the data (except possibly the first few ints) to       help catch access to already freed data      */    nset = head->size -  2 * sizeof(int);    if (nset > 0) 	memset( ahead + 2 * sizeof(int), 0xda, nset );    free( a );}/*+C   MPIU_trvalid - test the allocated blocks for validity.  This can be used to   check for memory overwrites.   Input Parameter:.  str - string to write out only if an error is detected.   Return value:   The number of errors detected.      Output Effect:   Error messages are written to stdout.  These have the form of either$   Block [id=%d(%d)] at address %lx is corrupted (probably write past end)$   Block allocated in <filename>[<linenumber>]   if the sentinal at the end of the block has been corrupted, and$   Block at address %lx is corrupted   if the sentinal at the begining of the block has been corrupted.   The address is the actual address of the block.  The id is the   value of TRID.   No output is generated if there are no problems detected.+*/int MPIU_trvalid( const char str[] ){    TRSPACE *head;    char    *a;    unsigned long *nend;    int     errs = 0;    head = TRhead;    while (head) {	if (head->cookie != COOKIE_VALUE) {	    if (!errs) msg_fprintf( stderr, "%s\n", str );	    errs++;	    msg_fprintf( stderr, "[%d] Block at address %lx is corrupted\n", 			 world_rank, (PointerInt)head );	    /* Must stop because if head is invalid, then the data in the	       head is probably also invalid, and using could lead to 	       SEGV or BUS  */	    return errs;	}	a    = (char *)(((TrSPACE*)head) + 1);	nend = (unsigned long *)(a + head->size);	if (nend[0] != COOKIE_VALUE) {	    if (!errs) msg_fprintf( stderr, "%s\n", str );	    errs++;	    head->fname[TR_FNAME_LEN-1]= 0;  /* Just in case */	    msg_fprintf( stderr, "[%d] Block [id=%d(%lu)] at address %lx is corrupted (probably write past end)\n", 			 world_rank, head->id, head->size, (PointerInt)a );	    msg_fprintf( stderr, 			 "[%d] Block allocated in %s[%d]\n", 			 world_rank, head->fname, head->lineno );	}	head = head->next;    }    return errs;}/*+C   MPIU_trspace - Return space statistics      Output parameters:.   space - number of bytes currently allocated.   frags - number of blocks currently allocated +*/void MPIU_trspace( int *space, int *fr ){    /* We use ints because systems without prototypes will usually       allow calls with ints instead of longs, leading to unexpected       behavior */    *space = (int)allocated;    *fr    = (int)frags;}/*+C  MPIU_trdump - Dump the allocated memory blocks to a file

⌨️ 快捷键说明

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