debug_malloc.c

来自「一个小公司要求给写的很简单的任务管理系统。」· C语言 代码 · 共 767 行 · 第 1/2 页

C
767
字号
            for (i = 0; i < trailing_extra_bytes; i++)	        p[i] = LEFT_OVER_CHAR;        }    }#endif    /* Fill out warrant */    if (malloc_watch) {        static Warrant_Record zero_warrant;        register void  *p1,                       *p2;	size_t len;	int start_pos = 0;	warrant_(mptr) = zero_warrant;	p1 = warrant_name_(mptr);	len = strlen(file);	if ( len >  WARRANT_NAME_MAX )  {            /*LINTED*/	    start_pos = (int)len - WARRANT_NAME_MAX;	}	p2 = ((char*)file) + start_pos;	/*LINTED*/	(void) memcpy(p1, p2, minimum(((int)len), WARRANT_NAME_MAX));	warrant_line_(mptr) = line;	warrant_id_(mptr)   = ++id_counter;	warrant_link_(mptr) = first_warrant_mptr;	first_warrant_mptr = mptr;    }}/* This function checks the clobber words at the beginning and end of the *   allocated space. */static voidmemory_check(void *uptr, int mid, const char *mfile, int mline, const char *file, int line){    int             neg_nbytes;    int             nbytes;    debug_check = "pointer value itself";    clobbered_ptr = uptr;    if (uptr == NULL)	memory_error((void *) NULL, "memory_check", mid, mfile, mline, file, line);    /* Check both Word structures */        debug_check = "first beginning clobber word";    clobbered_ptr = (char*)&user_nsize1_(uptr);    neg_nbytes = user_nsize1_(uptr);    if (neg_nbytes >= 0)	memory_error(user2malloc_(uptr), "memory_check", mid, mfile, mline, file, line);        debug_check = "second beginning clobber word";    clobbered_ptr = (char*)&user_nsize2_(uptr);    if (neg_nbytes != user_nsize2_(uptr))	memory_error(user2malloc_(uptr), "memory_check", mid, mfile, mline, file, line);        debug_check = "first ending clobber word";    clobbered_ptr = (char*)&user_tail_nsize1_(uptr);    if (neg_nbytes != user_tail_nsize1_(uptr))	memory_error(user2malloc_(uptr), "memory_check", mid, mfile, mline, file, line);        debug_check = "second ending clobber word";    clobbered_ptr = (char*)&user_tail_nsize2_(uptr);    if (neg_nbytes != user_tail_nsize2_(uptr))	memory_error(user2malloc_(uptr), "memory_check", mid, mfile, mline, file, line);    /* Get a positive count of bytes */    nbytes = -neg_nbytes;#ifdef LEFT_OVER_CHAR    {        /* Check those few extra bytes just before the tail Word structure */        register int    trailing_extra_bytes;        register int    i;        register char  *p;        /* LINTED */        trailing_extra_bytes = (int) (round_up_(nbytes) - nbytes);        p = ((char *) (uptr)) + nbytes;        debug_check = "trailing left over area";        for (i = 0; i < trailing_extra_bytes; i++) {            clobbered_ptr = p+1;	    if (p[i] != LEFT_OVER_CHAR) {	        memory_error(user2malloc_(uptr), "memory_check", mid, mfile, mline, file, line);	    }        }    }#endif    /* Make sure debug_check is cleared */    debug_check = NULL;}/* This function looks for the given malloc pointer in the police line up *   and removes it from the warrant list. *   	mptr		The pointer to the malloc space being removed */static intremove_warrant(void *mptr){    void           *mptr1,                   *last_mptr1;    /* Free it up from the list */    if (malloc_watch && mptr != NULL) {	int found;	found = 0;	last_mptr1 = NULL;	mptr1 = first_warrant_mptr;	while (mptr1 != NULL) {	    if (mptr1 == mptr) {		if (last_mptr1 == NULL)		    first_warrant_mptr = warrant_link_(mptr1);		else		    warrant_link_(last_mptr1) = warrant_link_(mptr1);		found = 1;		break;	    }	    last_mptr1 = mptr1;	    mptr1 = warrant_link_(mptr1);	}	return found;    }    return 1;}static voidactual_free(void *uptr, const char *file, int line){    void *mptr;    const char *mfile;    int mline;    int mid;    if ( uptr == NULL )	return;    mptr = user2malloc_(uptr);    memory_check(uptr, (mid=MID(mptr)), (mfile=MFILE(mptr)), (mline=MLINE(mptr)), file, line);    if (malloc_watch && remove_warrant(mptr)==0 )	memory_check(uptr, mid, mfile, mline, file, line);#ifdef FREED_CHAR    if ( mptr!=NULL ) {	size_t nbytes = -nsize1_(mptr);        /* LINTED */	(void)memset(mptr, FREED_CHAR, rbytes_(nbytes));    }#endif    free(mptr);}#ifdef MAX_FREE_DELAY_COUNTstatic void *free_delay[MAX_FREE_DELAY_COUNT];static int free_delay_pos = 0;static void delayed_free(void *uptr, const char* file, int line){    void *mptr;    void *olduptr = free_delay[free_delay_pos];    size_t nbytes;    if ( uptr==NULL )	return;    mptr = user2malloc_(uptr);    memory_check(uptr, MID(mptr), MFILE(mptr), MLINE(mptr), file, line);    if ( olduptr!=NULL ) {	actual_free(olduptr, file, line);    }    free_delay[free_delay_pos] = uptr;    free_delay_pos++;    free_delay_pos = free_delay_pos % MAX_FREE_DELAY_COUNT;    nbytes = -user_nsize1_(uptr);#ifdef FREED_CHAR    (void)memset(uptr, FREED_CHAR, (size_t)nbytes);#endif}static void delayed_free_all(const char *file, int line){    int i;    for ( i=0; i< MAX_FREE_DELAY_COUNT; i++) {	void *olduptr = free_delay[i];	free_delay[i] = NULL;	if ( olduptr!=NULL ) {	    actual_free(olduptr, file, line);        }    }}#endifvoiddebug_free(void *uptr, const char *file, int line){    int mid = 0;    if (uptr == NULL)	memory_error((void *) NULL, "debug_free", mid, file, line, file, line);#ifdef MAX_FREE_DELAY_COUNT    delayed_free(uptr, file, line);#else    actual_free(uptr, file, line);#endif}/* This function calls malloc(). */void           *debug_malloc(size_t nbytes, const char *file, int line){    void           *mptr;    void           *uptr;    int mid = id_counter;    /*LINTED*/    if ((int)nbytes <= 0)	memory_error((void *) NULL, "debug_malloc", mid, file, line, file, line);    /* LINTED */    mptr = malloc(rbytes_(nbytes));    if (mptr == NULL)	memory_error((void *) NULL, "debug_malloc", mid, file, line, file, line);    setup_space_and_issue_warrant(mptr, nbytes, file, line);    uptr = malloc2user_(mptr);#ifdef ALLOC_CHAR    (void)memset(uptr, ALLOC_CHAR, (size_t)nbytes);#endif    return uptr;}void           *debug_realloc(void *uptr, size_t nbytes, const char *file, int line){    void           *mptr;    void           *oldmptr;    void           *newuptr;    size_t         oldnbytes;    int mid = id_counter;    oldmptr = user2malloc_(uptr);    oldnbytes = 0;    if ((int)nbytes <= 0)	memory_error(oldmptr, "debug_realloc", mid, file, line, file, line);    if (uptr != NULL) {	memory_check(uptr, MID(oldmptr), MFILE(oldmptr), MLINE(oldmptr), file, line);        oldnbytes = -user_nsize1_(uptr);	if ( malloc_watch && remove_warrant(oldmptr)==0 )	    memory_check(uptr, MID(oldmptr), MFILE(oldmptr), MLINE(oldmptr), file, line);    }    if (uptr == NULL) {        /* LINTED */	mptr = malloc(rbytes_(nbytes));    } else {        /* LINTED */	mptr = realloc(oldmptr, rbytes_(nbytes));    }    if (mptr == NULL)	memory_error(oldmptr, "debug_realloc", mid, file, line, file, line);    setup_space_and_issue_warrant(mptr, nbytes, file, line);    newuptr = malloc2user_(mptr);#ifdef ALLOC_CHAR    if (uptr == NULL)        (void)memset(newuptr, ALLOC_CHAR, (size_t)nbytes);    else if ( nbytes > oldnbytes )        (void)memset(((char*)newuptr)+oldnbytes, ALLOC_CHAR, (size_t)nbytes-oldnbytes);#endif    return newuptr;}/* This function calls calloc(). */void           *debug_calloc(size_t nelem, size_t elsize, const char *file, int line){    void           *mptr;    size_t	    nbytes;    int mid = id_counter;    nbytes = nelem*elsize;    /*LINTED*/    if ((int)nbytes <= 0)	memory_error((void *) NULL, "debug_calloc", mid, file, line, file, line);    /* LINTED */    mptr = calloc(rbytes_(nbytes),1);    if (mptr == NULL)	memory_error((void *) NULL, "debug_calloc", mid, file, line, file, line);    setup_space_and_issue_warrant(mptr, nbytes, file, line);    return malloc2user_(mptr);}/* This function replaces strdup(). */char           *debug_strdup(const char *s1, const char *file, int line){    void           *mptr;    void           *uptr;    size_t	    nbytes;    int mid = id_counter;    if (s1 == NULL)	memory_error((void *) NULL, "debug_strdup", mid, file, line, file, line);    nbytes = strlen(s1)+1;    /*LINTED*/    if ((int)nbytes < 0)	memory_error((void *) NULL, "debug_strdup", mid, file, line, file, line);    /* LINTED */    mptr = malloc(rbytes_(nbytes));    if (mptr == NULL)	memory_error((void *) NULL, "debug_strdup", mid, file, line, file, line);    setup_space_and_issue_warrant(mptr, nbytes, file, line);    uptr = malloc2user_(mptr);    (void)strcpy((char*)uptr, s1);    return (char*)uptr;}voiddebug_malloc_verify(const char *file, int line){    void           *mptr;#ifdef MAX_FREE_DELAY_COUNT    delayed_free_all(file,line);#endif    if (!malloc_watch) {	return;    }    mptr = first_warrant_mptr;    if (mptr != NULL) {	/* Check all this memory first */	do {	    memory_check(malloc2user_(mptr), MID(mptr), MFILE(mptr), MLINE(mptr), file, line);	    mptr = warrant_link_(mptr);	} while (mptr != NULL);    }}/* Report outstanding space warrants to console. */voiddebug_malloc_police(const char *file, int line){    void           *mptr;#ifdef MAX_FREE_DELAY_COUNT    delayed_free_all(file,line);#endif    if (!malloc_watch) {	return;    }    mptr = first_warrant_mptr;    if (mptr != NULL) {        debug_malloc_verify(file, line);	/* Now issue warrants */        mptr = first_warrant_mptr;	do {	    error_message("Outstanding space warrant: %p (%d bytes) allocated by %s at line %d, allocation #%d",	       mptr, -nsize1_(mptr), warrant_name_(mptr), 	       warrant_line_(mptr), warrant_id_(mptr));	    mptr = warrant_link_(mptr);	} while (mptr != NULL);    }}#elsevoiddebug_malloc_verify(const char *file, int line){    file = file;    line = line;}voiddebug_malloc_police(const char *file, int line){    file = file;    line = line;}#endif

⌨️ 快捷键说明

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