malloc_guarded += malloc_guard;
malloc_pages(size_t size){    void *p, *delay_free = NULL;    int i;    struct rlimit rl;    struct pginfo **pd;    struct pdinfo *pi;    u_long pidx;    void *tp;    struct pgfree *pf;    u_long index;    int m;    size = pageround(size) + malloc_guard;    p = NULL;    /* Look for free pages before asking for more */    for (pf = free_list.next; pf; pf = pf->next) {#ifdef	MALLOC_EXTRA_SANITY	if (pf->size & malloc_pagemask) {	    wrterror("(ES): junk length entry on free_list\n");	    errno = EFAULT;	    return (NULL);	}	if (!pf->size) {	    wrterror("(ES): zero length entry on free_list\n");	    errno = EFAULT;	    return (NULL);	}	if (pf->page > (pf->page + pf->size)) {	    wrterror("(ES): sick entry on free_list\n");	    errno = EFAULT;	    return (NULL);	}	if ((pi = pf->pdir) == NULL) {	    wrterror("(ES): invalid page directory on free-list\n");	    errno = EFAULT;	    return (NULL);	}	if ((pidx = PI_IDX(ptr2index(pf->page))) != PD_IDX(pi->dirnum)) {	    wrterror("(ES): directory index mismatch on free-list\n");	    errno = EFAULT;	    return (NULL);	}	pd = pi->base;	if (pd[PI_OFF(ptr2index(pf->page))] != MALLOC_FREE) {	    wrterror("(ES): non-free first page on free-list\n");	    errno = EFAULT;	    return (NULL);	}	pidx = PI_IDX(ptr2index((pf->page)+(pf->size))-1);	for (pi=pf->pdir; pi!=NULL && PD_IDX(pi->dirnum)<pidx; pi=pi->next);	if (pi == NULL || PD_IDX(pi->dirnum) != pidx) {	    wrterror("(ES): last page not referenced in page directory\n");	    errno = EFAULT;	    return (NULL);	}	pd = pi->base;	if (pd[PI_OFF(ptr2index((pf->page)+(pf->size))-1)] != MALLOC_FREE) {	    wrterror("(ES): non-free last page on free-list\n");	    errno = EFAULT;	    return (NULL);	}#endif	/* MALLOC_EXTRA_SANITY */	if (pf->size < size)	    continue;	if (pf->size == size) {	    p = pf->page;	    pi = pf->pdir;	    if (pf->next != NULL)		    pf->next->prev = pf->prev;	    pf->prev->next = pf->next;	    delay_free = pf;	    break;	}	p = pf->page;	pf->page = (char *)pf->page + size;	pf->size -= size;	pidx = PI_IDX(ptr2index(pf->page));	for (pi=pf->pdir; pi!=NULL && PD_IDX(pi->dirnum)<pidx; pi=pi->next);	if (pi == NULL || PD_IDX(pi->dirnum) != pidx) {	    wrterror("(ES): hole in directories\n");	    errno = EFAULT;	    return (NULL);	}	tp = pf->pdir;	pf->pdir = pi;	pi = tp;	break;    }    size -= malloc_guard;#ifdef	MALLOC_EXTRA_SANITY    if (p != NULL && pi != NULL) {	pidx = PD_IDX(pi->dirnum);	pd = pi->base;    }    if (p != NULL && pd[PI_OFF(ptr2index(p))] != MALLOC_FREE) {	wrterror("(ES): allocated non-free page on free-list\n");	errno = EFAULT;	return (NULL);    }#endif	/* MALLOC_EXTRA_SANITY */    if (p != NULL && (malloc_guard || malloc_freeprot))	mprotect(p, size, PROT_READ|PROT_WRITE);    size >>= malloc_pageshift;    /* Map new pages */    if (p == NULL)	p = map_pages(size);    if (p != NULL) {	index = ptr2index(p);	pidx = PI_IDX(index);	pdir_lookup(index, &pi);#ifdef	MALLOC_EXTRA_SANITY	if (pi == NULL || PD_IDX(pi->dirnum) != pidx) {	    wrterror("(ES): mapped pages not found in directory\n");	    errno = EFAULT;	    return (NULL);	}#endif	/* MALLOC_EXTRA_SANITY */	if (pi != last_dir) {	    prev_dir = last_dir;	    last_dir = pi;	}	pd = pi->base;	pd[PI_OFF(index)] = MALLOC_FIRST;	for (i=1;i<size;i++) {	    if (!PI_OFF(index+i)) {		pidx++;		pi = pi->next;#ifdef	MALLOC_EXTRA_SANITY		if (pi == NULL || PD_IDX(pi->dirnum) != pidx) {		    wrterror("(ES): hole in mapped pages directory\n");		    errno = EFAULT;		    return (NULL);		}#endif	/* MALLOC_EXTRA_SANITY */		pd = pi->base;	    }	    pd[PI_OFF(index+i)] = MALLOC_FOLLOW;	}	if (malloc_guard) {	    if (!PI_OFF(index+i)) {		pidx++;		pi = pi->next;#ifdef	MALLOC_EXTRA_SANITY		if (pi == NULL || PD_IDX(pi->dirnum) != pidx) {		    wrterror("(ES): hole in mapped pages directory\n");		    errno = EFAULT;		    return (NULL);		}#endif	/* MALLOC_EXTRA_SANITY */		pd = pi->base;	    }	    pd[PI_OFF(index+i)] = MALLOC_FIRST;	}	malloc_used += size << malloc_pageshift;	if (malloc_junk)	    memset(p, SOME_JUNK, size << malloc_pageshift);    }    if (delay_free) {	if (px == NULL)	    px = delay_free;	else	    ifree(delay_free);    }    return (p);}