MALLOC_COPY(chunk2mem(newp), chunk2mem(oldp), oldsize - SIZE_SZ);
MALLOC_COPY(BOUNDED_N(chunk2mem(newp), oldsize), oldmem, oldsize);
chunk_realloc(arena* ar_ptr, mchunkptr oldp, INTERNAL_SIZE_T oldsize,              INTERNAL_SIZE_T nb)#elsechunk_realloc(ar_ptr, oldp, oldsize, nb)arena* ar_ptr; mchunkptr oldp; INTERNAL_SIZE_T oldsize, nb;#endif{  mchunkptr newp = oldp;      /* chunk to return */  INTERNAL_SIZE_T newsize = oldsize; /* its size */  mchunkptr next;             /* next contiguous chunk after oldp */  INTERNAL_SIZE_T  nextsize;  /* its size */  mchunkptr prev;             /* previous contiguous chunk before oldp */  INTERNAL_SIZE_T  prevsize;  /* its size */  mchunkptr remainder;        /* holds split off extra space from newp */  INTERNAL_SIZE_T  remainder_size;   /* its size */  mchunkptr bck;              /* misc temp for linking */  mchunkptr fwd;              /* misc temp for linking */  check_inuse_chunk(ar_ptr, oldp);  if ((long)(oldsize) < (long)(nb))  {    /* Try expanding forward */    next = chunk_at_offset(oldp, oldsize);    if (next == top(ar_ptr) || !inuse(next))    {      nextsize = chunksize(next);      /* Forward into top only if a remainder */      if (next == top(ar_ptr))      {        if ((long)(nextsize + newsize) >= (long)(nb + MINSIZE))        {          newsize += nextsize;          top(ar_ptr) = chunk_at_offset(oldp, nb);          set_head(top(ar_ptr), (newsize - nb) | PREV_INUSE);          set_head_size(oldp, nb);          return oldp;        }      }      /* Forward into next chunk */      else if (((long)(nextsize + newsize) >= (long)(nb)))      {        unlink(next, bck, fwd);        newsize  += nextsize;        goto split;      }    }    else    {      next = 0;      nextsize = 0;    }    /* Try shifting backwards. */    if (!prev_inuse(oldp))    {      prev = prev_chunk(oldp);      prevsize = chunksize(prev);      /* try forward + backward first to save a later consolidation */      if (next != 0)      {        /* into top */        if (next == top(ar_ptr))        {          if ((long)(nextsize + prevsize + newsize) >= (long)(nb + MINSIZE))          {            unlink(prev, bck, fwd);            newp = prev;            newsize += prevsize + nextsize;            MALLOC_COPY(chunk2mem(newp), chunk2mem(oldp), oldsize - SIZE_SZ);            top(ar_ptr) = chunk_at_offset(newp, nb);            set_head(top(ar_ptr), (newsize - nb) | PREV_INUSE);            set_head_size(newp, nb);            return newp;          }        }        /* into next chunk */        else if (((long)(nextsize + prevsize + newsize) >= (long)(nb)))        {          unlink(next, bck, fwd);          unlink(prev, bck, fwd);          newp = prev;          newsize += nextsize + prevsize;          MALLOC_COPY(chunk2mem(newp), chunk2mem(oldp), oldsize - SIZE_SZ);          goto split;        }      }      /* backward only */      if (prev != 0 && (long)(prevsize + newsize) >= (long)nb)      {        unlink(prev, bck, fwd);        newp = prev;        newsize += prevsize;        MALLOC_COPY(chunk2mem(newp), chunk2mem(oldp), oldsize - SIZE_SZ);        goto split;      }    }    /* Must allocate */    newp = chunk_alloc (ar_ptr, nb);    if (newp == 0) {      /* Maybe the failure is due to running out of mmapped areas. */      if (ar_ptr != &main_arena) {        (void)mutex_lock(&main_arena.mutex);        newp = chunk_alloc(&main_arena, nb);        (void)mutex_unlock(&main_arena.mutex);      } else {#if USE_ARENAS        /* ... or sbrk() has failed and there is still a chance to mmap() */        arena* ar_ptr2 = arena_get2(ar_ptr->next ? ar_ptr : 0, nb);        if(ar_ptr2) {          newp = chunk_alloc(ar_ptr2, nb);          (void)mutex_unlock(&ar_ptr2->mutex);        }#endif      }      if (newp == 0) /* propagate failure */        return 0;    }    /* Avoid copy if newp is next chunk after oldp. */    /* (This can only happen when new chunk is sbrk'ed.) */    if ( newp == next_chunk(oldp))    {      newsize += chunksize(newp);      newp = oldp;      goto split;    }    /* Otherwise copy, free, and exit */    MALLOC_COPY(chunk2mem(newp), chunk2mem(oldp), oldsize - SIZE_SZ);    chunk_free(ar_ptr, oldp);    return newp;  } split:  /* split off extra room in old or expanded chunk */  if (newsize - nb >= MINSIZE) /* split off remainder */  {    remainder = chunk_at_offset(newp, nb);    remainder_size = newsize - nb;    set_head_size(newp, nb);    set_head(remainder, remainder_size | PREV_INUSE);    set_inuse_bit_at_offset(remainder, remainder_size);    chunk_free(ar_ptr, remainder);  }  else  {    set_head_size(newp, newsize);    set_inuse_bit_at_offset(newp, newsize);  }  check_inuse_chunk(ar_ptr, newp);  return newp;}