heap_info *heap;
heap_info *old_heap, *heap; size_t old_heap_size;
static void malloc_extend_top(arena *ar_ptr, INTERNAL_SIZE_T nb)#elsestatic void malloc_extend_top(ar_ptr, nb) arena *ar_ptr; INTERNAL_SIZE_T nb;#endif{  unsigned long pagesz   = malloc_getpagesize;  mchunkptr old_top      = top(ar_ptr);        /* Record state of old top */  INTERNAL_SIZE_T old_top_size = chunksize(old_top);  INTERNAL_SIZE_T top_size;                    /* new size of top chunk */#ifndef NO_THREADS  if(ar_ptr == &main_arena) {#endif    char*     brk;                  /* return value from sbrk */    INTERNAL_SIZE_T front_misalign; /* unusable bytes at front of sbrked space */    INTERNAL_SIZE_T correction;     /* bytes for 2nd sbrk call */    char*     new_brk;              /* return of 2nd sbrk call */    char*     old_end = (char*)(chunk_at_offset(old_top, old_top_size));    /* Pad request with top_pad plus minimal overhead */    INTERNAL_SIZE_T sbrk_size = nb + top_pad + MINSIZE;    /* If not the first time through, round to preserve page boundary */    /* Otherwise, we need to correct to a page size below anyway. */    /* (We also correct below if an intervening foreign sbrk call.) */    if (sbrk_base != (char*)(-1))      sbrk_size = (sbrk_size + (pagesz - 1)) & ~(pagesz - 1);    brk = (char*)(MORECORE (sbrk_size));    /* Fail if sbrk failed or if a foreign sbrk call killed our space */    if (brk == (char*)(MORECORE_FAILURE) ||        (brk < old_end && old_top != initial_top(&main_arena)))      return;    sbrked_mem += sbrk_size;    if (brk == old_end) { /* can just add bytes to current top */      top_size = sbrk_size + old_top_size;      set_head(old_top, top_size | PREV_INUSE);      old_top = 0; /* don't free below */    } else {      if (sbrk_base == (char*)(-1)) /* First time through. Record base */        sbrk_base = brk;      else        /* Someone else called sbrk().  Count those bytes as sbrked_mem. */        sbrked_mem += brk - (char*)old_end;      /* Guarantee alignment of first new chunk made from this space */      front_misalign = (unsigned long)chunk2mem(brk) & MALLOC_ALIGN_MASK;      if (front_misalign > 0) {        correction = (MALLOC_ALIGNMENT) - front_misalign;        brk += correction;      } else        correction = 0;      /* Guarantee the next brk will be at a page boundary */      correction += pagesz - ((unsigned long)(brk + sbrk_size) & (pagesz - 1));      /* Allocate correction */      new_brk = (char*)(MORECORE (correction));      if (new_brk == (char*)(MORECORE_FAILURE)) return;      sbrked_mem += correction;      top(&main_arena) = (mchunkptr)brk;      top_size = new_brk - brk + correction;      set_head(top(&main_arena), top_size | PREV_INUSE);      if (old_top == initial_top(&main_arena))        old_top = 0; /* don't free below */    }    if ((unsigned long)sbrked_mem > (unsigned long)max_sbrked_mem)      max_sbrked_mem = sbrked_mem;    if ((unsigned long)(mmapped_mem + sbrked_mem) >        (unsigned long)max_total_mem)      max_total_mem = mmapped_mem + sbrked_mem;#ifndef NO_THREADS  } else { /* ar_ptr != &main_arena */    heap_info *heap;    if(old_top_size < MINSIZE) /* this should never happen */      return;    /* First try to extend the current heap. */    if(MINSIZE + nb <= old_top_size)      return;    heap = heap_for_ptr(old_top);    if(grow_heap(heap, MINSIZE + nb - old_top_size) == 0) {      top_size = heap->size - ((char *)old_top - (char *)heap);      set_head(old_top, top_size | PREV_INUSE);      return;    }    /* A new heap must be created. */    heap = new_heap(nb + top_pad + (MINSIZE + sizeof(*heap)));    if(!heap)      return;    heap->ar_ptr = ar_ptr;    /* Set up the new top, so we can safely use chunk_free() below. */    top(ar_ptr) = chunk_at_offset(heap, sizeof(*heap));    top_size = heap->size - sizeof(*heap);    set_head(top(ar_ptr), top_size | PREV_INUSE);  }#endif /* !defined(NO_THREADS) */  /* We always land on a page boundary */  assert(((unsigned long)((char*)top(ar_ptr) + top_size) & (pagesz-1)) == 0);  /* Setup fencepost and free the old top chunk. */  if(old_top) {    /* Keep size a multiple of MALLOC_ALIGNMENT. */    old_top_size = (old_top_size - 3*SIZE_SZ) & ~MALLOC_ALIGN_MASK;    /* If possible, release the rest. */    if (old_top_size >= MINSIZE) {      set_head(chunk_at_offset(old_top, old_top_size        ),               SIZE_SZ|PREV_INUSE);      set_head(chunk_at_offset(old_top, old_top_size+SIZE_SZ),               SIZE_SZ|PREV_INUSE);      set_head_size(old_top, old_top_size);      chunk_free(ar_ptr, old_top);    } else {      set_head(old_top, SIZE_SZ|PREV_INUSE);      set_head(chunk_at_offset(old_top, SIZE_SZ), SIZE_SZ|PREV_INUSE);    }  }}