home *** CD-ROM | disk | FTP | other *** search
Text File | 1992-01-29 | 40.0 KB | 1,797 lines |
- Newsgroups: comp.sources.misc
- From: dvadura@plg.waterloo.edu (Dennis Vadura)
- Subject: v27i106: dmake - dmake Version 3.8, Part05/41
- Message-ID: <1992Jan28.031308.7011@sparky.imd.sterling.com>
- X-Md4-Signature: bfbcc482364abe3521ca088b6ffcb493
- Date: Tue, 28 Jan 1992 03:13:08 GMT
- Approved: kent@sparky.imd.sterling.com
-
- Submitted-by: dvadura@plg.waterloo.edu (Dennis Vadura)
- Posting-number: Volume 27, Issue 106
- Archive-name: dmake/part05
- Environment: Atari-ST, Coherent, Mac, MSDOS, OS/2, UNIX
- Supersedes: dmake: Volume 19, Issue 22-58
-
- ---- Cut Here and feed the following to sh ----
- # this is dmake.shar.05 (part 5 of a multipart archive)
- # do not concatenate these parts, unpack them in order with /bin/sh
- # file dmake/dbug/malloc/malloc.3 continued
- #
- if test ! -r _shar_seq_.tmp; then
- echo 'Please unpack part 1 first!'
- exit 1
- fi
- (read Scheck
- if test "$Scheck" != 5; then
- echo Please unpack part "$Scheck" next!
- exit 1
- else
- exit 0
- fi
- ) < _shar_seq_.tmp || exit 1
- if test -f _shar_wnt_.tmp; then
- sed 's/^X//' << 'SHAR_EOF' >> 'dmake/dbug/malloc/malloc.3' &&
- of this check is considered a "warning level error" (described later) and
- is handled accordingly.
- .P
- 7. Run time checking can include verification of the malloc chain at each
- and every call to one of the malloc functions or manually by calling the
- malloc_chain_check function.
- .P
- 6. When a problem is found, the action taken is specified at runtime by
- environment variables or at compile time by the use of the mallopt()
- function.
- .P
- There are two arbitrary levels of errors, warning and fatal, that this
- library will detect. They are broken down as follows:
- .P
- .nf
- .in +.25i
- Warning messages include:
- .sp
- .in +.5i
- .ti -.25i
- Calling free with a bad pointer
- .br
- .ti -.25i
- Calling a bstring/string/memory (3) function which will go beyond
- the end of a malloc block. Note that the library function is
- not modified to refuse the operation.
- .sp
- .in -.5i
- Fatal errors are:
- .in +.5i
- .ti -.25i
- Detectable corruption to the malloc chain.
- .in -.5i
- .in -.25i
- .P
- The error handling for each level (warning or fatal) are specified using
- environment variables or mallopt(). The coding for the error handling is
- as follows:
- .sp
- .nf
- .in +.5i
- .ti -.25i
- X 0 - continue operations
- .ti -.25i
- X 1 - drop core and exit
- .ti -.25i
- X 2 - just exit
- .ti -.25i
- X 3 - drop core, but continue executing. Core files will
- be placed into core.[PID].[counter] i.e: core.00123.001
- .ti -.25i
- 128 - dump malloc chain and continue
- .ti -.25i
- 129 - dump malloc chain, dump core, and exit
- .ti -.25i
- 130 - dump malloc chain, exit
- .ti -.25i
- 131 - dump malloc chain, dump core, continue processing
- .in -.5i
- .P
- In addition error messages can be placed into an error file.
- .P
- \fBmalloc_opt\fP() is used to set the malloc debugging options. The
- following options can be set:
- .br
- .sp
- .in +.5i
- MALLOC_WARN - set the error handling for warning level errors. \fBval.i\fP is
- an integer that can contain any one of the following values:
- .sp
- .in +.5i
- M_HANDLE_IGNORE - ignore error
- .br
- M_HANDLE_ABORT - drop core and exit
- .br
- M_HANDLE_EXIT - just exit (no core drop)
- .br
- M_HANDLE_CORE - drop core, but keep on going
- .br
- .in -.5i
- .sp
- In addition, M_HANDLE_DUMP may be or'd in to cause a dump of the current
- malloc chain.
- .br
- .sp
- MALLOC_FATAL - set the error handling for fatal level errors. \fBval.i\fP is
- equivalent to \fBval.i\fP for MALLOC_WARN.
- .br
- .sp
- MALLOC_ERRFILE - set the destination for malloc error messages. \fBval.str\fP
- is a pointer to a character string containing the name of the file to be used
- for error messages.
- .br
- .sp
- MALLOC_CKCHAIN - set the malloc chain checking flag. If \fBval.i\fP is
- non-zero, chain checking at every call to malloc is turned on.
- .br
- .sp
- For example, to set up the session to generate a core file for
- every malloc warning, to drop core and exit on a malloc fatal, and
- to log all messages to the file "malloc_log" do the following:
- .sp
- .nf
- .in +.5i
- #include <malloc.h>
- malloc_opt(MALLOC_WARN,131);
- malloc_opt(MALLOC_FATAL,1);
- malloc_opt(MALLOC_ERRFILE,"malloc_log");
- .in -.5i
- .fi
- .in -.5i
- .sp
- \fBmalloc_opt\fP() can be used to set/alter the debugging options at any
- time.
- .P
- \fBmalloc_dump\fP() will dump a table of the malloc arena showing all
- allocated/freed segments and the first few bytes of data in each segment.
- \fBfd\fP is the file descriptor to write the data to.
- .P
- \fBmalloc_chain_check\fP() will check the status of the malloc arena.
- If \fBflag\fP is non-zero, an error found in the chain will cause a
- fatal error. \fBmalloc_chain_check\fP() returns zero when there are no
- problems found in the malloc chain, non-zero otherwise.
- .SH "ENVIRONMENT VARIABLES"
- Environment variables can be used to control error handling, error logging
- and malloc chain checking at run time. The following environment variables
- are used:
- .P
- MALLOC_WARN - specifies the error handling for warning errors
- .br
- MALLOC_FATAL - specifies the error handling for fatal errors
- .br
- MALLOC_ERRFILE - specifies the error log file for error messages.
- .br
- MALLOC_CKCHAIN - if 1, turns on malloc chain checking at every call to any
- of the malloc functions.
- .P
- For example, to set up the session to generate a core file for
- every malloc warning, to drop core and exit on a malloc fatal, and
- to log all messages to the file "malloc_log" do the following:
- .sp
- .nf
- .in +.5i
- MALLOC_WARN=131
- MALLOC_FATAL=1
- MALLOC_ERRFILE=malloc_log
- X
- export MALLOC_WARN MALLOC_FATAL MALLOC_ERRFILE
- .in -.5i
- .fi
- .SH WARNINGS
- This malloc library and it's associated string and memory functions are
- much less efficient than the standard functions due in part to the extra
- error checking. You do not want to use this library when generating a
- production (i.e. releasable) version of your software. It should only
- be used during development and testing.
- .SH SEE ALSO
- stat(2)
- .SH AUTHOR
- Conor P. Cahill
- Virtual Technologies Incorporated
- .sp
- uunet!virtech!cpcahil
- SHAR_EOF
- chmod 0640 dmake/dbug/malloc/malloc.3 ||
- echo 'restore of dmake/dbug/malloc/malloc.3 failed'
- Wc_c="`wc -c < 'dmake/dbug/malloc/malloc.3'`"
- test 6712 -eq "$Wc_c" ||
- echo 'dmake/dbug/malloc/malloc.3: original size 6712, current size' "$Wc_c"
- rm -f _shar_wnt_.tmp
- fi
- # ============= dmake/dbug/malloc/malloc.c ==============
- if test -f 'dmake/dbug/malloc/malloc.c' -a X"$1" != X"-c"; then
- echo 'x - skipping dmake/dbug/malloc/malloc.c (File already exists)'
- rm -f _shar_wnt_.tmp
- else
- > _shar_wnt_.tmp
- sed 's/^X//' << 'SHAR_EOF' > 'dmake/dbug/malloc/malloc.c' &&
- /*
- X * (c) Copyright 1990 Conor P. Cahill (uunet!virtech!cpcahil).
- X * You may copy, distribute, and use this software as long as this
- X * copyright statement is not removed.
- X */
- #include <stdio.h>
- #include <fcntl.h>
- #include "malloc.h"
- #include "tostring.h"
- X
- /*
- X * Function: malloc()
- X *
- X * Purpose: memory allocator
- X *
- X * Arguments: size - size of data area needed
- X *
- X * Returns: pointer to allocated area, or NULL if unable
- X * to allocate addtional data.
- X *
- X * Narrative:
- X *
- X */
- #ifndef lint
- static
- char rcs_hdr[] = "$Id: malloc.c,v 1.1 1992/01/24 03:29:05 dvadura Exp $";
- #endif
- X
- extern int malloc_checking;
- char * malloc_data_start;
- char * malloc_data_end;
- struct mlist * malloc_end;
- int malloc_errfd = 2;
- int malloc_errno;
- int malloc_fatal_level = M_HANDLE_CORE;
- struct mlist malloc_start;
- int malloc_warn_level;
- void malloc_memset();
- X
- char *
- malloc(size)
- X unsigned int size;
- {
- X char * func = "malloc";
- X char * getenv();
- X void malloc_fatal();
- X void malloc_init();
- X void malloc_split();
- X void malloc_warning();
- X unsigned int need;
- X struct mlist * oldptr;
- X struct mlist * ptr;
- X char * sbrk();
- X
- X /*
- X * If this is the first call to malloc...
- X */
- X if( malloc_data_start == (char *) 0 )
- X {
- X malloc_init();
- X }
- X
- X /*
- X * If malloc chain checking is on, go do it.
- X */
- X if( malloc_checking )
- X {
- X (void) malloc_chain_check(1);
- X }
- X
- X /*
- X * always make sure there is at least on extra byte in the malloc
- X * area so that we can verify that the user does not overrun the
- X * data area.
- X */
- X size++;
- X
- X /*
- X * Now look for a free area of memory of size bytes...
- X */
- X oldptr = NULL;
- X for(ptr = &malloc_start; ; ptr = ptr->next)
- X {
- X /*
- X * Since the malloc chain is a forward only chain, any
- X * pointer that we get should always be positioned in
- X * memory following the previous pointer. If this is not
- X * so, we must have a corrupted chain.
- X */
- X if( ptr )
- X {
- X if( IsLess(ptr,oldptr) )
- X {
- X malloc_errno = M_CODE_CHAIN_BROKE;
- X malloc_fatal(func);
- X return(NULL);
- X }
- X oldptr = ptr;
- X }
- X else if( oldptr != malloc_end )
- X {
- X /*
- X * This should never happen. If it does, then
- X * we got a real problem.
- X */
- X malloc_errno = M_CODE_NO_END;
- X malloc_fatal(func);
- X return(NULL);
- X }
- X
- X
- X /*
- X * if this element is already in use...
- X */
- X if( ptr && ((ptr->flag & M_INUSE) != 0) )
- X {
- X continue;
- X }
- X
- X /*
- X * if there isn't room for this block..
- X */
- X if( ptr && (ptr->s.size < size) )
- X {
- X continue;
- X }
- X
- X /*
- X * If ptr is null, we have run out of memory and must sbrk more
- X */
- X if( ptr == NULL )
- X {
- X need = (size + M_SIZE) * (size > 10*1024 ? 1:2);
- X if( need < M_BLOCKSIZE )
- X {
- X need = M_BLOCKSIZE;
- X }
- X else if( need & (M_BLOCKSIZE-1) )
- X {
- X need &= ~(M_BLOCKSIZE-1);
- X need += M_BLOCKSIZE;
- X }
- X ptr = (struct mlist *) sbrk((int)need);
- X if( ptr == (struct mlist *) -1 )
- X {
- X malloc_errno = M_CODE_NOMORE_MEM;
- X malloc_fatal(func);
- X }
- X malloc_data_end = sbrk((int)0);
- X
- X ptr->prev = oldptr;
- X ptr->next = (struct mlist *) 0;
- X ptr->s.size = need - M_SIZE;
- X ptr->flag = M_MAGIC;
- X
- X oldptr->next = ptr;
- X malloc_end = ptr;
- X
- X
- X } /* if( ptr ==... */
- X
- X /*
- X * Now ptr points to a memory location that can store
- X * this data, so lets go to work.
- X */
- X
- X ptr->r_size = size; /* save requested size */
- X ptr->flag |= M_INUSE;
- X
- X /*
- X * split off unneeded data area in this block, if possible...
- X */
- X malloc_split(ptr);
- X
- X /*
- X * re-adjust the requested size so that it is what the user
- X * actually requested...
- X */
- X
- X ptr->r_size--;
- X
- X /*
- X * just to make sure that noone is misusing malloced
- X * memory without initializing it, lets set it to
- X * all '\01's. We call local_memset() because memset()
- X * may be checking for malloc'd ptrs and this isn't
- X * a malloc'd ptr yet.
- X */
- X malloc_memset(ptr->data,M_FILL,(int)ptr->s.size);
- X
- X return( ptr->data);
- X
- X } /* for(... */
- X
- } /* malloc(... */
- X
- /*
- X * Function: malloc_split()
- X *
- X * Purpose: to split a malloc segment if there is enough room at the
- X * end of the segment that isn't being used
- X *
- X * Arguments: ptr - pointer to segment to split
- X *
- X * Returns: nothing of any use.
- X *
- X * Narrative:
- X * get the needed size of the module
- X * round the size up to appropriat boundry
- X * calculate amount of left over space
- X * if there is enough left over space
- X * create new malloc block out of remainder
- X * if next block is free
- X * join the two blocks together
- X * fill new empty block with free space filler
- X * re-adjust pointers and size of current malloc block
- X *
- X *
- X *
- X * Mod History:
- X * 90/01/27 cpcahil Initial revision.
- X */
- void
- malloc_split(ptr)
- X struct mlist * ptr;
- {
- X extern struct mlist * malloc_end;
- X void malloc_join();
- X int rest;
- X int size;
- X struct mlist * tptr;
- X
- X size = ptr->r_size;
- X
- X /*
- X * roundup size to the appropriate boundry
- X */
- X
- X M_ROUNDUP(size);
- X
- X /*
- X * figure out how much room is left in the array.
- X * if there is enough room, create a new mlist
- X * structure there.
- X */
- X
- X if( ptr->s.size > size )
- X {
- X rest = ptr->s.size - size;
- X }
- X else
- X {
- X rest = 0;
- X }
- X
- X if( rest > (M_SIZE+M_RND) )
- X {
- X tptr = (struct mlist *) (ptr->data+size);
- X tptr->prev = ptr;
- X tptr->next = ptr->next;
- X tptr->flag = M_MAGIC;
- X tptr->s.size = rest - M_SIZE;
- X
- X /*
- X * If possible, join this segment with the next one
- X */
- X
- X malloc_join(tptr, tptr->next,0,0);
- X
- X if( tptr->next )
- X {
- X tptr->next->prev = tptr;
- X }
- X
- X malloc_memset(tptr->data,M_FREE_FILL, (int)tptr->s.size);
- X
- X ptr->next = tptr;
- X ptr->s.size = size;
- X
- X if( malloc_end == ptr )
- X {
- X malloc_end = tptr;
- X }
- X }
- X
- } /* malloc_split(... */
- X
- /*
- X * Function: malloc_join()
- X *
- X * Purpose: to join two malloc segments together (if possible)
- X *
- X * Arguments: ptr - pointer to segment to join to.
- X * nextptr - pointer to next segment to join to ptr.
- X *
- X * Returns: nothing of any values.
- X *
- X * Narrative:
- X *
- X * Mod History:
- X * 90/01/27 cpcahil Initial revision.
- X */
- void
- malloc_join(ptr,nextptr, inuse_override, fill_flag)
- X struct mlist * ptr;
- X struct mlist * nextptr;
- X int inuse_override;
- X int fill_flag;
- {
- X unsigned int newsize;
- X
- X if( ptr && ! (inuse_override || (ptr->flag & M_INUSE)) &&
- X nextptr && ! (nextptr->flag & M_INUSE) &&
- X ((ptr->data+ptr->s.size) == (char *) nextptr) )
- X {
- X if( malloc_end == nextptr )
- X {
- X malloc_end = ptr;
- X }
- X ptr->next = nextptr->next;
- X newsize = nextptr->s.size + M_SIZE;
- X
- X /*
- X * if we are to fill and this segment is in use,
- X * fill in with M_FILL newly added space...
- X */
- X
- X if(fill_flag && (ptr->flag & M_INUSE) )
- X {
- X malloc_memset(ptr->data+ptr->s.size,
- X M_FILL, (int)(nextptr->s.size + M_SIZE));
- X }
- X
- X ptr->s.size += newsize;
- X if( ptr->next )
- X {
- X ptr->next->prev = ptr;
- X }
- X }
- X
- } /* malloc_join(... */
- X
- X
- /*
- X * The following mess is just to ensure that the versions of these functions in
- X * the current library are included (to make sure that we don't accidentaly get
- X * the libc versions. (This is the lazy man's -u ld directive)
- X */
- X
- void free();
- int strcmp();
- int memcmp();
- char * realloc();
- X
- void (*malloc_void_funcs[])() =
- {
- X free,
- };
- X
- int (*malloc_int_funcs[])() =
- {
- X strcmp,
- X memcmp,
- };
- X
- char * (*malloc_char_star_funcs[])() =
- {
- X realloc,
- };
- X
- /*
- X * This is malloc's own memset which is used without checking the parameters.
- X */
- X
- void
- malloc_memset(ptr,byte,len)
- X char * ptr;
- X char byte;
- X int len;
- {
- X
- X while(len-- > 0)
- X {
- X *ptr++ = byte;
- X }
- X
- } /* malloc_memset(... */
- X
- /*
- X * Function: malloc_fatal()
- X *
- X * Purpose: to display fatal error message and take approrpriate action
- X *
- X * Arguments: funcname - name of function calling this routine
- X *
- X * Returns: nothing of any value
- X *
- X * Narrative:
- X *
- X * Notes: This routine does not make use of any libc functions to build
- X * and/or disply the error message. This is due to the fact that
- X * we are probably at a point where malloc is having a real problem
- X * and we don't want to call any function that may use malloc.
- X */
- void
- malloc_fatal(funcname)
- X char * funcname;
- {
- X char errbuf[128];
- X void exit();
- X void malloc_err_handler();
- X extern char * malloc_err_strings[];
- X extern int malloc_errno;
- X extern int malloc_fatal_level;
- X char * s;
- X char * t;
- X
- X s = errbuf;
- X t = "Fatal error: ";
- X while( *s = *t++)
- X {
- X s++;
- X }
- X t = funcname;
- X while( *s = *t++)
- X {
- X s++;
- X }
- X
- X t = "(): ";
- X while( *s = *t++)
- X {
- X s++;
- X }
- X
- X t = malloc_err_strings[malloc_errno];
- X while( *s = *t++)
- X {
- X s++;
- X }
- X
- X *(s++) = '\n';
- X
- X if( write(malloc_errfd,errbuf,(unsigned)(s-errbuf)) != (s-errbuf))
- X {
- X (void) write(2,"I/O error to error file\n",(unsigned)24);
- X exit(110);
- X }
- X malloc_err_handler(malloc_fatal_level);
- X
- } /* malloc_fatal(... */
- X
- /*
- X * Function: malloc_warning()
- X *
- X * Purpose: to display warning error message and take approrpriate action
- X *
- X * Arguments: funcname - name of function calling this routine
- X *
- X * Returns: nothing of any value
- X *
- X * Narrative:
- X *
- X * Notes: This routine does not make use of any libc functions to build
- X * and/or disply the error message. This is due to the fact that
- X * we are probably at a point where malloc is having a real problem
- X * and we don't want to call any function that may use malloc.
- X */
- void
- malloc_warning(funcname)
- X char * funcname;
- {
- X char errbuf[128];
- X void exit();
- X void malloc_err_handler();
- X extern char * malloc_err_strings[];
- X extern int malloc_errno;
- X extern int malloc_warn_level;
- X char * s;
- X char * t;
- X
- X s = errbuf;
- X t = "Warning: ";
- X while( *s = *t++)
- X {
- X s++;
- X }
- X t = funcname;
- X while( *s = *t++)
- X {
- X s++;
- X }
- X
- X t = "(): ";
- X while( *s = *t++)
- X {
- X s++;
- X }
- X
- X t = malloc_err_strings[malloc_errno];
- X while( *s = *t++)
- X {
- X s++;
- X }
- X
- X *(s++) = '\n';
- X
- X if( write(malloc_errfd,errbuf,(unsigned)(s-errbuf)) != (s-errbuf))
- X {
- X (void) write(2,"I/O error to error file\n",(unsigned)24);
- X exit(110);
- X }
- X
- X malloc_err_handler(malloc_warn_level);
- X
- } /* malloc_warning(... */
- X
- /*
- X * Function: malloc_err_handler()
- X *
- X * Purpose: to take the appropriate action for warning and/or fatal
- X * error conditions.
- X *
- X * Arguments: level - error handling level
- X *
- X * Returns: nothing of any value
- X *
- X * Narrative:
- X *
- X * Notes: This routine does not make use of any libc functions to build
- X * and/or disply the error message. This is due to the fact that
- X * we are probably at a point where malloc is having a real problem
- X * and we don't want to call any function that may use malloc.
- X */
- void
- malloc_err_handler(level)
- {
- X void exit();
- X void malloc_dump();
- X extern int malloc_errfd;
- X
- X if( level & M_HANDLE_DUMP )
- X {
- X malloc_dump(malloc_errfd);
- X }
- X
- X switch( level & ~M_HANDLE_DUMP )
- X {
- X /*
- X * If we are to drop a core file and exit
- X */
- X case M_HANDLE_ABORT:
- X (void) abort();
- X break;
- X
- X /*
- X * If we are to exit..
- X */
- X case M_HANDLE_EXIT:
- X exit(200);
- X break;
- X
- #ifndef __MSDOS__
- X /*
- X * If we are to dump a core, but keep going on our merry way
- X */
- X case M_HANDLE_CORE:
- X {
- X int pid;
- X
- X /*
- X * fork so child can abort (and dump core)
- X */
- X if( (pid = fork()) == 0 )
- X {
- X (void) write(2,"Child dumping core\n",
- X (unsigned)9);
- X (void) abort();
- X }
- X
- X /*
- X * wait for child to finish dumping core
- X */
- X while( wait((int *)0) != pid)
- X {
- X }
- X
- X /*
- X * Move core file to core.pid.cnt so
- X * multiple cores don't overwrite each
- X * other.
- X */
- X if( access("core",0) == 0 )
- X {
- X static int corecnt;
- X char filenam[32];
- X filenam[0] = 'c';
- X filenam[1] = 'o';
- X filenam[2] = 'r';
- X filenam[3] = 'e';
- X filenam[4] = '.';
- X (void)tostring(filenam+5,getpid(),
- X 5, B_DEC, '0');
- X filenam[10] = '.';
- X (void)tostring(filenam+11,corecnt++,
- X 3, B_DEC, '0');
- X filenam[14] = '\0';
- X (void) unlink(filenam);
- X if( link("core",filenam) == 0)
- X {
- X (void) unlink("core");
- X }
- X }
- X }
- #endif
- X
- X
- X /*
- X * If we are to just ignore the error and keep on processing
- X */
- X case M_HANDLE_IGNORE:
- X break;
- X
- X } /* switch(... */
- X
- } /* malloc_err_handler(... */
- X
- /*
- X * $Log: malloc.c,v $
- X * Revision 1.1 1992/01/24 03:29:05 dvadura
- X * dmake Version 3.8, Initial revision
- X *
- X * Revision 1.6 90/05/11 00:13:09 cpcahil
- X * added copyright statment
- X *
- X * Revision 1.5 90/02/25 11:01:18 cpcahil
- X * added support for malloc chain checking.
- X *
- X * Revision 1.4 90/02/24 21:50:21 cpcahil
- X * lots of lint fixes
- X *
- X * Revision 1.3 90/02/24 14:51:18 cpcahil
- X * 1. changed malloc_fatal and malloc_warn to use malloc_errno and be passed
- X * the function name as a parameter.
- X * 2. Added several function headers.
- X * 3. Changed uses of malloc_fatal/warning to conform to new usage.
- X *
- X * Revision 1.2 90/02/23 18:05:23 cpcahil
- X * fixed open of error log to use append mode.
- X *
- X * Revision 1.1 90/02/22 23:17:43 cpcahil
- X * Initial revision
- X *
- X */
- SHAR_EOF
- chmod 0640 dmake/dbug/malloc/malloc.c ||
- echo 'restore of dmake/dbug/malloc/malloc.c failed'
- Wc_c="`wc -c < 'dmake/dbug/malloc/malloc.c'`"
- test 12856 -eq "$Wc_c" ||
- echo 'dmake/dbug/malloc/malloc.c: original size 12856, current size' "$Wc_c"
- rm -f _shar_wnt_.tmp
- fi
- # ============= dmake/dbug/malloc/malloc.h ==============
- if test -f 'dmake/dbug/malloc/malloc.h' -a X"$1" != X"-c"; then
- echo 'x - skipping dmake/dbug/malloc/malloc.h (File already exists)'
- rm -f _shar_wnt_.tmp
- else
- > _shar_wnt_.tmp
- sed 's/^X//' << 'SHAR_EOF' > 'dmake/dbug/malloc/malloc.h' &&
- /*
- X * (c) Copyright 1990 Conor P. Cahill (uunet!virtech!cpcahil).
- X * You may copy, distribute, and use this software as long as this
- X * copyright statement is not removed.
- X */
- /*
- X * $Id: malloc.h,v 1.1 1992/01/24 03:29:06 dvadura Exp $
- X */
- struct mlist
- {
- X struct mlist * next; /* next entry in chain */
- X struct mlist * prev; /* prev entry in chain */
- X int flag; /* inuse flag */
- X unsigned int r_size; /* requested size */
- X union
- X {
- X unsigned int size; /* actual size */
- X double unused_just_for_alignment;
- X } s;
- X char data[4];
- };
- X
- #define M_SIZE ((int)(char *)((struct mlist *)0)->data)
- #define M_RND 0x08
- X
- #define M_INUSE 0x01
- #define M_MAGIC 0x03156100
- X
- #define M_BLOCKSIZE (1024*8)
- X
- #define M_FILL '\01'
- #define M_FREE_FILL '\02'
- X
- #define M_ROUNDUP(size) {\
- X if( size & (M_RND-1) ) \
- X { \
- X size &= ~(M_RND-1); \
- X size += M_RND; \
- X } \
- X }
- X
- /*
- X * Malloc warning/fatal error handler defines...
- X */
- #define M_HANDLE_DUMP 0x80 /* 128 */
- #define M_HANDLE_IGNORE 0
- #define M_HANDLE_ABORT 1
- #define M_HANDLE_EXIT 2
- #define M_HANDLE_CORE 3
- X
- /*
- X * Mallopt commands and defaults
- X */
- X
- #define MALLOC_WARN 1 /* set malloc warning handling */
- #define MALLOC_FATAL 2 /* set malloc fatal handling */
- #define MALLOC_ERRFILE 3 /* specify malloc error file */
- #define MALLOC_CKCHAIN 4 /* turn on chain checking */
- union malloptarg
- {
- X int i;
- X char * str;
- };
- X
- /*
- X * Malloc warning/fatal error codes
- X */
- X
- #define M_CODE_CHAIN_BROKE 1 /* malloc chain is broken */
- #define M_CODE_NO_END 2 /* chain end != endptr */
- #define M_CODE_BAD_PTR 3 /* pointer not in malloc area */
- #define M_CODE_BAD_MAGIC 4 /* bad magic number in header */
- #define M_CODE_BAD_CONNECT 5 /* chain poingers corrupt */
- #define M_CODE_OVERRUN 6 /* data overrun in malloc seg */
- #define M_CODE_REUSE 7 /* reuse of freed area */
- #define M_CODE_NOT_INUSE 8 /* pointer is not in use */
- #define M_CODE_NOMORE_MEM 9 /* no more memory available */
- #define M_CODE_OUTOF_BOUNDS 10 /* gone beyound bounds */
- X
- void malloc_warning();
- void malloc_fatal();
- void malloc_check_data();
- void malloc_check_str();
- void malloc_verify();
- X
- /*
- X * $Log: malloc.h,v $
- X * Revision 1.1 1992/01/24 03:29:06 dvadura
- X * dmake Version 3.8, Initial revision
- X *
- X * Revision 1.4 90/08/29 22:23:38 cpcahil
- X * fixed mallopt to use a union as an argument.
- X *
- X * Revision 1.3 90/05/11 11:04:10 cpcahil
- X * took out some extraneous lines
- X *
- X * Revision 1.2 90/05/11 00:13:09 cpcahil
- X * added copyright statment
- X *
- X * Revision 1.1 90/02/23 07:09:03 cpcahil
- X * Initial revision
- X *
- X */
- SHAR_EOF
- chmod 0640 dmake/dbug/malloc/malloc.h ||
- echo 'restore of dmake/dbug/malloc/malloc.h failed'
- Wc_c="`wc -c < 'dmake/dbug/malloc/malloc.h'`"
- test 2578 -eq "$Wc_c" ||
- echo 'dmake/dbug/malloc/malloc.h: original size 2578, current size' "$Wc_c"
- rm -f _shar_wnt_.tmp
- fi
- # ============= dmake/dbug/malloc/mallopt.c ==============
- if test -f 'dmake/dbug/malloc/mallopt.c' -a X"$1" != X"-c"; then
- echo 'x - skipping dmake/dbug/malloc/mallopt.c (File already exists)'
- rm -f _shar_wnt_.tmp
- else
- > _shar_wnt_.tmp
- sed 's/^X//' << 'SHAR_EOF' > 'dmake/dbug/malloc/mallopt.c' &&
- /*
- X * (c) Copyright 1990 Conor P. Cahill (uunet!virtech!cpcahil).
- X * You may copy, distribute, and use this software as long as this
- X * copyright statement is not removed.
- X */
- #include <stdio.h>
- #include <fcntl.h>
- #include "malloc.h"
- X
- /*
- X * Function: mallopt()
- X *
- X * Purpose: to set options for the malloc debugging library
- X *
- X * Arguments: none
- X *
- X * Returns: nothing of any value
- X *
- X * Narrative:
- X *
- X */
- X
- #ifndef lint
- static
- char rcs_hdr[] = "$Id: mallopt.c,v 1.1 1992/01/24 03:29:07 dvadura Exp $";
- #endif
- X
- int
- mallopt(cmd,value)
- X int cmd;
- X union malloptarg value;
- {
- X int i;
- X extern int malloc_checking;
- X extern char * malloc_data_start;
- X extern int malloc_errfd;
- X extern int malloc_fatal_level;
- X void malloc_init();
- X extern int malloc_warn_level;
- X register char * s;
- X
- X /*
- X * If not initialized...
- X */
- X if( malloc_data_start == (char *) 0)
- X {
- X malloc_init();
- X }
- X
- X
- X switch(cmd)
- X {
- X case MALLOC_WARN:
- X malloc_warn_level = value.i;
- X break;
- X
- X case MALLOC_FATAL:
- X malloc_fatal_level = value.i;
- X break;
- X
- X case MALLOC_CKCHAIN:
- X malloc_checking = value.i;
- X break;
- X
- X case MALLOC_ERRFILE:
- X
- X i = open(value.str,O_CREAT|O_APPEND|O_WRONLY,0666);
- X if( i == -1 )
- X {
- X (void) write(2,
- X "Unable to open malloc error file: ",
- X (unsigned) 34);
- X for(s=value.str; *s; s++)
- X {
- X /* do nothing */;
- X }
- X (void) write(2,value.str,
- X (unsigned)(s-value.str));
- X (void) write(2,"\n",(unsigned)1);
- X }
- X else
- X {
- X if( malloc_errfd != 2 )
- X {
- X (void) close(malloc_errfd);
- X }
- X malloc_errfd = i;
- X }
- X
- X break;
- X
- X default:
- X return(1);
- X }
- X
- X return(0);
- }
- X
- /*
- X * $Log: mallopt.c,v $
- X * Revision 1.1 1992/01/24 03:29:07 dvadura
- X * dmake Version 3.8, Initial revision
- X *
- X * Revision 1.6 90/08/29 22:23:36 cpcahil
- X * fixed mallopt to use a union as an argument.
- X *
- X * Revision 1.5 90/08/29 21:22:51 cpcahil
- X * miscellaneous lint fixes
- X *
- X * Revision 1.4 90/05/11 00:13:10 cpcahil
- X * added copyright statment
- X *
- X * Revision 1.3 90/02/25 11:03:26 cpcahil
- X * changed to return int so that it agrees with l libmalloc.a's mallopt()
- X *
- X * Revision 1.2 90/02/25 11:01:21 cpcahil
- X * added support for malloc chain checking.
- X *
- X * Revision 1.1 90/02/24 21:50:24 cpcahil
- X * Initial revision
- X *
- X * Revision 1.1 90/02/24 17:10:53 cpcahil
- X * Initial revision
- X *
- X */
- SHAR_EOF
- chmod 0640 dmake/dbug/malloc/mallopt.c ||
- echo 'restore of dmake/dbug/malloc/mallopt.c failed'
- Wc_c="`wc -c < 'dmake/dbug/malloc/mallopt.c'`"
- test 2372 -eq "$Wc_c" ||
- echo 'dmake/dbug/malloc/mallopt.c: original size 2372, current size' "$Wc_c"
- rm -f _shar_wnt_.tmp
- fi
- # ============= dmake/dbug/malloc/memory.c ==============
- if test -f 'dmake/dbug/malloc/memory.c' -a X"$1" != X"-c"; then
- echo 'x - skipping dmake/dbug/malloc/memory.c (File already exists)'
- rm -f _shar_wnt_.tmp
- else
- > _shar_wnt_.tmp
- sed 's/^X//' << 'SHAR_EOF' > 'dmake/dbug/malloc/memory.c' &&
- /*
- X * (c) Copyright 1990 Conor P. Cahill (uunet!virtech!cpcahil).
- X * You may copy, distribute, and use this software as long as this
- X * copyright statement is not removed.
- X */
- X
- #ifndef lint
- static
- char rcs_hdr[] = "$Id: memory.c,v 1.1 1992/01/24 03:29:08 dvadura Exp $";
- #endif
- X
- void malloc_check_data();
- X
- char *
- memccpy(ptr1, ptr2, ch, len)
- X register char * ptr1;
- X register char * ptr2;
- X int len;
- X int ch;
- {
- X int check;
- X register int i;
- X char * rtn;
- X
- X /*
- X * I know that the assignment could be done in the following, but
- X * I wanted to perform a check before any assignment, so first I
- X * determine the length, check the pointers and then do the assignment.
- X */
- X for( i=0; (i < len) && (ptr2[i] != ch); i++)
- X {
- X }
- X if( ptr2[i] == ch )
- X {
- X check = i+1;
- X }
- X else
- X {
- X check = len;
- X }
- X
- X malloc_check_data("memccpy", ptr1, check);
- X malloc_check_data("memccpy", ptr2, check);
- X
- X /*
- X * if we found the character...
- X */
- X
- X if( i < len )
- X {
- X rtn = ptr1+i+1;
- X i++;
- X }
- X else
- X {
- X rtn = (char *) 0;
- X }
- X
- X while( i-- )
- X {
- X *(ptr1++) = *(ptr2++);
- X }
- X
- X return(rtn);
- }
- X
- char *
- memchr(ptr1,ch,len)
- X register char * ptr1;
- X register int ch;
- X int len;
- {
- X int i;
- X
- X for( i=0; (i < len) && (ptr1[i] != (char) ch); i++)
- X {
- X }
- X
- X malloc_check_data("memchr", ptr1, i);
- X
- X if( i < len )
- X {
- X return( ptr1+i );
- X }
- X else
- X {
- X return( (char *) 0);
- X }
- }
- X
- char *
- memcpy(ptr1, ptr2, len)
- X register char * ptr1;
- X register char * ptr2;
- X register int len;
- {
- X char * rtn = ptr1;
- X
- X malloc_check_data("memcpy", ptr1, len);
- X malloc_check_data("memcpy", ptr2, len);
- X
- X /*
- X * while the normal memcpy does not guarrantee that it will
- X * handle overlapping memory correctly, we will try...
- X */
- X if( ptr1 > ptr2 && ptr1 < (ptr2+len))
- X {
- X ptr1 += (len-1);
- X ptr2 += (len-1);
- X while( len-- > 0 )
- X {
- X *(ptr1--) = *(ptr2--);
- X }
- X }
- X else
- X {
- X while( len-- > 0 )
- X {
- X *(ptr1++) = *(ptr2++);
- X }
- X }
- X
- X return(rtn);
- }
- X
- int
- memcmp(ptr1, ptr2, len)
- X register char * ptr1;
- X register char * ptr2;
- X register int len;
- {
- X malloc_check_data("memcpy", ptr1, len);
- X malloc_check_data("memcpy", ptr2, len);
- X
- X while( --len >= 0 && (*ptr1 == *ptr2) )
- X {
- X ptr1++;
- X ptr2++;
- X }
- X
- X /*
- X * If stopped by len, return zero
- X */
- X if( len < 0 )
- X {
- X return(0);
- X }
- X
- X return( *ptr1 - *ptr2 );
- }
- X
- char *
- memset(ptr1, ch, len)
- X register char * ptr1;
- X register int ch;
- X register int len;
- {
- X char * rtn = ptr1;
- X
- X malloc_check_data("memcpy", ptr1, len);
- X
- X while( len-- )
- X {
- X *(ptr1++) = ch;
- X }
- X
- X return(rtn);
- }
- X
- char *
- bcopy(ptr2,ptr1,len)
- X char * ptr2;
- X char * ptr1;
- X int len;
- {
- X return(memcpy(ptr1,ptr2,len));
- }
- X
- char *
- bzero(ptr1,len)
- X char * ptr1;
- X int len;
- {
- X return(memset(ptr1,'\0',len));
- }
- X
- int
- bcmp(ptr2, ptr1, len)
- X char * ptr1;
- X char * ptr2;
- X int len;
- {
- X return( memcmp(ptr1,ptr2,len) );
- }
- X
- /*
- X * $Log: memory.c,v $
- X * Revision 1.1 1992/01/24 03:29:08 dvadura
- X * dmake Version 3.8, Initial revision
- X *
- X * Revision 1.7 90/08/29 21:27:58 cpcahil
- X * fixed value of check in memccpy when character was not found.
- X *
- X * Revision 1.6 90/07/16 20:06:26 cpcahil
- X * fixed several minor bugs found with Henry Spencer's string/mem tester
- X * program.
- X *
- X *
- X * Revision 1.5 90/05/11 15:39:36 cpcahil
- X * fixed bug in memccpy().
- X *
- X * Revision 1.4 90/05/11 00:13:10 cpcahil
- X * added copyright statment
- X *
- X * Revision 1.3 90/02/24 21:50:29 cpcahil
- X * lots of lint fixes
- X *
- X * Revision 1.2 90/02/24 17:29:41 cpcahil
- X * changed $Header to $Id so full path wouldnt be included as part of rcs
- X * id string
- X *
- X * Revision 1.1 90/02/22 23:17:43 cpcahil
- X * Initial revision
- X *
- X */
- SHAR_EOF
- chmod 0640 dmake/dbug/malloc/memory.c ||
- echo 'restore of dmake/dbug/malloc/memory.c failed'
- Wc_c="`wc -c < 'dmake/dbug/malloc/memory.c'`"
- test 3596 -eq "$Wc_c" ||
- echo 'dmake/dbug/malloc/memory.c: original size 3596, current size' "$Wc_c"
- rm -f _shar_wnt_.tmp
- fi
- # ============= dmake/dbug/malloc/mlc_chk.c ==============
- if test -f 'dmake/dbug/malloc/mlc_chk.c' -a X"$1" != X"-c"; then
- echo 'x - skipping dmake/dbug/malloc/mlc_chk.c (File already exists)'
- rm -f _shar_wnt_.tmp
- else
- > _shar_wnt_.tmp
- sed 's/^X//' << 'SHAR_EOF' > 'dmake/dbug/malloc/mlc_chk.c' &&
- /*
- X * (c) Copyright 1990 Conor P. Cahill (uunet!virtech!cpcahil).
- X * You may copy, distribute, and use this software as long as this
- X * copyright statement is not removed.
- X */
- X
- #include <stdio.h>
- #include "malloc.h"
- #include "debug.h"
- X
- #ifndef lint
- static
- char rcs_hdr[] = "$Id: mlc_chk.c,v 1.1 1992/01/24 03:29:09 dvadura Exp $";
- #endif
- X
- extern struct mlist malloc_start;
- extern struct mlist * malloc_end;
- extern char * malloc_data_start;
- extern char * malloc_data_end;
- X
- /*
- X * Function: malloc_in_arena()
- X *
- X * Purpose: to verify address is within malloc arena.
- X *
- X * Arguments: ptr - pointer to verify
- X *
- X * Returns: TRUE - if pointer is within malloc area
- X * FALSE - otherwise
- X *
- X * Narrative:
- X * IF pointer is >= malloc area start AND <= malloc area end
- X * return TRUE
- X * ELSE
- X * return FALSE
- X *
- X * Mod History:
- X * 90/01/24 cpcahil Initial revision.
- X */
- int
- malloc_in_arena(ptr)
- X char * ptr;
- {
- X extern char * malloc_data_start;
- X extern char * malloc_data_end;
- X int rtn = 0;
- X
- X if( ptr >= malloc_data_start && ptr <= malloc_data_end )
- X {
- X rtn = 1;
- X }
- X
- X return(rtn);
- }
- X
- /*
- X * Function: malloc_check_str()
- X *
- X * Arguments: func - name of function calling this routine
- X * str - pointer to area to check
- X *
- X * Purpose: to verify that if str is within the malloc arena, the data
- X * it points to does not extend beyond the applicable region.
- X *
- X * Returns: Nothing of any use (function is void).
- X *
- X * Narrative:
- X * IF pointer is within malloc arena
- X * determin length of string
- X * call malloc_verify() to verify data is withing applicable region
- X * return
- X *
- X * Mod History:
- X * 90/01/24 cpcahil Initial revision.
- X * 90/01/29 cpcahil Added code to ignore recursive calls.
- X */
- void
- malloc_check_str(func,str)
- X char * func;
- X char * str;
- {
- X static int layers;
- X register char * s;
- X
- X if( (layers++ == 0) && malloc_in_arena(str) )
- X {
- X for( s=str; *s; s++)
- X {
- X }
- X
- X malloc_verify(func,str,s-str+1);
- X }
- X
- X layers--;
- }
- X
- /*
- X * Function: malloc_check_strn()
- X *
- X * Arguments: func - name of function calling this routine
- X * str - pointer to area to check
- X * len - max length of string
- X *
- X * Purpose: to verify that if str is within the malloc arena, the data
- X * it points to does not extend beyond the applicable region.
- X *
- X * Returns: Nothing of any use (function is void).
- X *
- X * Narrative:
- X * IF pointer is within malloc arena
- X * determin length of string
- X * call malloc_verify() to verify data is withing applicable region
- X * return
- X *
- X * Mod History:
- X * 90/01/24 cpcahil Initial revision.
- X * 90/01/29 cpcahil Added code to ignore recursive calls.
- X * 90/08/29 cpcahil added length (for strn* functions)
- X */
- void
- malloc_check_strn(func,str,len)
- X char * func;
- X char * str;
- X int len;
- {
- X register int i;
- X static int layers;
- X register char * s;
- X
- X if( (layers++ == 0) && malloc_in_arena(str) )
- X {
- X for( s=str,i=0; (i < len) && *s; s++)
- X {
- X }
- X
- X malloc_verify(func,str,s-str+1);
- X }
- X
- X layers--;
- }
- X
- /*
- X * Function: malloc_check_data()
- X *
- X * Arguments: func - name of function calling this routine
- X * ptr - pointer to area to check
- X * len - length to verify
- X *
- X * Purpose: to verify that if ptr is within the malloc arena, the data
- X * it points to does not extend beyond the applicable region.
- X *
- X * Returns: Nothing of any use (function is void).
- X *
- X * Narrative:
- X * IF pointer is within malloc arena
- X * call malloc_verify() to verify data is withing applicable region
- X * return
- X *
- X * Mod History:
- X * 90/01/24 cpcahil Initial revision.
- X * 90/01/29 cpcahil Added code to ignore recursive calls.
- X */
- void
- malloc_check_data(func,ptr,len)
- X char * func;
- X char * ptr;
- X int len;
- {
- X static int layers;
- X
- X if( layers++ == 0 )
- X {
- X DEBUG3(40,"malloc_check_data(%s,0x%x,%d) called...",
- X func,ptr,len);
- X if( malloc_in_arena(ptr) )
- X {
- X DEBUG0(10,"pointer in malloc arena, verifying...");
- X malloc_verify(func,ptr,len);
- X }
- X }
- X
- X layers--;
- }
- X
- /*
- X * Function: malloc_verify()
- X *
- X * Arguments: func - name of function calling the malloc check routines
- X * ptr - pointer to area to check
- X * len - length to verify
- X *
- X * Purpose: to verify that the data ptr points to does not extend beyond
- X * the applicable malloc region. This function is only called
- X * if it has been determined that ptr points into the malloc arena.
- X *
- X * Returns: Nothing of any use (function is void).
- X *
- X * Narrative:
- X *
- X * Mod History:
- X * 90/01/24 cpcahil Initial revision.
- X */
- void
- malloc_verify(func,ptr,len)
- X char * func;
- X char * ptr;
- X int len;
- {
- X extern struct mlist * malloc_end;
- X extern int malloc_errno;
- X extern struct mlist malloc_start;
- X struct mlist * mptr;
- X
- X DEBUG3(40,"malloc_verify(%s,0x%x,%d) called...", func,ptr,len);
- X /*
- X * Find the malloc block that includes this pointer
- X */
- X mptr = &malloc_start;
- X while( mptr &&
- X ! (((char *)mptr < ptr) && ((mptr->data+mptr->s.size) > ptr) ) )
- X {
- X mptr = mptr->next;
- X }
- X
- X /*
- X * if ptr was not in a malloc block, it must be part of
- X * some direct sbrk() stuff, so just return.
- X */
- X if( ! mptr )
- X {
- X DEBUG1(10,"ptr (0x%x) not found in malloc search", ptr);
- X return;
- X }
- X
- X /*
- X * Now we have a valid malloc block that contains the indicated
- X * pointer. We must verify that it is withing the requested block
- X * size (as opposed to the real block size which is rounded up to
- X * allow for correct alignment).
- X */
- X
- X DEBUG4(60,"Checking 0x%x-0x%x, 0x%x-0x%x",
- X ptr, ptr+len, mptr->data, mptr->data+mptr->r_size);
- X
- X if( (ptr < mptr->data) || ((ptr+len) > (mptr->data+mptr->r_size)) )
- X {
- X DEBUG4(0,"pointer not within region 0x%x-0x%x, 0x%x-0x%x",
- X ptr, ptr+len, mptr->data, mptr->data+mptr->r_size);
- X
- X malloc_errno = M_CODE_OUTOF_BOUNDS;
- X malloc_warning(func);
- X }
- X
- X return;
- }
- X
- /*
- X * $Log: mlc_chk.c,v $
- X * Revision 1.1 1992/01/24 03:29:09 dvadura
- X * dmake Version 3.8, Initial revision
- X *
- X * Revision 1.5 90/08/29 22:23:48 cpcahil
- X * added new function to check on strings up to a specified length
- X * and used it within several strn* functions.
- X *
- X * Revision 1.4 90/05/11 00:13:09 cpcahil
- X * added copyright statment
- X *
- X * Revision 1.3 90/02/24 21:50:22 cpcahil
- X * lots of lint fixes
- X *
- X * Revision 1.2 90/02/24 17:29:38 cpcahil
- X * changed $Header to $Id so full path wouldnt be included as part of rcs
- X * id string
- X *
- X * Revision 1.1 90/02/24 14:57:03 cpcahil
- X * Initial revision
- X *
- X */
- SHAR_EOF
- chmod 0640 dmake/dbug/malloc/mlc_chk.c ||
- echo 'restore of dmake/dbug/malloc/mlc_chk.c failed'
- Wc_c="`wc -c < 'dmake/dbug/malloc/mlc_chk.c'`"
- test 6393 -eq "$Wc_c" ||
- echo 'dmake/dbug/malloc/mlc_chk.c: original size 6393, current size' "$Wc_c"
- rm -f _shar_wnt_.tmp
- fi
- # ============= dmake/dbug/malloc/mlc_chn.c ==============
- if test -f 'dmake/dbug/malloc/mlc_chn.c' -a X"$1" != X"-c"; then
- echo 'x - skipping dmake/dbug/malloc/mlc_chn.c (File already exists)'
- rm -f _shar_wnt_.tmp
- else
- > _shar_wnt_.tmp
- sed 's/^X//' << 'SHAR_EOF' > 'dmake/dbug/malloc/mlc_chn.c' &&
- /*
- X * (c) Copyright 1990 Conor P. Cahill (uunet!virtech!cpcahil).
- X * You may copy, distribute, and use this software as long as this
- X * copyright statement is not removed.
- X */
- #include <stdio.h>
- #include <fcntl.h>
- #include "malloc.h"
- X
- /*
- X * Function: malloc_chain_check()
- X *
- X * Purpose: to verify malloc chain is intact
- X *
- X * Arguments: todo - 0 - just check and return status
- X * 1 - call malloc_warn if error detected
- X *
- X * Returns: 0 - malloc chain intact & no overflows
- X * other - problems detected in malloc chain
- X *
- X * Narrative:
- X *
- X * Notes: If todo is non-zero the malloc_warn function, when called
- X * may not return (i.e. it may exit)
- X *
- X */
- #ifndef lint
- static
- char rcs_hdr[] = "$Id: mlc_chn.c,v 1.1 1992/01/24 03:29:10 dvadura Exp $";
- #endif
- X
- X
- int
- malloc_chain_check(todo)
- X int todo;
- {
- X char * func = "malloc_chain_check";
- X int i;
- X extern char * malloc_data_start;
- X extern char * malloc_data_end;
- X extern struct mlist * malloc_end;
- X extern int malloc_errno;
- X extern struct mlist malloc_start;
- X struct mlist * oldptr;
- X struct mlist * ptr;
- X int rtn = 0;
- X
- X oldptr = &malloc_start;
- X for(ptr = malloc_start.next; ; ptr = ptr->next)
- X {
- X /*
- X * Since the malloc chain is a forward only chain, any
- X * pointer that we get should always be positioned in
- X * memory following the previous pointer. If this is not
- X * so, we must have a corrupted chain.
- X */
- X if( ptr )
- X {
- X if(ptr < oldptr )
- X {
- X malloc_errno = M_CODE_CHAIN_BROKE;
- X if( todo )
- X {
- X malloc_fatal(func);
- X }
- X rtn++;
- X break;
- X }
- X oldptr = ptr;
- X }
- X else
- X {
- X if( oldptr != malloc_end )
- X {
- X /*
- X * This should never happen. If it does, then
- X * we got a real problem.
- X */
- X malloc_errno = M_CODE_NO_END;
- X if( todo )
- X {
- X malloc_fatal(func);
- X }
- X rtn++;
- X }
- X break;
- X }
- X
- X /*
- X * verify that ptr is within the malloc region...
- X * since we started within the malloc chain this should never
- X * happen.
- X */
- X
- X if( ((char *)ptr < malloc_data_start) ||
- X ((char *)ptr > malloc_data_end) )
- X {
- X malloc_errno = M_CODE_BAD_PTR;
- X if( todo )
- X {
- X malloc_fatal(func);
- X }
- X rtn++;
- X break;
- X }
- X
- X /*
- X * verify magic flag is set
- X */
- X
- X if( (ptr->flag&M_MAGIC) != M_MAGIC )
- X {
- X malloc_errno = M_CODE_BAD_MAGIC;
- X if( todo )
- X {
- X malloc_warning(func);
- X }
- SHAR_EOF
- true || echo 'restore of dmake/dbug/malloc/mlc_chn.c failed'
- fi
- echo 'End of part 5, continue with part 6'
- echo 6 > _shar_seq_.tmp
- exit 0
- exit 0 # Just in case...
-