home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3484 < prev    next >
Encoding:
Text File  |  1991-06-14  |  2.9 KB  |  137 lines

  1. Newsgroups: alt.sources,alt.sources.d
  2. From: Andrew Valencia <vandys@sequent.com>
  3. Subject: Re: Paranoid malloc()s?
  4. Message-ID: <1991Jun13.151052.27322@sequent.com>
  5. Date: Thu, 13 Jun 91 15:10:52 GMT
  6.  
  7. ss7m+@andrew.cmu.edu (Steven M. Stadnicki) writes:
  8.  
  9. >Does anyone know where any librarys of this nature can be found?  I've got a
  10. >lurking memory leak somewhere in the code I'm working with, and was hoping for
  11. >something that'll match mallocs and frees and tell me what I'm missing...
  12.  
  13. Well, I don't have quite that.  But here's my malloc/free package for
  14. catching abuse of allocated and use of free'ed or realloc'd memory.  Use
  15. at your own risk!  It uses mmap() and munmap(), so you'll need a later
  16. vintage System V or BSD.
  17.  
  18.                         Andy Valencia
  19.                         vandys@sequent.com
  20.  
  21. /*
  22.  * A brute-force malloc() interface which attempts to back up each
  23.  * allocated piece of data against an invalid page, so that references
  24.  * off the end cause a segmentation violation.
  25.  */
  26. extern char *sbrk();
  27.  
  28. static unsigned long pagesize;
  29.  
  30. char *
  31. malloc(size)
  32.     unsigned size;
  33. {
  34.     static int setup = 0;
  35.     unsigned long l;
  36.     char *p;
  37.  
  38.     /*
  39.      * Adjust up so we can slip a "size" word up front.  Also pad by an
  40.      * extra longword because the C library expects strings to not fall
  41.      * into an invalid page within one word from their end.
  42.      */
  43.     size += sizeof(unsigned) + sizeof(long);
  44.  
  45.     /*
  46.      * Round size to longword boundary
  47.      */
  48.     if (size & (sizeof(long)-1)) {
  49.         size += (sizeof(long) - (size & (sizeof(long)-1)));
  50.     }
  51.  
  52.     /*
  53.      * Get some overhead over with once
  54.      */
  55.     if (!setup) {
  56.         pagesize = getpagesize();
  57.         setup = 1;
  58.     }
  59.  
  60.     /*
  61.      * Make sure break is on a page boundry
  62.      */
  63.     l = (unsigned long)sbrk(0);
  64.     l &= (pagesize-1);
  65.     l = pagesize - l;
  66.     (void)sbrk((int)l);
  67.  
  68.     /*
  69.      * Now allocate one page beyond size needed to hold data
  70.      */
  71.     l = (size + (pagesize-1)) & ~(pagesize-1);
  72.     l = pagesize * (l/pagesize + 1);
  73.     p = sbrk((int)l);
  74.  
  75.     /*
  76.      * Out of memory--foo
  77.      */
  78.     if (p == 0)
  79.         return(p);
  80.  
  81.     /*
  82.      * Unvirtualize last page, return pointer to data backed
  83.      * up against top of remaining memory.
  84.      */
  85.     p += l;
  86.     (void) munmap(p - pagesize, pagesize);
  87.  
  88.     /*
  89.      * Adjust data to back up against the end of the memory
  90.      */
  91.     p -= (pagesize + size);
  92.     *(unsigned *)p = size;
  93.     return (p+sizeof(unsigned));
  94. }
  95.  
  96. /*
  97.  * Free memory.  Unvirtualize and thus catch stale-memory cheaters.
  98.  */
  99. void
  100. free(ptr)
  101.     char *ptr;
  102. {
  103.     unsigned long l;
  104.     int npages;
  105.  
  106.     /*
  107.      * Figure out how big a chunk we're done with
  108.      */
  109.     ptr -= sizeof(unsigned);
  110.     l = *(unsigned *)ptr;
  111.     l = (l + (pagesize-1)) & ~(pagesize-1);
  112.     npages = l/pagesize;
  113.  
  114.     /*
  115.      * Get base of memory, unmap it
  116.      */
  117.     l = (unsigned long)ptr;
  118.     l &= ~(pagesize-1);
  119.     (void) munmap(l, npages*pagesize);
  120. }
  121.  
  122. char *
  123. realloc(ptr, size)
  124.     char *ptr;
  125.     unsigned size;
  126. {
  127.     register char *p;
  128.     unsigned osize;
  129.  
  130.     if ((p = malloc(size)) == 0)
  131.         return(p);
  132.     osize = *(unsigned *)(ptr - sizeof(unsigned));
  133.     memcpy(p, ptr, osize);
  134.     free(ptr);
  135.     return(p);
  136. }
  137.