home *** CD-ROM | disk | FTP | other *** search
/ IRIS Development Option 6.2 / IRIS_Development_Option_6.2_814-0478-001.iso / dist / c_dev.idb / usr / share / src / customalloc / sbrk-realloc.c.z / sbrk-realloc.c
C/C++ Source or Header  |  1996-03-14  |  1KB  |  40 lines

  1. static MallocPtrType __sbrk_realloc(MallocPtrType mem,
  2.                          MallocArgType bytes)
  3. {
  4.   if ( mem == 0 ) {
  5.     /*
  6.      * Some people do the weirdest things.
  7.      */
  8.     return( (MallocPtrType) malloc(bytes) );
  9.   } else {
  10.     /*
  11.      * We know p is a pointer to an sbrk'd region at this point
  12.      */
  13.     int new_user_size = ((bytes + 0x3) & (~0x3));
  14.     int new_rounded_size = new_user_size + (2 * SIZE_SZ);
  15.     
  16.     SizeType *ptr = (SizeType *) mem;
  17.     char *base = (char *) &ptr[-1];
  18.     int old_rounded_size = ptr[-1] & ~0x3;
  19.     int old_user_size = old_rounded_size - (2 * SIZE_SZ);
  20.     
  21.     /*
  22.      *  OK to simply extend?
  23.      */
  24.     if ( (base + old_rounded_size) == sbrk_ptr
  25.     && ((base + new_rounded_size) < sbrk_end ) ) {
  26.       sbrk_ptr = base + new_rounded_size;
  27.       ptr[-1] = ((SizeType) new_rounded_size) | 0x3;
  28.       return( (MallocPtrType) ptr );
  29.     } else {
  30.       int copy_size = (old_user_size < new_user_size )
  31.     ? old_user_size  : new_user_size;
  32.       
  33.       MallocPtrType newmem = (MallocPtrType) malloc(new_user_size);
  34.       bcopy(mem, newmem, copy_size);
  35.       free( mem );
  36.       return( (MallocPtrType) newmem );
  37.     }
  38.   }
  39. }
  40.