home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / DMAKE38B.ZIP / DBUG / MALLOC / REALLOC.C < prev    next >
C/C++ Source or Header  |  1992-01-23  |  5KB  |  216 lines

  1. /*
  2.  * (c) Copyright 1990 Conor P. Cahill (uunet!virtech!cpcahil).  
  3.  * You may copy, distribute, and use this software as long as this
  4.  * copyright statement is not removed.
  5.  */
  6. #include <stdio.h>
  7. #include "malloc.h"
  8.  
  9. /*
  10.  * Function:    realloc()
  11.  *
  12.  * Purpose:    to re-allocate a data area.
  13.  *
  14.  * Arguments:    cptr    - pointer to area to reallocate
  15.  *        size    - size to change area to
  16.  *
  17.  * Returns:    pointer to new area (may be same area)
  18.  *
  19.  * Narrative:    verify pointer is within malloc region
  20.  *        obtain mlist pointer from cptr
  21.  *        verify magic number is correct
  22.  *        verify inuse flag is set
  23.  *        verify connection to adjoining segments is correct
  24.  *        save requested size
  25.  *        round-up size to appropriate boundry
  26.  *        IF size is bigger than what is in this segment
  27.  *            try to join next segment to this segment
  28.  *        IF size is less than what is is this segment
  29.  *            determine leftover amount of space
  30.  *        ELSE
  31.  *            allocate new segment of size bites
  32.  *            IF allocation failed
  33.  *                return NULL
  34.  *            copy previous data to new segment
  35.  *            free previous segment
  36.  *            return new pointer
  37.  *        split of extra space in this segment (if any)
  38.  *        clear bytes beyound what they had before
  39.  *        return pointer to data 
  40.  */
  41. #ifndef lint
  42. static
  43. char rcs_hdr[] = "$Id: realloc.c,v 1.1 1992/01/24 03:29:11 dvadura Exp $";
  44. #endif
  45.  
  46. char *
  47. realloc(cptr,size)
  48.     char            * cptr;
  49.     unsigned int          size;
  50. {
  51.     void              free();
  52.     char            * func = "realloc";
  53.     int              i;
  54.     char            * malloc();
  55.     extern int          malloc_checking;
  56.     extern struct mlist     * malloc_end;
  57.     extern int          malloc_errno;
  58.     extern char        * malloc_data_end;
  59.     extern char        * malloc_data_start;
  60.     void              malloc_join();
  61.     void              malloc_memset();
  62.     void              malloc_split();
  63.     char            * memcpy();
  64.     char            * new_cptr;
  65.     struct mlist        * ptr;
  66.     int              r_size;
  67.  
  68.     /*
  69.      * IF malloc chain checking is on, go do it.
  70.      */
  71.     if( malloc_checking )
  72.     {
  73.         (void) malloc_chain_check(1);
  74.     }
  75.  
  76.     /*
  77.      * verify that cptr is within the malloc region...
  78.      */
  79.     if( cptr < malloc_data_start || cptr > malloc_data_end )
  80.     {
  81.         malloc_errno = M_CODE_BAD_PTR;
  82.         malloc_warning(func);
  83.         return (NULL);
  84.     }
  85.  
  86.     /* 
  87.      * convert pointer to mlist struct pointer.  To do this we must 
  88.      * move the pointer backwards the correct number of bytes...
  89.      */
  90.     
  91.     ptr = (struct mlist *) (cptr - M_SIZE);
  92.     
  93.     if( (ptr->flag&M_MAGIC) != M_MAGIC )
  94.     {
  95.         malloc_errno = M_CODE_BAD_MAGIC;
  96.         malloc_warning(func);
  97.         return(NULL);
  98.     }
  99.  
  100.     if( ! (ptr->flag & M_INUSE) )
  101.     {
  102.         malloc_errno = M_CODE_NOT_INUSE ;
  103.         malloc_warning(func);
  104.         return(NULL);
  105.     }
  106.  
  107.      if( (ptr->prev && (ptr->prev->next != ptr) ) ||
  108.         (ptr->next && (ptr->next->prev != ptr) ) ||
  109.         ((ptr->next == NULL) && (ptr->prev == NULL)) )
  110.     {
  111.         malloc_errno = M_CODE_BAD_CONNECT;
  112.         malloc_warning(func);
  113.         return(NULL);
  114.     }
  115.  
  116.     r_size = ++size;
  117.  
  118.     M_ROUNDUP(size);
  119.  
  120.     if( size > ptr->s.size )
  121.     {
  122.         malloc_join(ptr,ptr->next,1,1);
  123.     }
  124.  
  125.     if( size > ptr->s.size )
  126.     {
  127.         /*
  128.          * else we can't combine it, so lets allocate a new chunk,
  129.          * copy the data and free the old chunk...
  130.          */
  131.         new_cptr = malloc(size);
  132.  
  133.         if( new_cptr == (char *) 0)
  134.         {
  135.             return(new_cptr);
  136.         }
  137.  
  138.         if( r_size < ptr->r_size )
  139.         {
  140.             i = r_size;
  141.         }
  142.         else
  143.         {
  144.             i = ptr->r_size;
  145.         }
  146.         (void)memcpy(new_cptr,ptr->data,i);
  147.         free(cptr);
  148.         return(new_cptr);
  149.  
  150.     } /* else... */
  151.  
  152.     /*
  153.      * save amount of real data in new segment (this will be used in the
  154.      * memset later) and then save requested size of this segment.
  155.      */
  156.  
  157.     if( ptr->r_size < r_size )
  158.     {
  159.         i = ptr->r_size;
  160.     }
  161.     else
  162.     {
  163.         i = r_size;
  164.     }
  165.  
  166.     ptr->r_size = r_size;
  167.  
  168.     /*
  169.      * split off extra free space at end of this segment, if possible...
  170.      */
  171.  
  172.     malloc_split(ptr);
  173.  
  174.     malloc_memset( ptr->data+i, M_FILL, (int) (ptr->s.size - i));
  175.         
  176.     return(ptr->data);
  177.  
  178. } /* realloc(... */
  179.  
  180.  
  181. /*
  182.  * $Log: realloc.c,v $
  183.  * Revision 1.1  1992/01/24  03:29:11  dvadura
  184.  * dmake Version 3.8, Initial revision
  185.  *
  186.  * Revision 1.8  90/08/29  21:22:52  cpcahil
  187.  * miscellaneous lint fixes
  188.  * 
  189.  * Revision 1.7  90/05/11  00:13:10  cpcahil
  190.  * added copyright statment
  191.  * 
  192.  * Revision 1.6  90/02/25  11:01:20  cpcahil
  193.  * added support for malloc chain checking.
  194.  * 
  195.  * Revision 1.5  90/02/24  21:50:31  cpcahil
  196.  * lots of lint fixes
  197.  * 
  198.  * Revision 1.4  90/02/24  17:29:39  cpcahil
  199.  * changed $Header to $Id so full path wouldnt be included as part of rcs 
  200.  * id string
  201.  * 
  202.  * Revision 1.3  90/02/24  17:20:00  cpcahil
  203.  * attempt to get rid of full path in rcs header.
  204.  * 
  205.  * Revision 1.2  90/02/24  15:14:20  cpcahil
  206.  * 1. added function header 
  207.  * 2. changed calls to malloc_warning to conform to new usage
  208.  * 3. added setting of malloc_errno
  209.  *  4. broke up bad pointer determination so that errno's would be more
  210.  *    descriptive
  211.  * 
  212.  * Revision 1.1  90/02/22  23:17:43  cpcahil
  213.  * Initial revision
  214.  * 
  215.  */
  216.